/
slice.js
49 lines (41 loc) · 1007 Bytes
/
slice.js
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
/* Create slice of source array or array-like object.
*
* |Name |Desc |
* |----------------|--------------------------|
* |array |Array to slice |
* |start=0 |Start position |
* |end=array.length|End position, not included|
*/
/* example
* slice([1, 2, 3, 4], 1, 2); // -> [2]
*/
/* module
* env: all
*/
/* typescript
* export declare function slice(
* array: any[],
* start?: number,
* end?: number
* ): any[];
*/
exports = function(arr, start, end) {
const len = arr.length;
if (start == null) {
start = 0;
} else if (start < 0) {
start = Math.max(len + start, 0);
} else {
start = Math.min(start, len);
}
if (end == null) {
end = len;
} else if (end < 0) {
end = Math.max(len + end, 0);
} else {
end = Math.min(end, len);
}
const ret = [];
while (start < end) ret.push(arr[start++]);
return ret;
};