Skip to content

Commit ea07f80

Browse files
authored
Feature/array includes (#796)
* Added tests * Implemented array.includes * PR feedback
1 parent 17bb60f commit ea07f80

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

src/LuaLib.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export enum LuaLibFeature {
88
ArrayForEach = "ArrayForEach",
99
ArrayFind = "ArrayFind",
1010
ArrayFindIndex = "ArrayFindIndex",
11+
ArrayIncludes = "ArrayIncludes",
1112
ArrayIndexOf = "ArrayIndexOf",
1213
ArrayMap = "ArrayMap",
1314
ArrayPush = "ArrayPush",

src/lualib/ArrayIncludes.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// https://www.ecma-international.org/ecma-262/9.0/index.html#sec-array.prototype.includes
2+
function __TS__ArrayIncludes<T>(this: T[], searchElement: T, fromIndex = 0): boolean {
3+
const len = this.length;
4+
let k = fromIndex;
5+
6+
if (fromIndex < 0) {
7+
k = len + fromIndex;
8+
}
9+
10+
if (k < 0) {
11+
k = 0;
12+
}
13+
14+
for (const i of forRange(k, len)) {
15+
if (this[i] === searchElement) {
16+
return true;
17+
}
18+
}
19+
20+
return false;
21+
}

src/transformation/builtins/array.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export function transformArrayPrototypeCall(
4040
return transformLuaLibFunction(context, LuaLibFeature.ArrayFind, node, caller, ...params);
4141
case "findIndex":
4242
return transformLuaLibFunction(context, LuaLibFeature.ArrayFindIndex, node, caller, ...params);
43+
case "includes":
44+
return transformLuaLibFunction(context, LuaLibFeature.ArrayIncludes, node, caller, ...params);
4345
case "indexOf":
4446
return transformLuaLibFunction(context, LuaLibFeature.ArrayIndexOf, node, caller, ...params);
4547
case "map":

test/unit/builtins/array.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,37 @@ test.each([
389389
`.expectToMatchJsResult();
390390
});
391391

392+
test.each([
393+
{ array: [1, 2, 3], includes: 2 },
394+
{ array: [1, 2, 3, 2, 2], includes: 2 },
395+
{ array: [1, 2, 3], includes: 4 },
396+
{ array: ["a", "b", "c"], includes: "d" },
397+
{ array: [[1], [2], [3]], includes: [2] },
398+
{ array: [1, [2], 3], includes: 2 },
399+
])("array.includes (%p)", ({ array, includes }) => {
400+
util.testExpressionTemplate`${array}.includes(${includes})`.expectToMatchJsResult();
401+
});
402+
403+
test("array.includes reference", () => {
404+
util.testFunction`
405+
const inst = [2];
406+
return [[1], [3], inst].includes(inst);
407+
`.expectToMatchJsResult();
408+
});
409+
410+
test.each([
411+
{ array: [1, 2, 3], includes: 2, fromIndex: 0 },
412+
{ array: [1, 2, 3], includes: 2, fromIndex: 1 },
413+
{ array: [1, 2, 3], includes: 2, fromIndex: 2 },
414+
{ array: [1, 2, 3], includes: 2, fromIndex: 3 },
415+
{ array: [1, 2, 3], includes: 2, fromIndex: -1 },
416+
{ array: [1, 2, 3], includes: 2, fromIndex: -2 },
417+
{ array: [1, 2, 3], includes: 2, fromIndex: -3 },
418+
{ array: [1, 2, 3], includes: 2, fromIndex: -4 },
419+
])("array.includes with fromIndex (%p)", ({ array, includes, fromIndex }) => {
420+
util.testExpressionTemplate`${array}.includes(${includes}, ${fromIndex})`.expectToMatchJsResult();
421+
});
422+
392423
test.each([
393424
{ array: [] },
394425
{ array: ["test1"] },

0 commit comments

Comments
 (0)