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 / nthArg.ts

import argumentor from '../cast/fromArgumentsToArray'
import curryN from './curry'
import nth from './nth'

type Nth<List, Index extends keyof List> = List[Index]

/**
 * @desc Returns a function which returns its nth argument.
 * @memberOf fp
 * @since 5.0.0-beta.6
 *
 * @param n arg to get
 * @see deps/argumentor
 *
 * @curried 1
 * @tests fp/nthArg
 *
 * @func
 * @fork v0.9.0
 * @category Function
 * @sig Number -> *... -> *
 *
 * @symb nthArg(-1)(a, b, c) = c
 * @symb nthArg(0)(a, b, c) = a
 * @symb nthArg(1)(a, b, c) = b
 *
 * @example
 *
 *      nthArg(1)('a', 'b', 'c')  //=> 'b'
 *      nthArg(-1)('a', 'b', 'c') //=> 'c'
 *
 */
const nthArg = function(n: number) {
  const arity = n < 0 ? 1 : n + 1

  // <typeof arity>
  return curryN(arity, function<Type extends Array<any>>(
    ...args: Type
  ): Nth<Type, typeof arity> {
    // return nth(n, argumentor.apply(null, arguments))
    return nth(argumentor.apply(undefined, arguments), n)
  })
}

export default nthArg