-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathslice.ts
80 lines (70 loc) · 2.18 KB
/
slice.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright (c) 2020 Jozty. All rights reserved. MIT license.
import curryN from './utils/curry_n.ts';
import type { InferType, PH } from './utils/types.ts';
import { isString } from './utils/is.ts';
// @types
type Slice_1<L extends ArrayLike<unknown> | string> = (
fromIndex: number,
) => InferType<L>;
type Slice_2<L extends ArrayLike<unknown> | string> = (
toIndex: number,
) => InferType<L>;
type Slice_3 = <L extends ArrayLike<unknown> | string>(list: L) => InferType<L>;
type Slice_2_3 =
& ((toIndex: number) => Slice_3)
& (<L extends ArrayLike<unknown> | string>(
toIndex: PH,
list: L,
) => Slice_2<L>)
& (<L extends ArrayLike<unknown> | string>(
toIndex: number,
list: L,
) => InferType<L>);
type Slice_1_3 =
& ((fromIndex: number) => Slice_3)
& (<L extends ArrayLike<unknown> | string>(
fromIndex: PH,
list: L,
) => Slice_1<L>)
& (<L extends ArrayLike<unknown> | string>(
fromIndex: number,
list: L,
) => InferType<L>);
type Slice_1_2<L extends ArrayLike<unknown> | string> =
& ((fromIndex: number) => Slice_2<L>)
& ((fromIndex: PH, toIndex: number) => Slice_1<L>)
& ((fromIndex: number, toIndex: number) => InferType<L>);
type Slice =
& ((fromIndex: number) => Slice_2_3)
& ((fromIndex: PH, toIndex: number) => Slice_1_3)
& (<L extends ArrayLike<unknown> | string>(
fromIndex: PH,
toIndex: PH,
list: L,
) => Slice_1_2<L>)
& ((fromIndex: number, toIndex: number) => Slice_3)
& (<L extends ArrayLike<unknown> | string>(
fromIndex: number,
toIndex: PH,
list: L,
) => Slice_2<L>)
& (<L extends ArrayLike<unknown> | string>(
fromIndex: PH,
toIndex: number,
list: L,
) => Slice_1<L>)
& (<L extends ArrayLike<unknown> | string>(
fromIndex: number,
toIndex: number,
list: L,
) => InferType<L>);
function _slice<L extends ArrayLike<unknown> | string>(
fromIndex: number,
toIndex: number,
list: L,
) {
if (isString(list)) return list.slice(fromIndex, toIndex);
return Array.prototype.slice.call(list, fromIndex, toIndex);
}
/** Returns the elements of the given list or string `fromIndex` (inclusive) to `toIndex` (exclusive). */
export const slice = curryN(3, _slice) as Slice;