Implement toObject

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

View File

@@ -1,6 +1,6 @@
{
"name": "@infendro/collection",
"version": "0.0.1",
"version": "0.0.2",
"scripts": {
"build": "tsc"
},

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);
},

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>;
}