From c27d24ab1109b94720deb03dea68d1779e7f807d Mon Sep 17 00:00:00 2001 From: elioat Date: Tue, 20 Aug 2024 11:33:58 -0400 Subject: * --- js/b/b.js | 38 +------------------------------------- js/b/b.min.js | 4 ---- js/b/tests.js | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 41 deletions(-) delete mode 100644 js/b/b.min.js create mode 100644 js/b/tests.js (limited to 'js') diff --git a/js/b/b.js b/js/b/b.js index 5462e93..ee33eef 100644 --- a/js/b/b.js +++ b/js/b/b.js @@ -119,40 +119,4 @@ const b = { }, }; -// Tests! -// pipe -const addOne = (x) => x + 1; -const double = (x) => x * 2; -console.assert(b.pipe(addOne, double)(3) === 8, 'Test for pipe failed'); - -// compose -console.assert(b.compose(double, addOne)(3) === 8, 'Test for compose failed'); - -// identity -console.assert(b.identity(5) === 5, 'Test for identity failed'); - -// curry -const add = (x, y) => x + y; -const curriedAdd = b.curry(add); -console.assert(curriedAdd(3)(4) === 7, 'Test for curry failed'); - -// match -const matchDigits = b.match()(/\d+/g); -console.assert(JSON.stringify(matchDigits('a1b2c3')) === JSON.stringify(['1', '2', '3']), 'Test for match failed'); - -// replace -const censor = b.replace()(/badword/g, '****'); -console.assert(censor('This is a badword in a sentence.') === 'This is a **** in a sentence.', 'Test for replace failed'); - -// filter -const isEven = (x) => x % 2 === 0; -const filterEvens = b.filter()(isEven); -console.assert(JSON.stringify(filterEvens([1, 2, 3, 4])) === JSON.stringify([2, 4]), 'Test for filter failed'); - -// map -const mapDoubles = b.map()(double); -console.assert(JSON.stringify(mapDoubles([1, 2, 3])) === JSON.stringify([2, 4, 6]), 'Test for map failed'); - -// deepMap -const nestedArray = [1, [2, [3, 4]], [5, 6]]; -console.assert(JSON.stringify(b.deepMap()(double, nestedArray)) === JSON.stringify([2, [4, [6, 8]], [10, 12]]), 'Test for deepMap failed'); \ No newline at end of file +export default b; \ No newline at end of file diff --git a/js/b/b.min.js b/js/b/b.min.js deleted file mode 100644 index c94132e..0000000 --- a/js/b/b.min.js +++ /dev/null @@ -1,4 +0,0 @@ -// eli_oat -// docs available in the unminified version -// https://eli.li/_assets/bin/b.js -'use strict';const b={curry:function(fn){const curried=(...args)=>{if(args.length>=fn.length){return fn(...args)}else{return(...rest)=>curried(...args,...rest)}}return curried},pipe:(...fns)=>(value)=>fns.reduce((acc,fn)=>fn(acc),value),compose:(...fns)=>(...args)=>fns.reduceRight((res,fn)=>fn(...[].concat(res)),args),identity:x=>x,match:function(){return this.curry((what,s)=>s.match(what))},replace:function(){return this.curry((what,replacement,s)=>s.replace(what,replacement))},filter:function(){return this.curry((f,xs)=>xs.filter(f))},map:function(){return this.curry((f,xs)=>xs.map(f))},deepMap:function(){return this.curry(function deepMap(f,xs){return Array.isArray(xs)?xs.map((x)=>deepMap(f,x)):f(xs)})}}; \ No newline at end of file diff --git a/js/b/tests.js b/js/b/tests.js new file mode 100644 index 0000000..62ebf4a --- /dev/null +++ b/js/b/tests.js @@ -0,0 +1,39 @@ +import b from './b'; + +// Tests! +// pipe +const addOne = (x) => x + 1; +const double = (x) => x * 2; +console.assert(b.pipe(addOne, double)(3) === 8, 'Test for pipe failed'); + +// compose +console.assert(b.compose(double, addOne)(3) === 8, 'Test for compose failed'); + +// identity +console.assert(b.identity(5) === 5, 'Test for identity failed'); + +// curry +const add = (x, y) => x + y; +const curriedAdd = b.curry(add); +console.assert(curriedAdd(3)(4) === 7, 'Test for curry failed'); + +// match +const matchDigits = b.match()(/\d+/g); +console.assert(JSON.stringify(matchDigits('a1b2c3')) === JSON.stringify(['1', '2', '3']), 'Test for match failed'); + +// replace +const censor = b.replace()(/badword/g, '****'); +console.assert(censor('This is a badword in a sentence.') === 'This is a **** in a sentence.', 'Test for replace failed'); + +// filter +const isEven = (x) => x % 2 === 0; +const filterEvens = b.filter()(isEven); +console.assert(JSON.stringify(filterEvens([1, 2, 3, 4])) === JSON.stringify([2, 4]), 'Test for filter failed'); + +// map +const mapDoubles = b.map()(double); +console.assert(JSON.stringify(mapDoubles([1, 2, 3])) === JSON.stringify([2, 4, 6]), 'Test for map failed'); + +// deepMap +const nestedArray = [1, [2, [3, 4]], [5, 6]]; +console.assert(JSON.stringify(b.deepMap()(double, nestedArray)) === JSON.stringify([2, [4, [6, 8]], [10, 12]]), 'Test for deepMap failed'); \ No newline at end of file -- cgit 1.4.1-2-gfad0