Skip to content

Commit ffde390

Browse files
authored
Implement array.entries() (#1031)
* Implement array.entries * Added extra test for indirect use of array.entries() * Added destructuring test for array.entries * Fix prettier
1 parent bd17ed7 commit ffde390

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

src/LuaLib.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { EmitHost } from "./transpilation";
33

44
export enum LuaLibFeature {
55
ArrayConcat = "ArrayConcat",
6+
ArrayEntries = "ArrayEntries",
67
ArrayEvery = "ArrayEvery",
78
ArrayFilter = "ArrayFilter",
89
ArrayForEach = "ArrayForEach",

src/lualib/ArrayEntries.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// https://262.ecma-international.org/10.0/#sec-array.prototype.entries
2+
function __TS__ArrayEntries<T>(this: void, array: T[]): IterableIterator<[number, T]> {
3+
let key = 0;
4+
return {
5+
[Symbol.iterator](): IterableIterator<[number, T]> {
6+
return this;
7+
},
8+
next(): IteratorResult<[number, T]> {
9+
const result = { done: array[key] === undefined, value: [key, array[key]] as [number, T] };
10+
key++;
11+
return result;
12+
},
13+
};
14+
}

src/transformation/builtins/array.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ export function transformArrayPrototypeCall(
3636
switch (expressionName) {
3737
case "concat":
3838
return transformLuaLibFunction(context, LuaLibFeature.ArrayConcat, node, caller, ...params);
39+
case "entries":
40+
return transformLuaLibFunction(context, LuaLibFeature.ArrayEntries, node, caller);
3941
case "push":
4042
return transformLuaLibFunction(context, LuaLibFeature.ArrayPush, node, caller, ...params);
4143
case "reverse":

test/unit/builtins/array.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,35 @@ describe.each(["reduce", "reduceRight"])("array.%s", reduce => {
594594
});
595595
});
596596

597+
test.each([{ array: [] }, { array: ["a", "b", "c"] }, { array: [{ foo: "foo" }, { bar: "bar" }] }])(
598+
"array.entries (%p)",
599+
({ array }) => {
600+
util.testFunction`
601+
const array = ${util.formatCode(array)};
602+
const result = [];
603+
for (const [i, v] of array.entries()) {
604+
result.push([i, v]);
605+
}
606+
return result;
607+
`.expectToMatchJsResult();
608+
}
609+
);
610+
611+
test("array.entries indirect use", () => {
612+
util.testFunction`
613+
const entries = ["a", "b", "c"].entries();
614+
const result = [];
615+
for (const [i, v] of entries) {
616+
result.push([i, v]);
617+
}
618+
return result;
619+
`.expectToMatchJsResult();
620+
});
621+
622+
test("array.entries destructured", () => {
623+
util.testExpression`[...["a", "b", "c"].entries()]`.expectToMatchJsResult();
624+
});
625+
597626
const genericChecks = [
598627
"function generic<T extends number[]>(array: T)",
599628
"function generic<T extends [...number[]]>(array: T)",

0 commit comments

Comments
 (0)