Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ES2019 Object.fromEntries function (#30934)
* add ES2019 Object.fromEntries function * add some comments * apply suggested changes * add readonly and general any
- Loading branch information
1 parent
1d83982
commit 49d6f61
Showing
9 changed files
with
192 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
/// <reference lib="es2018" /> | ||
/// <reference lib="es2019.array" /> | ||
/// <reference lib="es2019.object" /> | ||
/// <reference lib="es2019.string" /> | ||
/// <reference lib="es2019.symbol" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/// <reference lib="es2015.iterable" /> | ||
|
||
interface ObjectConstructor { | ||
/** | ||
* Returns an object created by key-value entries for properties and methods | ||
* @param entries An iterable object that contains key-value entries for properties and methods. | ||
*/ | ||
fromEntries<T = any>(entries: Iterable<readonly [PropertyKey, T]>): { [k in PropertyKey]: T }; | ||
|
||
/** | ||
* Returns an object created by key-value entries for properties and methods | ||
* @param entries An iterable object that contains key-value entries for properties and methods. | ||
*/ | ||
fromEntries(entries: Iterable<readonly any[]>): any; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//// [objectFromEntries.ts] | ||
const o = Object.fromEntries([['a', 1], ['b', 2], ['c', 3]]); | ||
const o2 = Object.fromEntries(new URLSearchParams()); | ||
const o3 = Object.fromEntries(new Map([[Symbol("key"), "value"]])); | ||
|
||
const frozenArray = Object.freeze([['a', 1], ['b', 2], ['c', 3]]); | ||
const o4 = Object.fromEntries(frozenArray); | ||
|
||
const frozenArray2: readonly [string, number][] = Object.freeze([['a', 1], ['b', 2], ['c', 3]]); | ||
const o5 = Object.fromEntries(frozenArray2); | ||
|
||
|
||
//// [objectFromEntries.js] | ||
const o = Object.fromEntries([['a', 1], ['b', 2], ['c', 3]]); | ||
const o2 = Object.fromEntries(new URLSearchParams()); | ||
const o3 = Object.fromEntries(new Map([[Symbol("key"), "value"]])); | ||
const frozenArray = Object.freeze([['a', 1], ['b', 2], ['c', 3]]); | ||
const o4 = Object.fromEntries(frozenArray); | ||
const frozenArray2 = Object.freeze([['a', 1], ['b', 2], ['c', 3]]); | ||
const o5 = Object.fromEntries(frozenArray2); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
=== tests/cases/compiler/objectFromEntries.ts === | ||
const o = Object.fromEntries([['a', 1], ['b', 2], ['c', 3]]); | ||
>o : Symbol(o, Decl(objectFromEntries.ts, 0, 5)) | ||
>Object.fromEntries : Symbol(ObjectConstructor.fromEntries, Decl(lib.es2019.object.d.ts, --, --), Decl(lib.es2019.object.d.ts, --, --)) | ||
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>fromEntries : Symbol(ObjectConstructor.fromEntries, Decl(lib.es2019.object.d.ts, --, --), Decl(lib.es2019.object.d.ts, --, --)) | ||
|
||
const o2 = Object.fromEntries(new URLSearchParams()); | ||
>o2 : Symbol(o2, Decl(objectFromEntries.ts, 1, 5)) | ||
>Object.fromEntries : Symbol(ObjectConstructor.fromEntries, Decl(lib.es2019.object.d.ts, --, --), Decl(lib.es2019.object.d.ts, --, --)) | ||
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>fromEntries : Symbol(ObjectConstructor.fromEntries, Decl(lib.es2019.object.d.ts, --, --), Decl(lib.es2019.object.d.ts, --, --)) | ||
>URLSearchParams : Symbol(URLSearchParams, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --), Decl(lib.dom.iterable.d.ts, --, --)) | ||
|
||
const o3 = Object.fromEntries(new Map([[Symbol("key"), "value"]])); | ||
>o3 : Symbol(o3, Decl(objectFromEntries.ts, 2, 5)) | ||
>Object.fromEntries : Symbol(ObjectConstructor.fromEntries, Decl(lib.es2019.object.d.ts, --, --), Decl(lib.es2019.object.d.ts, --, --)) | ||
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>fromEntries : Symbol(ObjectConstructor.fromEntries, Decl(lib.es2019.object.d.ts, --, --), Decl(lib.es2019.object.d.ts, --, --)) | ||
>Map : Symbol(Map, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --)) | ||
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --)) | ||
|
||
const frozenArray = Object.freeze([['a', 1], ['b', 2], ['c', 3]]); | ||
>frozenArray : Symbol(frozenArray, Decl(objectFromEntries.ts, 4, 5)) | ||
>Object.freeze : Symbol(ObjectConstructor.freeze, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>freeze : Symbol(ObjectConstructor.freeze, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
|
||
const o4 = Object.fromEntries(frozenArray); | ||
>o4 : Symbol(o4, Decl(objectFromEntries.ts, 5, 5)) | ||
>Object.fromEntries : Symbol(ObjectConstructor.fromEntries, Decl(lib.es2019.object.d.ts, --, --), Decl(lib.es2019.object.d.ts, --, --)) | ||
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>fromEntries : Symbol(ObjectConstructor.fromEntries, Decl(lib.es2019.object.d.ts, --, --), Decl(lib.es2019.object.d.ts, --, --)) | ||
>frozenArray : Symbol(frozenArray, Decl(objectFromEntries.ts, 4, 5)) | ||
|
||
const frozenArray2: readonly [string, number][] = Object.freeze([['a', 1], ['b', 2], ['c', 3]]); | ||
>frozenArray2 : Symbol(frozenArray2, Decl(objectFromEntries.ts, 7, 5)) | ||
>Object.freeze : Symbol(ObjectConstructor.freeze, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>freeze : Symbol(ObjectConstructor.freeze, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
|
||
const o5 = Object.fromEntries(frozenArray2); | ||
>o5 : Symbol(o5, Decl(objectFromEntries.ts, 8, 5)) | ||
>Object.fromEntries : Symbol(ObjectConstructor.fromEntries, Decl(lib.es2019.object.d.ts, --, --), Decl(lib.es2019.object.d.ts, --, --)) | ||
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>fromEntries : Symbol(ObjectConstructor.fromEntries, Decl(lib.es2019.object.d.ts, --, --), Decl(lib.es2019.object.d.ts, --, --)) | ||
>frozenArray2 : Symbol(frozenArray2, Decl(objectFromEntries.ts, 7, 5)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
=== tests/cases/compiler/objectFromEntries.ts === | ||
const o = Object.fromEntries([['a', 1], ['b', 2], ['c', 3]]); | ||
>o : { [x: string]: number; [x: number]: number; } | ||
>Object.fromEntries([['a', 1], ['b', 2], ['c', 3]]) : { [x: string]: number; [x: number]: number; } | ||
>Object.fromEntries : { <T = any>(entries: Iterable<readonly [string | number | symbol, T]>): { [x: string]: T; [x: number]: T; }; (entries: Iterable<readonly any[]>): any; } | ||
>Object : ObjectConstructor | ||
>fromEntries : { <T = any>(entries: Iterable<readonly [string | number | symbol, T]>): { [x: string]: T; [x: number]: T; }; (entries: Iterable<readonly any[]>): any; } | ||
>[['a', 1], ['b', 2], ['c', 3]] : [string, number][] | ||
>['a', 1] : [string, number] | ||
>'a' : "a" | ||
>1 : 1 | ||
>['b', 2] : [string, number] | ||
>'b' : "b" | ||
>2 : 2 | ||
>['c', 3] : [string, number] | ||
>'c' : "c" | ||
>3 : 3 | ||
|
||
const o2 = Object.fromEntries(new URLSearchParams()); | ||
>o2 : { [x: string]: string; [x: number]: string; } | ||
>Object.fromEntries(new URLSearchParams()) : { [x: string]: string; [x: number]: string; } | ||
>Object.fromEntries : { <T = any>(entries: Iterable<readonly [string | number | symbol, T]>): { [x: string]: T; [x: number]: T; }; (entries: Iterable<readonly any[]>): any; } | ||
>Object : ObjectConstructor | ||
>fromEntries : { <T = any>(entries: Iterable<readonly [string | number | symbol, T]>): { [x: string]: T; [x: number]: T; }; (entries: Iterable<readonly any[]>): any; } | ||
>new URLSearchParams() : URLSearchParams | ||
>URLSearchParams : { new (init?: string | URLSearchParams | string[][] | Record<string, string>): URLSearchParams; prototype: URLSearchParams; } | ||
|
||
const o3 = Object.fromEntries(new Map([[Symbol("key"), "value"]])); | ||
>o3 : { [x: string]: string; [x: number]: string; } | ||
>Object.fromEntries(new Map([[Symbol("key"), "value"]])) : { [x: string]: string; [x: number]: string; } | ||
>Object.fromEntries : { <T = any>(entries: Iterable<readonly [string | number | symbol, T]>): { [x: string]: T; [x: number]: T; }; (entries: Iterable<readonly any[]>): any; } | ||
>Object : ObjectConstructor | ||
>fromEntries : { <T = any>(entries: Iterable<readonly [string | number | symbol, T]>): { [x: string]: T; [x: number]: T; }; (entries: Iterable<readonly any[]>): any; } | ||
>new Map([[Symbol("key"), "value"]]) : Map<symbol, string> | ||
>Map : MapConstructor | ||
>[[Symbol("key"), "value"]] : [symbol, string][] | ||
>[Symbol("key"), "value"] : [symbol, string] | ||
>Symbol("key") : symbol | ||
>Symbol : SymbolConstructor | ||
>"key" : "key" | ||
>"value" : "value" | ||
|
||
const frozenArray = Object.freeze([['a', 1], ['b', 2], ['c', 3]]); | ||
>frozenArray : readonly (string | number)[][] | ||
>Object.freeze([['a', 1], ['b', 2], ['c', 3]]) : readonly (string | number)[][] | ||
>Object.freeze : { <T>(a: T[]): readonly T[]; <T extends Function>(f: T): T; <T>(o: T): Readonly<T>; } | ||
>Object : ObjectConstructor | ||
>freeze : { <T>(a: T[]): readonly T[]; <T extends Function>(f: T): T; <T>(o: T): Readonly<T>; } | ||
>[['a', 1], ['b', 2], ['c', 3]] : (string | number)[][] | ||
>['a', 1] : (string | number)[] | ||
>'a' : "a" | ||
>1 : 1 | ||
>['b', 2] : (string | number)[] | ||
>'b' : "b" | ||
>2 : 2 | ||
>['c', 3] : (string | number)[] | ||
>'c' : "c" | ||
>3 : 3 | ||
|
||
const o4 = Object.fromEntries(frozenArray); | ||
>o4 : any | ||
>Object.fromEntries(frozenArray) : any | ||
>Object.fromEntries : { <T = any>(entries: Iterable<readonly [string | number | symbol, T]>): { [x: string]: T; [x: number]: T; }; (entries: Iterable<readonly any[]>): any; } | ||
>Object : ObjectConstructor | ||
>fromEntries : { <T = any>(entries: Iterable<readonly [string | number | symbol, T]>): { [x: string]: T; [x: number]: T; }; (entries: Iterable<readonly any[]>): any; } | ||
>frozenArray : readonly (string | number)[][] | ||
|
||
const frozenArray2: readonly [string, number][] = Object.freeze([['a', 1], ['b', 2], ['c', 3]]); | ||
>frozenArray2 : readonly [string, number][] | ||
>Object.freeze([['a', 1], ['b', 2], ['c', 3]]) : readonly [string, number][] | ||
>Object.freeze : { <T>(a: T[]): readonly T[]; <T extends Function>(f: T): T; <T>(o: T): Readonly<T>; } | ||
>Object : ObjectConstructor | ||
>freeze : { <T>(a: T[]): readonly T[]; <T extends Function>(f: T): T; <T>(o: T): Readonly<T>; } | ||
>[['a', 1], ['b', 2], ['c', 3]] : [string, number][] | ||
>['a', 1] : [string, number] | ||
>'a' : "a" | ||
>1 : 1 | ||
>['b', 2] : [string, number] | ||
>'b' : "b" | ||
>2 : 2 | ||
>['c', 3] : [string, number] | ||
>'c' : "c" | ||
>3 : 3 | ||
|
||
const o5 = Object.fromEntries(frozenArray2); | ||
>o5 : { [x: string]: number; [x: number]: number; } | ||
>Object.fromEntries(frozenArray2) : { [x: string]: number; [x: number]: number; } | ||
>Object.fromEntries : { <T = any>(entries: Iterable<readonly [string | number | symbol, T]>): { [x: string]: T; [x: number]: T; }; (entries: Iterable<readonly any[]>): any; } | ||
>Object : ObjectConstructor | ||
>fromEntries : { <T = any>(entries: Iterable<readonly [string | number | symbol, T]>): { [x: string]: T; [x: number]: T; }; (entries: Iterable<readonly any[]>): any; } | ||
>frozenArray2 : readonly [string, number][] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// @target: es2019 | ||
|
||
const o = Object.fromEntries([['a', 1], ['b', 2], ['c', 3]]); | ||
const o2 = Object.fromEntries(new URLSearchParams()); | ||
const o3 = Object.fromEntries(new Map([[Symbol("key"), "value"]])); | ||
|
||
const frozenArray = Object.freeze([['a', 1], ['b', 2], ['c', 3]]); | ||
const o4 = Object.fromEntries(frozenArray); | ||
|
||
const frozenArray2: readonly [string, number][] = Object.freeze([['a', 1], ['b', 2], ['c', 3]]); | ||
const o5 = Object.fromEntries(frozenArray2); |