diff options
author | elioat <elioat@tilde.institute> | 2023-07-19 22:58:17 -0400 |
---|---|---|
committer | elioat <elioat@tilde.institute> | 2023-07-19 22:58:17 -0400 |
commit | aaacfd6f4d6dd57098037de179f98d846b6ad5f3 (patch) | |
tree | 0284aa0218c853f3da67d0ad41ce8e4c73b016a8 /js | |
parent | 313c7d81a673031813bfca84fa64eb3cbe7be24b (diff) | |
download | tour-aaacfd6f4d6dd57098037de179f98d846b6ad5f3.tar.gz |
*
Diffstat (limited to 'js')
-rw-r--r-- | js/b.js | 50 |
1 files changed, 50 insertions, 0 deletions
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 |