Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/LuaLib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export enum LuaLibFeature {
ArrayForEach = "ArrayForEach",
ArrayFind = "ArrayFind",
ArrayFindIndex = "ArrayFindIndex",
ArrayIncludes = "ArrayIncludes",
ArrayIndexOf = "ArrayIndexOf",
ArrayMap = "ArrayMap",
ArrayPush = "ArrayPush",
Expand Down
21 changes: 21 additions & 0 deletions src/lualib/ArrayIncludes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// https://www.ecma-international.org/ecma-262/9.0/index.html#sec-array.prototype.includes
function __TS__ArrayIncludes<T>(this: T[], searchElement: T, fromIndex = 0): boolean {
const len = this.length;
let k = fromIndex;

if (fromIndex < 0) {
k = len + fromIndex;
}

if (k < 0) {
k = 0;
}

for (const i of forRange(k, len)) {
if (this[i] === searchElement) {
return true;
}
}

return false;
}
2 changes: 2 additions & 0 deletions src/transformation/builtins/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export function transformArrayPrototypeCall(
return transformLuaLibFunction(context, LuaLibFeature.ArrayFind, node, caller, ...params);
case "findIndex":
return transformLuaLibFunction(context, LuaLibFeature.ArrayFindIndex, node, caller, ...params);
case "includes":
return transformLuaLibFunction(context, LuaLibFeature.ArrayIncludes, node, caller, ...params);
case "indexOf":
return transformLuaLibFunction(context, LuaLibFeature.ArrayIndexOf, node, caller, ...params);
case "map":
Expand Down
31 changes: 31 additions & 0 deletions test/unit/builtins/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,37 @@ test.each([
`.expectToMatchJsResult();
});

test.each([
{ array: [1, 2, 3], includes: 2 },
{ array: [1, 2, 3, 2, 2], includes: 2 },
{ array: [1, 2, 3], includes: 4 },
{ array: ["a", "b", "c"], includes: "d" },
{ array: [[1], [2], [3]], includes: [2] },
{ array: [1, [2], 3], includes: 2 },
])("array.includes (%p)", ({ array, includes }) => {
util.testExpressionTemplate`${array}.includes(${includes})`.expectToMatchJsResult();
});

test("array.includes reference", () => {
util.testFunction`
const inst = [2];
return [[1], [3], inst].includes(inst);
`.expectToMatchJsResult();
});

test.each([
{ array: [1, 2, 3], includes: 2, fromIndex: 0 },
{ array: [1, 2, 3], includes: 2, fromIndex: 1 },
{ array: [1, 2, 3], includes: 2, fromIndex: 2 },
{ array: [1, 2, 3], includes: 2, fromIndex: 3 },
{ array: [1, 2, 3], includes: 2, fromIndex: -1 },
{ array: [1, 2, 3], includes: 2, fromIndex: -2 },
{ array: [1, 2, 3], includes: 2, fromIndex: -3 },
{ array: [1, 2, 3], includes: 2, fromIndex: -4 },
])("array.includes with fromIndex (%p)", ({ array, includes, fromIndex }) => {
util.testExpressionTemplate`${array}.includes(${includes}, ${fromIndex})`.expectToMatchJsResult();
});

test.each([
{ array: [] },
{ array: ["test1"] },
Expand Down