UNPKG

@bitty/pipe

Version:

A pipe function to perform function composition in LTR (Left To Right) direction.

37 lines (33 loc) 1.29 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.pipe = factory()); })(this, (function () { 'use strict'; /** * 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)); }; } return pipe; })); //# sourceMappingURL=pipe.umd.js.map