forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArraySplice.ts
More file actions
72 lines (58 loc) · 1.83 KB
/
ArraySplice.ts
File metadata and controls
72 lines (58 loc) · 1.83 KB
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
function __TS__ArraySplice<T>(this: void, list: T[], start: number, deleteCount: number, ...items: T[]): T[] {
const len = list.length;
let actualStart: number;
if (start < 0) {
actualStart = Math.max(len + start, 0);
} else {
actualStart = Math.min(start, len);
}
const itemCount = items.length;
let actualDeleteCount: number;
if (!start) {
actualDeleteCount = 0;
} else if (!deleteCount) {
actualDeleteCount = len - actualStart;
} else {
actualDeleteCount = Math.min(Math.max(deleteCount, 0), len - actualStart);
}
const out: T[] = [];
for (let k = 0; k < actualDeleteCount; k++) {
const from = actualStart + k;
if (list[from]) {
out[k] = list[from];
}
}
if (itemCount < actualDeleteCount) {
for (let k = actualStart; k < len - actualDeleteCount; k++) {
const from = k + actualDeleteCount;
const to = k + itemCount;
if (list[from]) {
list[to] = list[from];
} else {
list[to] = undefined;
}
}
for (let k = len; k > len - actualDeleteCount + itemCount; k--) {
list[k - 1] = undefined;
}
} else if (itemCount > actualDeleteCount) {
for (let k = len - actualDeleteCount; k > actualStart; k--) {
const from = k + actualDeleteCount - 1;
const to = k + itemCount - 1;
if (list[from]) {
list[to] = list[from];
} else {
list[to] = undefined;
}
}
}
let j = actualStart;
for (const e of items) {
list[j] = e;
j++;
}
for (let k = list.length - 1; k >= len - actualDeleteCount + itemCount; k--) {
list[k] = undefined;
}
return out;
}