Skip to content

Instantly share code, notes, and snippets.

@marcoslhc
Last active April 9, 2017 01:38
Show Gist options
  • Select an option

  • Save marcoslhc/bb3763a34578b06742b501ff7bd3236b to your computer and use it in GitHub Desktop.

Select an option

Save marcoslhc/bb3763a34578b06742b501ff7bd3236b to your computer and use it in GitHub Desktop.
Transducer composed
// 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