Skip to content

Instantly share code, notes, and snippets.

@dinocarl
Last active March 12, 2020 17:28
Show Gist options
  • Save dinocarl/f914d11f55a990e25cbf3b6f760ad703 to your computer and use it in GitHub Desktop.
Save dinocarl/f914d11f55a990e25cbf3b6f760ad703 to your computer and use it in GitHub Desktop.
const test = require('tape');
const emptyContainer = () => null;
const checkWith = el1 => el2 => el1 === el2 ? el2 : null;
const inContainer = ( containingFn, el ) => containingFn( el ) === el;
const inEither = ( a, b ) => el => inContainer( a, el ) ? el : b( el );
const inAny = ( ...containingFns ) => containingFns.reduce( inEither, checkWith( null ) );
const inBoth = ( a, b ) => el => inContainer( a, el ) && inContainer( b, el ) ? el : null;
const inAll = ( ...containingFns ) => containingFns.reduce( inBoth, checkWith );
// test data
const randEls = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const fnSet = checkWith('success');
const fnSet1 = checkWith(true);
const fnSet2 = checkWith(false);
const fnSet3 = checkWith('test1');
const fnSet4 = checkWith('test2');
const fnSet5 = checkWith('test1');
const fnSet6 = checkWith(false);
const uSet = inEither(fnSet3, fnSet4);
const uSet1 = inEither(fnSet1, fnSet2);
const uSet2 = inEither(fnSet2, fnSet1);
const uaSet = inAny( ...randEls.map(checkWith) );
const iSet = inBoth(fnSet3, fnSet5);
const iSet1 = inBoth(fnSet3, fnSet4);
const iSet2 = inBoth(fnSet1, fnSet2);
const iSet3 = inBoth(fnSet2, fnSet1);
const iSet4 = inBoth(fnSet2, fnSet6);
const iSet5 = inBoth(fnSet6, fnSet2);
// assemble the test data
const testData = [].concat(
{
title: 'Empty Container',
result: [
emptyContainer(1),
emptyContainer(false),
emptyContainer('test'),
],
expect1: [null, null, null],
},
{
title: 'Check With',
result: [
fnSet(1),
fnSet(false),
fnSet('test'),
],
expect1: [null, null, null],
},
{
title: 'In Container',
result: [
inContainer(fnSet, 'success'),
inContainer(fnSet2, false),
inContainer(fnSet, 'fail'),
inContainer(fnSet2, true),
],
expect1: [true, true, false, false],
},
{
title: 'In Either',
result: [
uSet(1),
uSet(false),
uSet('test'),
uSet('test1'),
uSet('test2'),
uSet1(false),
uSet2(false),
],
expect1: [null, null, null, 'test1', 'test2', false, false],
},
{
title: 'In Any',
result: [
uaSet(1),
uaSet(3),
uaSet(7),
uaSet(10),
uaSet(false),
uaSet('test'),
uaSet('test1'),
uaSet('test2'),
],
expect1: [1, 3, 7, 10, null, null, null, null],
},
{
title: 'In Both - Overlap',
result: [
iSet(1),
iSet(false),
iSet('test'),
iSet('test1'),
],
expect1: [null, null, null, 'test1'],
},
{
title: 'In Both - No Overlap',
result: [
iSet1(1),
iSet1(false),
iSet1('test'),
iSet1('test1'),
iSet1('test2'),
],
expect1: [null, null, null, null, null],
},
{
title: 'In Both - True/False Checks',
result: [
iSet2(false),
iSet3(false),
iSet4(false),
iSet5(false),
],
expect1: [null, null, false, false],
},
);
// run the tests
test('FSet Tests', function (assert) {
assert.plan(testData.length);
testData.forEach(function (testCase) {
assert.deepEqual(testCase.result, testCase.expect1, testCase.title);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment