Skip to content

Commit 5bb7a0b

Browse files
endelPerryvw
authored andcommitted
implement Array.prototype.findIndex. closes #456 (#457)
* implement Array.prototype.findIndex. closes #456 * refactor findIndex method, add more test cases
1 parent 10002b5 commit 5bb7a0b

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

src/LuaLib.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export enum LuaLibFeature {
66
ArrayEvery = "ArrayEvery",
77
ArrayFilter = "ArrayFilter",
88
ArrayForEach = "ArrayForEach",
9+
ArrayFindIndex = "ArrayFindIndex",
910
ArrayIndexOf = "ArrayIndexOf",
1011
ArrayMap = "ArrayMap",
1112
ArrayPush = "ArrayPush",

src/LuaTransformer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3724,6 +3724,8 @@ export class LuaTransformer {
37243724
);
37253725
case "forEach":
37263726
return this.transformLuaLibFunction(LuaLibFeature.ArrayForEach, node, caller, ...params);
3727+
case "findIndex":
3728+
return this.transformLuaLibFunction(LuaLibFeature.ArrayFindIndex, node, caller, ...params);
37273729
case "indexOf":
37283730
return this.transformLuaLibFunction(LuaLibFeature.ArrayIndexOf, node, caller, ...params);
37293731
case "map":

src/lualib/ArrayFindIndex.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function __TS__ArrayFindIndex<T>(
2+
arr: T[],
3+
callbackFn: (this: void, element: T, index?: number, array?: T[]) => boolean
4+
): number {
5+
for (let i = 0, len = arr.length; i < len; i++) {
6+
if (callbackFn(arr[i], i, arr)) {
7+
return i;
8+
}
9+
}
10+
return -1;
11+
}

test/unit/lualib/lualib.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,39 @@ export class LuaLibTests
2121
Expect(result).toBe(JSON.stringify(expected));
2222
}
2323

24+
@TestCase([], 3, -1)
25+
@TestCase([0, 2, 4, 8], 10, -1)
26+
@TestCase([0, 2, 4, 8], 8, 3)
27+
@Test("array.findIndex[value]")
28+
public findIndexByValue(inp: number[], searchEl: number, expected: number): void
29+
{
30+
const result = util.transpileAndExecute(
31+
`let arrTest = ${JSON.stringify(inp)};
32+
return JSONStringify(arrTest.findIndex((elem, index) => {
33+
return elem === ${searchEl};
34+
}));`
35+
);
36+
37+
// Assert
38+
Expect(result).toBe(expected);
39+
}
40+
41+
@TestCase([0, 2, 4, 8], 3, 8)
42+
@TestCase([0, 2, 4, 8], 1, 2)
43+
@Test("array.findIndex[index]")
44+
public findIndexByIndex(inp: number[], expected: number, value: number): void
45+
{
46+
const result = util.transpileAndExecute(
47+
`let arrTest = ${JSON.stringify(inp)};
48+
return JSONStringify(arrTest.findIndex((elem, index, arr) => {
49+
return index === ${expected} && arr[${expected}] === ${value};
50+
}));`
51+
);
52+
53+
// Assert
54+
Expect(result).toBe(expected);
55+
}
56+
2457
@TestCase([], "x => x")
2558
@TestCase([0, 1, 2, 3], "x => x")
2659
@TestCase([0, 1, 2, 3], "x => x*2")

0 commit comments

Comments
 (0)