forked from Tinkoff/utils.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnth.ts
More file actions
25 lines (23 loc) · 617 Bytes
/
nth.ts
File metadata and controls
25 lines (23 loc) · 617 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { nth } from '../typings/types';
import curryN from '../function/curryN';
/**
* Returns the nth element of the given array. If n is negative the
* element at index length + n is returned.
*
* @param {Number} index
* @param {*} arr
* @return {*}
* @example
*
* var list = ['foo', 'bar', 'baz', 'quux'];
* nth(1, list); //=> 'bar'
* nth(2, list); //=> 'baz'
* nth(-1, list); //=> 'quux'
* nth(-99, list); //=> undefined
*/
export default curryN(2, (index = 0, arr = []) => {
if (index < 0) {
index += arr.length;
}
return arr[index];
}) as typeof nth