Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @module lib/compose.js
- */
- /**
- * Identity function
- *
- * @param {any} arg An input
- * @returns {any} The original input
- * @private
- */
- const identity = arg => arg;
- /**
- * Check if function is promise
- *
- * @param {function} fn Function to check
- * @returns {boolean} Whether function is promise
- * @private
- */
- const isPromise = fn => {
- return fn != null
- && typeof fn === 'object'
- && typeof fn.then === 'function'
- && typeof fn.catch === 'function';
- };
- /**
- * Create composed function
- *
- * @param {function} first First function
- * @param {...function} rest Rest of the functions
- * @returns {function} Composed function
- */
- export default (first = identity, ...rest) => {
- return (...args) => {
- return rest.reduce(
- (computed, fn) => {
- return isPromise(computed)
- ? computed.then(fn)
- : fn(computed);
- },
- first(...args)
- );
- };
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement