Skip to content

Commit 0274929

Browse files
ark120202Perryvw
authored andcommitted
Add Object.fromEntries (#497)
1 parent 8fb72f4 commit 0274929

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

src/LuaLib.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export enum LuaLibFeature {
3131
NewIndex = "NewIndex",
3232
ObjectAssign = "ObjectAssign",
3333
ObjectEntries = "ObjectEntries",
34+
ObjectFromEntries = "ObjectFromEntries",
3435
ObjectKeys = "ObjectKeys",
3536
ObjectValues = "ObjectValues",
3637
Set = "Set",
@@ -47,6 +48,7 @@ const luaLibDependencies: {[lib in LuaLibFeature]?: LuaLibFeature[]} = {
4748
ArrayFlat: [LuaLibFeature.ArrayConcat],
4849
ArrayFlatMap: [LuaLibFeature.ArrayConcat],
4950
Iterator: [LuaLibFeature.Symbol],
51+
ObjectFromEntries: [LuaLibFeature.Iterator, LuaLibFeature.Symbol],
5052
Map: [LuaLibFeature.InstanceOf, LuaLibFeature.Iterator, LuaLibFeature.Symbol],
5153
Set: [LuaLibFeature.InstanceOf, LuaLibFeature.Iterator, LuaLibFeature.Symbol],
5254
WeakMap: [LuaLibFeature.InstanceOf, LuaLibFeature.Iterator, LuaLibFeature.Symbol],

src/LuaTransformer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3707,6 +3707,8 @@ export class LuaTransformer {
37073707
return this.transformLuaLibFunction(LuaLibFeature.ObjectAssign, expression, ...parameters);
37083708
case "entries":
37093709
return this.transformLuaLibFunction(LuaLibFeature.ObjectEntries, expression, ...parameters);
3710+
case "fromEntries":
3711+
return this.transformLuaLibFunction(LuaLibFeature.ObjectFromEntries, expression, ...parameters);
37103712
case "keys":
37113713
return this.transformLuaLibFunction(LuaLibFeature.ObjectKeys, expression, ...parameters);
37123714
case "values":

src/lualib/ObjectFromEntries.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function __TS__ObjectFromEntries<T>(
2+
this: void,
3+
entries: ReadonlyArray<[string, T]> | Iterable<[string, T]>
4+
): Record<string, T> {
5+
const obj: Record<string, T> = {};
6+
7+
const iterable = entries as Iterable<[string, T]>;
8+
if (iterable[Symbol.iterator]) {
9+
const iterator = iterable[Symbol.iterator]();
10+
while (true) {
11+
const result = iterator.next();
12+
if (result.done) break;
13+
14+
const value: [string, T] = result.value;
15+
obj[value[0]] = value[1];
16+
}
17+
} else {
18+
for (const entry of entries as ReadonlyArray<[string, T]>) {
19+
obj[entry[0]] = entry[1];
20+
}
21+
}
22+
23+
return obj;
24+
}

test/unit/lualib/lualib.spec.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ test.each([
441441
{ initial: "{}", parameters: "{a: 3}", expected: { a: 3 } },
442442
{ initial: "{a: 3}", parameters: "{a: 5}", expected: { a: 5 } },
443443
{ initial: "{a: 3}", parameters: "{b: 5},{c: 7}", expected: { a: 3, b: 5, c: 7 } },
444-
])("Object.Assign (%p)", ({ initial, parameters, expected }) => {
444+
])("Object.assign (%p)", ({ initial, parameters, expected }) => {
445445
const jsonResult = util.transpileAndExecute(`
446446
return JSONStringify(Object.assign(${initial},${parameters}));
447447
`);
@@ -517,3 +517,40 @@ test.each([
517517
}
518518
}
519519
});
520+
521+
// https://github.com/Microsoft/TypeScript/pull/26149
522+
const objectFromEntriesDeclaration = `
523+
interface ObjectConstructor {
524+
fromEntries<T>(entries: ReadonlyArray<[string, T]> | Iterable<[string, T]>): Record<string, T>;
525+
fromEntries(entries: ReadonlyArray<[string, any]> | Iterable<[string, any]>): Record<string, any>;
526+
}
527+
`;
528+
529+
test.each([
530+
{ entries: [], expected: [] },
531+
{ entries: [["a", 1], ["b", 2]], expected: { a: 1, b: 2 } },
532+
{ entries: [["a", 1], ["a", 2]], expected: { a: 2 } },
533+
])("Object.fromEntries (%p)", ({ entries, expected }) => {
534+
const result = util.transpileAndExecute(
535+
`const obj = Object.fromEntries(${JSON.stringify(entries)});
536+
return JSONStringify(obj);`,
537+
undefined,
538+
undefined,
539+
objectFromEntriesDeclaration,
540+
);
541+
542+
expect(JSON.parse(result)).toEqual(expected);
543+
});
544+
545+
test("Object.fromEntries (Map)", () => {
546+
const result = util.transpileAndExecute(
547+
`const map = new Map([["foo", "bar"]]);
548+
const obj = Object.fromEntries(map);
549+
return JSONStringify(obj);`,
550+
undefined,
551+
undefined,
552+
objectFromEntriesDeclaration,
553+
);
554+
555+
expect(JSON.parse(result)).toEqual({ foo: "bar" });
556+
});

0 commit comments

Comments
 (0)