source: node_modules/ramda/es/nth.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 968 bytes
Line 
1import _curry2 from "./internal/_curry2.js";
2import _isString from "./internal/_isString.js";
3/**
4 * Returns the nth element of the given list or string. If n is negative the
5 * element at index length + n is returned.
6 *
7 * @func
8 * @memberOf R
9 * @since v0.1.0
10 * @category List
11 * @sig Number -> [a] -> a | Undefined
12 * @sig Number -> String -> String
13 * @param {Number} offset
14 * @param {*} list
15 * @return {*}
16 * @example
17 *
18 * const list = ['foo', 'bar', 'baz', 'quux'];
19 * R.nth(1, list); //=> 'bar'
20 * R.nth(-1, list); //=> 'quux'
21 * R.nth(-99, list); //=> undefined
22 *
23 * R.nth(2, 'abc'); //=> 'c'
24 * R.nth(3, 'abc'); //=> ''
25 * @symb R.nth(-1, [a, b, c]) = c
26 * @symb R.nth(0, [a, b, c]) = a
27 * @symb R.nth(1, [a, b, c]) = b
28 */
29
30var nth =
31/*#__PURE__*/
32_curry2(function nth(offset, list) {
33 var idx = offset < 0 ? list.length + offset : offset;
34 return _isString(list) ? list.charAt(idx) : list[idx];
35});
36
37export default nth;
Note: See TracBrowser for help on using the repository browser.