mirror of
https://github.com/swc-project/swc.git
synced 2024-12-30 09:03:37 +03:00
39 lines
1020 B
TypeScript
39 lines
1020 B
TypeScript
|
// Loaded from https://deno.land/x/ramda@v0.27.2/source/nth.js
|
||
|
|
||
|
|
||
|
import _curry2 from './internal/_curry2.js';
|
||
|
import _isString from './internal/_isString.js';
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Returns the nth element of the given list or string. If n is negative the
|
||
|
* element at index length + n is returned.
|
||
|
*
|
||
|
* @func
|
||
|
* @memberOf R
|
||
|
* @since v0.1.0
|
||
|
* @category List
|
||
|
* @sig Number -> [a] -> a | Undefined
|
||
|
* @sig Number -> String -> String
|
||
|
* @param {Number} offset
|
||
|
* @param {*} list
|
||
|
* @return {*}
|
||
|
* @example
|
||
|
*
|
||
|
* const list = ['foo', 'bar', 'baz', 'quux'];
|
||
|
* R.nth(1, list); //=> 'bar'
|
||
|
* R.nth(-1, list); //=> 'quux'
|
||
|
* R.nth(-99, list); //=> undefined
|
||
|
*
|
||
|
* R.nth(2, 'abc'); //=> 'c'
|
||
|
* R.nth(3, 'abc'); //=> ''
|
||
|
* @symb R.nth(-1, [a, b, c]) = c
|
||
|
* @symb R.nth(0, [a, b, c]) = a
|
||
|
* @symb R.nth(1, [a, b, c]) = b
|
||
|
*/
|
||
|
var nth = _curry2(function nth(offset, list) {
|
||
|
var idx = offset < 0 ? list.length + offset : offset;
|
||
|
return _isString(list) ? list.charAt(idx) : list[idx];
|
||
|
});
|
||
|
export default nth;
|