Skip to content

Commit afa4e5b

Browse files
committed
Fixed slice
1 parent deaf455 commit afa4e5b

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

src/lualib/ArraySlice.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// https://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf 22.1.3.23
2+
function __TS__ArraySlice<T>(list: T[], first: number, last: number): T[] {
3+
const len = list.length;
4+
5+
let k: number;
6+
if (first < 0) {
7+
k = Math.max(len + first, 0);
8+
} else {
9+
k = Math.min(first, len);
10+
}
11+
12+
let relativeEnd = last;
13+
if (last === undefined) {
14+
relativeEnd = len;
15+
}
16+
17+
let final: number;
18+
if (relativeEnd < 0) {
19+
final = Math.max(len + relativeEnd, 0);
20+
} else {
21+
final = Math.min(relativeEnd, len);
22+
}
23+
24+
const out = [];
25+
26+
let n = 0;
27+
while (k < final) {
28+
out[n] = list[k];
29+
k++;
30+
n++;
31+
}
32+
return out;
33+
}

src/lualib/ArraySplice.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// https://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
2-
function __TS__ArraySplice<T>(list: T[], start: number, deleteCount: number, ...items: T[]) {
2+
function __TS__ArraySplice<T>(list: T[], start: number, deleteCount: number, ...items: T[]): T[] {
33
// 1. 2.
44
const len = list.length;
55

test/unit/lualib.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { Expect, Test, TestCase, FocusTests } from "alsatian";
1+
import { Expect, Test, TestCase } from "alsatian";
22
import * as util from "../src/util";
33

4-
@FocusTests
54
export class LuaLibArrayTests {
65

76
@TestCase([0, 1, 2, 3], [1, 2, 3, 4])

0 commit comments

Comments
 (0)