From 6f7934054c21388b8f14ba07a558632f3455a2df Mon Sep 17 00:00:00 2001 From: Endel Dreyer Date: Tue, 26 Feb 2019 11:49:11 -0300 Subject: [PATCH 1/2] implement Array.prototype.findIndex. closes #456 --- src/LuaLib.ts | 1 + src/LuaTransformer.ts | 2 ++ src/lualib/ArrayFindIndex.ts | 17 +++++++++++++++++ test/unit/lualib/lualib.spec.ts | 17 +++++++++++++++++ 4 files changed, 37 insertions(+) create mode 100644 src/lualib/ArrayFindIndex.ts diff --git a/src/LuaLib.ts b/src/LuaLib.ts index 864d438f6..3db60196c 100644 --- a/src/LuaLib.ts +++ b/src/LuaLib.ts @@ -6,6 +6,7 @@ export enum LuaLibFeature { ArrayEvery = "ArrayEvery", ArrayFilter = "ArrayFilter", ArrayForEach = "ArrayForEach", + ArrayFindIndex = "ArrayFindIndex", ArrayIndexOf = "ArrayIndexOf", ArrayMap = "ArrayMap", ArrayPush = "ArrayPush", diff --git a/src/LuaTransformer.ts b/src/LuaTransformer.ts index 7d7420ca6..238289a07 100644 --- a/src/LuaTransformer.ts +++ b/src/LuaTransformer.ts @@ -3724,6 +3724,8 @@ export class LuaTransformer { ); case "forEach": return this.transformLuaLibFunction(LuaLibFeature.ArrayForEach, node, caller, ...params); + case "findIndex": + return this.transformLuaLibFunction(LuaLibFeature.ArrayFindIndex, node, caller, ...params); case "indexOf": return this.transformLuaLibFunction(LuaLibFeature.ArrayIndexOf, node, caller, ...params); case "map": diff --git a/src/lualib/ArrayFindIndex.ts b/src/lualib/ArrayFindIndex.ts new file mode 100644 index 000000000..82866c251 --- /dev/null +++ b/src/lualib/ArrayFindIndex.ts @@ -0,0 +1,17 @@ +function __TS__ArrayFindIndex( + arr: T[], + callbackFn: (this: void, element: T, index?: number, array?: T[]) => boolean +): number { + const len = arr.length; + if (len === 0) { + return -1; + } + + for (let i = 0; i < len; i++) { + if (callbackFn(arr[i], i, arr)) { + return i; + } + } + + return -1; +} diff --git a/test/unit/lualib/lualib.spec.ts b/test/unit/lualib/lualib.spec.ts index be87d1084..3c666c2dc 100644 --- a/test/unit/lualib/lualib.spec.ts +++ b/test/unit/lualib/lualib.spec.ts @@ -21,6 +21,23 @@ export class LuaLibTests Expect(result).toBe(JSON.stringify(expected)); } + @TestCase([0, 2, 4, 8], 10, -1) + @TestCase([0, 2, 4, 8], 4, 2) + @TestCase([0, 2, 4, 8], 8, 3) + @Test("findIndex") + public findIndex(inp: number[], searchEl: number, expected: number): void + { + const result = util.transpileAndExecute( + `let arrTest = ${JSON.stringify(inp)}; + return JSONStringify(arrTest.findIndex((elem, index) => { + return elem === ${searchEl}; + }));` + ); + + // Assert + Expect(result).toBe(expected); + } + @TestCase([], "x => x") @TestCase([0, 1, 2, 3], "x => x") @TestCase([0, 1, 2, 3], "x => x*2") From 858b8435ecd2886f6857e3fb9703e472c425fcf1 Mon Sep 17 00:00:00 2001 From: Endel Dreyer Date: Tue, 26 Feb 2019 12:23:24 -0300 Subject: [PATCH 2/2] refactor findIndex method, add more test cases --- src/lualib/ArrayFindIndex.ts | 8 +------- test/unit/lualib/lualib.spec.ts | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/lualib/ArrayFindIndex.ts b/src/lualib/ArrayFindIndex.ts index 82866c251..b60190c09 100644 --- a/src/lualib/ArrayFindIndex.ts +++ b/src/lualib/ArrayFindIndex.ts @@ -2,16 +2,10 @@ function __TS__ArrayFindIndex( arr: T[], callbackFn: (this: void, element: T, index?: number, array?: T[]) => boolean ): number { - const len = arr.length; - if (len === 0) { - return -1; - } - - for (let i = 0; i < len; i++) { + for (let i = 0, len = arr.length; i < len; i++) { if (callbackFn(arr[i], i, arr)) { return i; } } - return -1; } diff --git a/test/unit/lualib/lualib.spec.ts b/test/unit/lualib/lualib.spec.ts index 3c666c2dc..83b98aba2 100644 --- a/test/unit/lualib/lualib.spec.ts +++ b/test/unit/lualib/lualib.spec.ts @@ -21,11 +21,11 @@ export class LuaLibTests Expect(result).toBe(JSON.stringify(expected)); } + @TestCase([], 3, -1) @TestCase([0, 2, 4, 8], 10, -1) - @TestCase([0, 2, 4, 8], 4, 2) @TestCase([0, 2, 4, 8], 8, 3) - @Test("findIndex") - public findIndex(inp: number[], searchEl: number, expected: number): void + @Test("array.findIndex[value]") + public findIndexByValue(inp: number[], searchEl: number, expected: number): void { const result = util.transpileAndExecute( `let arrTest = ${JSON.stringify(inp)}; @@ -38,6 +38,22 @@ export class LuaLibTests Expect(result).toBe(expected); } + @TestCase([0, 2, 4, 8], 3, 8) + @TestCase([0, 2, 4, 8], 1, 2) + @Test("array.findIndex[index]") + public findIndexByIndex(inp: number[], expected: number, value: number): void + { + const result = util.transpileAndExecute( + `let arrTest = ${JSON.stringify(inp)}; + return JSONStringify(arrTest.findIndex((elem, index, arr) => { + return index === ${expected} && arr[${expected}] === ${value}; + }));` + ); + + // Assert + Expect(result).toBe(expected); + } + @TestCase([], "x => x") @TestCase([0, 1, 2, 3], "x => x") @TestCase([0, 1, 2, 3], "x => x*2")