[d24f17c] | 1 | import _curry2 from "./internal/_curry2.js";
|
---|
| 2 | import _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 |
|
---|
| 30 | var 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 |
|
---|
| 37 | export default nth; |
---|