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

import curry from '../fp/curry'
import { isString } from '../is'

/**
 * Gets the element at index `n` of `array`. If `n` is negative, the nth
 * element from the end is returned.
 *
 * @memberOf fp
 * @since 5.0.0-beta.1
 *
 * @param array The array|obj|string to query.
 * @param [n=0] The index of the element to return.
 * @returns the nth element of `array`.
 *
 * @tests fp/arity
 *
 * @fork 4.11.0
 * @category Array
 *
 * @name nth
 * @alias at
 * @alias atIndex
 * @alias atPosition
 *
 * {@link http://documentcloud.github.io/underscore-contrib/#nth underscore-contrib-nth}
 * {@link https://github.com/lodash/lodash/blob/master/.internal/baseAt.js lodash-base-at}
 * {@link https://github.com/lodash/lodash/blob/master/at.js lodash-at}
 * {@link https://github.com/ramda/ramda/blob/v0.24.1/src/nth.js ramda-nth}
 *
 * @see {@link ramda-nth}
 * @see {@link lodash-base-at}
 * @see {@link lodash-at}
 * @see {@link underscore-contrib-nth}
 *
 * @example
 *
 *   const array = ['a', 'b', 'c', 'd']
 *
 *   nth(array, 1)
 *   //=> 'b'
 *
 *   nth(array, -2)
 *   //=> 'c'
 *
 */
function nth(list: any[] | string, offset?: number) {
  // isNill(array) return

  // @TODO from 0
  const index = offset < 0 ? list.length + offset : offset
  // return list[index]
  return isString(list) ? list.charAt(index) : list[index]
}

export default curry(2, nth)