forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArraySlice.ts
More file actions
35 lines (29 loc) · 791 Bytes
/
ArraySlice.ts
File metadata and controls
35 lines (29 loc) · 791 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
26
27
28
29
30
31
32
33
34
35
// https://www.ecma-international.org/ecma-262/9.0/index.html#sec-array.prototype.slice
function __TS__ArraySlice<T>(this: void, list: T[], first: number, last: number): T[] {
const len = list.length;
const relativeStart = first || 0;
let k: number;
if (relativeStart < 0) {
k = Math.max(len + relativeStart, 0);
} else {
k = Math.min(relativeStart, len);
}
let relativeEnd = last;
if (last === undefined) {
relativeEnd = len;
}
let final: number;
if (relativeEnd < 0) {
final = Math.max(len + relativeEnd, 0);
} else {
final = Math.min(relativeEnd, len);
}
const out = [];
let n = 0;
while (k < final) {
out[n] = list[k];
k++;
n++;
}
return out;
}