Skip to content
This repository was archived by the owner on Aug 24, 2025. It is now read-only.

Commit f87f9b3

Browse files
committed
Allow toMap* to be used with arrays.
1 parent 8d66898 commit f87f9b3

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/lang/toMap.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { toMapBy } from "./toMapBy.js";
2+
13
/**
24
* Creates a map from the own entries of an object.
35
*
@@ -11,5 +13,10 @@
1113
* // => Map{"a": 1, "b": 4, "c": 5}
1214
*/
1315
export const toMap = <TValue>(
14-
object: Record<string, TValue>
15-
): Map<string, TValue> => new Map(Object.entries(object));
16+
object: Record<PropertyKey, TValue>
17+
): Map<string, TValue> =>
18+
toMapBy(
19+
object,
20+
(key) => key,
21+
(key, value) => value
22+
);

src/lang/toMapBy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* // => Map{{key: "a"}: 2, {key: "b"}: 8, {key: "a"}: 10}
1414
*/
1515
export const toMapBy = <TValue, UKey, VInitialValue>(
16-
object: Record<string, VInitialValue>,
16+
object: Record<PropertyKey, VInitialValue>,
1717
keyMapper: (key: string, val: VInitialValue) => UKey,
1818
valueMapper: (key: string, value: VInitialValue) => TValue
1919
): Map<UKey, TValue> =>

0 commit comments

Comments
 (0)