Learn more  » Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

skava / chain-able-deps   js

Repository URL to install this package:

Version: 6.0.4 

/ src / fp / pipeTwo.ts

/**
 * Performs left-to-right function composition. ONLY CAN PIPE 2 ARGUMENTS
 *
 * @NOTE The result of pipe is not automatically curried.
 * @NOTE This is a variation, is the internal version with only 2 functions, for now
 *
 * @func
 * @memberOf fp
 * @since v5.0.0
 * @category Function
 *
 * @param f function first
 * @param g function next
 *
 * @see https://github.com/ramda/ramda/blob/master/src/pipe.js
 * @see https://github.com/ramda/ramda/blob/master/test/pipe.js
 *
 * @types fp
 * @tests fp/pipe
 *
 * @example
 *
 *      var f = R.pipe(Math.pow, R.negate);
 *      f(3, 4); // -(3^4) + 1
 *
 */
interface PipeFunction<Return = any, Arg = any> extends Function {
  call(thisArg: any): Return
  (): Return
  (...args: Arg[]): Return
}

function pipe<One = PipeFunction<One, any>, Two = PipeFunction>(
  this: any,
  first: One,
  second: Two
) {
  type PipeReturn = ReturnType<One>

  // return second.call(this, first.apply(this, arguments))
  return (...args: any[]): PipeReturn => {
    // (first as PipeFunction<Array<ReturnType<One | Two>>>)
    const firstResult = (first as any)(...args)
    const secondResult = (second as any)(...firstResult)
    return secondResult as PipeReturn
  }
}

export default pipe