Skip to content

Commit 9fb5a05

Browse files
committed
Make toSortedObjectFromEntries accept iterables
1 parent 6142e70 commit 9fb5a05

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

registry/lib/objects.d.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ declare const Objects: {
3535
getterDefObj: GetterDefObj | undefined,
3636
stats?: LazyGetterStats | undefined
3737
) => object
38-
getOwnPropertyValues<T>(obj: { [key: string]: T } | null | undefined): T[]
39-
hasKeys(obj: any): obj is Record<string, any>
38+
getOwnPropertyValues<T>(
39+
obj: { [key: PropertyKey]: T } | null | undefined
40+
): T[]
41+
hasKeys(obj: any): obj is Record<PropertyKey, any>
4042
hasOwn(
4143
obj: any,
4244
propKey: PropertyKey
@@ -45,11 +47,11 @@ declare const Objects: {
4547
isObjectObject(value: any): value is { [key: PropertyKey]: any }
4648
merge<T extends object, U extends object>(target: T, source: U): T & U
4749
objectEntries: typeof objectEntries
48-
toSortedObject<T>(obj: { [key: string | symbol]: T }): {
49-
[key: string | symbol]: T
50+
toSortedObject<T>(obj: { [key: PropertyKey]: T }): {
51+
[key: PropertyKey]: T
5052
}
51-
toSortedObjectFromEntries<T>(entries: Array<[string | symbol, T]>): {
52-
[key: string]: T
53+
toSortedObjectFromEntries<T>(entries: Iterable<[PropertyKey, T]>): {
54+
[key: PropertyKey]: T
5355
}
5456
}
5557
declare namespace Objects {

registry/lib/objects.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,20 @@ function defineLazyGetters(object, getterDefObj, stats) {
121121
return object
122122
}
123123

124+
let _localeCompare
125+
/*@__NO_SIDE_EFFECTS__*/
126+
function entryKeyComparator(a, b) {
127+
if (_localeCompare === undefined) {
128+
const sorts = /*@__PURE__*/ require('./sorts')
129+
_localeCompare = sorts.localeCompare
130+
}
131+
const keyA = a[0]
132+
const keyB = b[0]
133+
const strKeyA = typeof keyA === 'string' ? keyA : String(keyA)
134+
const strKeyB = typeof keyB === 'string' ? keyB : String(keyB)
135+
return _localeCompare(strKeyA, strKeyB)
136+
}
137+
124138
/*@__NO_SIDE_EFFECTS__*/
125139
function getOwnPropertyValues(obj) {
126140
if (obj === null || obj === undefined) {
@@ -251,25 +265,23 @@ function toSortedObject(obj) {
251265

252266
/*@__NO_SIDE_EFFECTS__*/
253267
function toSortedObjectFromEntries(entries) {
254-
const { length } = entries
255-
if (!length) {
256-
return {}
257-
}
258-
const stringEntries = []
268+
const otherEntries = []
259269
const symbolEntries = []
260-
for (let i = 0; i < length; i += 1) {
261-
const entry = entries[i]
270+
// Use for-of to work with entries iterators.
271+
for (const entry of entries) {
262272
if (typeof entry[0] === 'symbol') {
263273
symbolEntries.push(entry)
264274
} else {
265-
stringEntries.push(entry)
275+
otherEntries.push(entry)
266276
}
267277
}
268-
const { localeCompare } = /*@__PURE__*/ require('./sorts')
278+
if (!otherEntries.length && !symbolEntries.length) {
279+
return []
280+
}
269281
return ObjectFromEntries([
270282
// The String constructor is safe to use with symbols.
271-
...symbolEntries.sort((a, b) => localeCompare(String(a[0]), String(b[0]))),
272-
...stringEntries.sort((a, b) => localeCompare(a[0], b[0]))
283+
...symbolEntries.sort(entryKeyComparator),
284+
...otherEntries.sort(entryKeyComparator)
273285
])
274286
}
275287

0 commit comments

Comments
 (0)