From 941a5f97c8cd0eaa005a2c3c0395e26f7e37aeb6 Mon Sep 17 00:00:00 2001 From: elioat Date: Tue, 20 Aug 2024 16:09:06 -0400 Subject: * --- js/b/b.js | 16 ++++++++-------- js/b/tests.js | 28 ++++++++++++++-------------- 2 files changed, 22 insertions(+), 22 deletions(-) (limited to 'js/b') diff --git a/js/b/b.js b/js/b/b.js index ee33eef..04959c4 100644 --- a/js/b/b.js +++ b/js/b/b.js @@ -20,17 +20,17 @@ // */ -// b is for useful stuff +// b is for useful stuff -// pipe: Runs functions from left to right. -// compose: Runs functions from right to left. -// identity: Returns whatever you give it as is. -// curry: Converts a function to its curried form. +// curry: Converts a function to its curried form, allowing for partial application. +// pipe: Runs functions from left to right, passing the result of one to the next. +// compose: Runs functions from right to left, passing the result of one to the next. +// identity: Returns whatever you give it as is. Useful for composition...sometimes. // match: Creates a curried function to match a regular expression in a string. -// replace: Creates a curried function to replace parts of a string. -// filter: Curried array filtering. +// replace: Creates a curried function to replace parts of a string, based on a regex or substring. +// filter: Curried array filtering by a predicate function. // map: Curried array mapping. -// deepMap: Curried recursive mapping for nested arrays or matrices. +// deepMap: Curried recursive mapping for nested arrays or matrices, of any shape. 'use strict' diff --git a/js/b/tests.js b/js/b/tests.js index 62ebf4a..5b8ee87 100644 --- a/js/b/tests.js +++ b/js/b/tests.js @@ -1,39 +1,39 @@ import b from './b'; -// Tests! + +// curry +const add = (x, y) => x + y; +const curriedAdd = b.curry(add); +console.assert(curriedAdd(3)(4) === 7, 'Unexpected result from curried function'); + // pipe const addOne = (x) => x + 1; const double = (x) => x * 2; -console.assert(b.pipe(addOne, double)(3) === 8, 'Test for pipe failed'); +console.assert(b.pipe(addOne, double)(3) === 8, 'Unexpected result from pipe'); // compose -console.assert(b.compose(double, addOne)(3) === 8, 'Test for compose failed'); +console.assert(b.compose(double, addOne)(3) === 8, 'Unexpected result from compose'); // 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'); +console.assert(b.identity(5) === 5, 'Unexpected result from identity'); // match const matchDigits = b.match()(/\d+/g); -console.assert(JSON.stringify(matchDigits('a1b2c3')) === JSON.stringify(['1', '2', '3']), 'Test for match failed'); +console.assert(JSON.stringify(matchDigits('a1b2c3')) === JSON.stringify(['1', '2', '3']), 'Unexpected result from match'); // 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'); +console.assert(censor('This is a badword in a sentence.') === 'This is a **** in a sentence.', 'Unexpected result from replace'); // 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'); +console.assert(JSON.stringify(filterEvens([1, 2, 3, 4])) === JSON.stringify([2, 4]), 'Unexpected result from filter'); // map const mapDoubles = b.map()(double); -console.assert(JSON.stringify(mapDoubles([1, 2, 3])) === JSON.stringify([2, 4, 6]), 'Test for map failed'); +console.assert(JSON.stringify(mapDoubles([1, 2, 3])) === JSON.stringify([2, 4, 6]), 'Unexpected result from map'); // 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 +console.assert(JSON.stringify(b.deepMap()(double, nestedArray)) === JSON.stringify([2, [4, [6, 8]], [10, 12]]), 'Unexpected result from deepMap'); \ No newline at end of file -- cgit 1.4.1-2-gfad0