From aaacfd6f4d6dd57098037de179f98d846b6ad5f3 Mon Sep 17 00:00:00 2001 From: elioat Date: Wed, 19 Jul 2023 22:58:17 -0400 Subject: * --- js/b.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 js/b.js (limited to 'js') diff --git a/js/b.js b/js/b.js new file mode 100644 index 0000000..d1d44b2 --- /dev/null +++ b/js/b.js @@ -0,0 +1,50 @@ +// b is for useful stuff + +'use strict' + +const b = { + pipe: (...args) => args.reduce((acc, el) => el(acc)), + compose: (...fns) => (...args) => fns.reduceRight((res, fn) => [fn.call(null, ...res)], args)[0], + identity: x => x, + curry: (fn) => { + let curried = (...args) => { + if (args.length >= fn.length) + return fn(...args) + else + return (...rest) => curried(...args, ...rest) + } + return curried + }, + match: () => b.curry((what, s) => s.match(what)), + replace: () => b.curry((what, replacement, s) => s.replace(what, replacement)), + filter: () => b.curry((f, xs) => xs.filter(f)), + map: () => b.curry((f, xs) => xs.map(f)) +}; + + +// pipe +const title = '10 Weird Facts About Dogs'; +const toLowerCase = (str) => str.toLowerCase(); +const addHyphens = (str) => str.replace(/\s/g, '-'); +const reverseText = (str) => (str === '') ? '' : reverseText(str.substr(1)) + str.charAt(0); +const slug = b.pipe(title, toLowerCase, addHyphens, reverseText); +console.log('pipe ', slug); + + +// compose +const toUpperCase = x => x.toUpperCase(); +const exclaim = x => `${x}!`; +const shout = b.compose(exclaim, toUpperCase); +const ret = shout('send in the clowns'); // "SEND IN THE CLOWNS!" +console.log('compose ', ret); + + +// curry family +const hasLetterR = b.match(/r/g); +const r1 = hasLetterR('hello world'); +const r2 = hasLetterR('just j and s and t etc'); +const r3 = b.filter(hasLetterR, ['rock and roll', 'smooth jazz']); +const noVowels = b.replace(/[aeiou]/ig); +const censored = noVowels('*'); +const r4 = censored('Chocolate Rain'); +console.log('curry ', r1, r2, r3, r4); \ No newline at end of file -- cgit 1.4.1-2-gfad0