Implement toObject

This commit is contained in:
Infendro committed 2026-05-07 21:33:30 +02:00
1 parent a1bbf7cd1a
commit d4875a8ff0
3 files changed
+12 -1

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@infendro/collection",
"version": "0.0.1",
"version": "0.0.2",
"scripts": {
"build": "tsc"
},
+5
View File
@@ -78,10 +78,12 @@ import {unzip} from "./collection/unzip";
import {windowed} from "./collection/windowed";
import {withIndex} from "./collection/withIndex";
import {zip} from "./collection/zip";
import {Key, Object, toObject} from "./collection/toObject";
export type Collection<T> = {
[Symbol.iterator](): Iterator<T>
toObject<K extends Key, V>(this: Collection<[K, V]>): Object<K, V | undefined>
toArray(): T[]
toSet(): Set<T>
toMap<K, V>(this: Collection<[K, V]>): Map<K, V>
@@ -190,6 +192,9 @@ function wrap<T>(value: T[]): Collection<T> {
[Symbol.iterator]() {
return value[Symbol.iterator]();
},
toObject<K extends Key, V>() {
return toObject(value as [K, V][]);
},
toArray() {
return toArray(value);
},
+6
View File
@@ -0,0 +1,6 @@
export type Key = keyof any
export type Object<K extends Key, V> = { [key in K]: V }
export function toObject<K extends Key, V>(value: [K, V][]): Object<K, V | undefined> {
return Object.fromEntries(value) as Object<K, V | undefined>;
}