Last active
April 9, 2017 01:38
-
-
Save marcoslhc/bb3763a34578b06742b501ff7bd3236b to your computer and use it in GitHub Desktop.
Transducer composed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Stripped straight from the good Dan Abramov | |
| // https://gist.github.com/gaearon/ffd88b0e4f00b22c3159 | |
| function compose(...functions) { | |
| return function (arg) { | |
| return functions.reduceRight(function (composed, f) { | |
| return f(composed); | |
| }, arg); | |
| }; | |
| }; | |
| // we need a function to consistenly provide the same type | |
| // expect(ingest([], 1)).toEqual([1]); | |
| // expect(ingest([1], 2)).toEqual([1, 2]); | |
| function ingest(accum, value) { | |
| return accum.concat(value); | |
| } | |
| // expect(getSelectedClass(/--solarized/)('btn--solarized')).toBe(true) | |
| function getSelectedClass(classRegex) { | |
| return function(classStr) { | |
| classRegex.test(classStr); | |
| } | |
| } | |
| // expect(addClassModifier('active')('btn')).toBe("btn--active"); | |
| function addClassModifier(className) { | |
| return function (classStr) { | |
| return classStr.concat('--').concat(className); | |
| } | |
| } | |
| const transducer = compose( | |
| mapReduce(addClassModifier('active')), | |
| filterReduce(getSelectedClass(/--solarized/)), | |
| )(ingest); | |
| ClassList.of([ | |
| 'btn__background--solarized', | |
| 'btn__text--solarized', | |
| 'btn__overlay--solarized', | |
| 'btn__icon', | |
| 'btn__placeholder' | |
| ]).reduce(transducer, []); | |
| // ClassList.of([ | |
| // "btn__background--solarized--active", | |
| // "btn__text--solarized--active", | |
| // "btn__overlay--solarized--active" | |
| // ]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment