/
first.ts
64 lines (57 loc) · 1.45 KB
/
first.ts
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import type { IterableContainer } from "./_types";
import type { LazyEvaluator } from "./pipe";
import { purry } from "./purry";
type First<T extends IterableContainer> = T extends []
? undefined
: T extends readonly [unknown, ...Array<unknown>]
? T[0]
: T extends readonly [...infer Pre, infer Last]
? Last | Pre[0]
: T[0] | undefined;
/**
* Gets the first element of `array`.
*
* @param data - The array.
* @returns The first element of the array.
* @signature
* R.first(array)
* @example
* R.first([1, 2, 3]) // => 1
* R.first([]) // => undefined
* @dataFirst
* @pipeable
* @category Array
*/
export function first<T extends IterableContainer>(data: T): First<T>;
/**
* Gets the first element of `array`.
*
* @returns The first element of the array.
* @signature
* R.first()(array)
* @example
* R.pipe(
* [1, 2, 4, 8, 16],
* R.filter(x => x > 3),
* R.first(),
* x => x + 1
* ); // => 5
* @dataLast
* @pipeable
* @category Array
*/
export function first(): <T extends IterableContainer>(data: T) => First<T>;
export function first(): unknown {
return purry(_first, arguments, first.lazy);
}
function _first<T>([item]: ReadonlyArray<T>): T | undefined {
return item;
}
export namespace first {
export function lazy<T>(): LazyEvaluator<T> {
return (value) => ({ done: true, hasNext: true, next: value });
}
export namespace lazy {
export const single = true;
}
}