@bitty/pipe
Version:
A pipe function to perform function composition in LTR (Left To Right) direction.
29 lines (27 loc) • 855 B
JavaScript
/**
* Performs function composition in LTR (Left To Right) direction.
*
* @example
* const normalizeWhiteSpaces = text => name.replace(/\s+/g, ' ').trim();
*
* const getInitials = pipe(
* normalizeWhiteSpaces,
* name => name.split(' ').map(name => name.charAt(0)),
* initials => initials.join('').toLocaleUpperCase()
* );
*
* getInitials('Vitor Luiz Cavalcanti');
* //=> "VLC"
*
* @param {Function} fn - An arity N function. Its result is the argument of next one.
* @param {...Function[]} fns - Functions of arity 1. Each one's result is next's argument.
* @returns {Function}
*/
function pipe(fn) {
var fns = [].slice.call(arguments, 1);
return function () {
return fns.reduce(function (x, fn) { return fn(x); }, fn.apply(null, arguments));
};
}
export { pipe as default };
//# sourceMappingURL=pipe.mjs.map