diff --git a/package.json b/package.json index 42bc6ca..4574d96 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@infendro/collection", - "version": "0.0.1", + "version": "0.0.2", "scripts": { "build": "tsc" }, diff --git a/src/collection.ts b/src/collection.ts index f0749cf..4156fe3 100644 --- a/src/collection.ts +++ b/src/collection.ts @@ -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 = { [Symbol.iterator](): Iterator + toObject(this: Collection<[K, V]>): Object toArray(): T[] toSet(): Set toMap(this: Collection<[K, V]>): Map @@ -190,6 +192,9 @@ function wrap(value: T[]): Collection { [Symbol.iterator]() { return value[Symbol.iterator](); }, + toObject() { + return toObject(value as [K, V][]); + }, toArray() { return toArray(value); }, diff --git a/src/collection/toObject.ts b/src/collection/toObject.ts new file mode 100644 index 0000000..c2b9304 --- /dev/null +++ b/src/collection/toObject.ts @@ -0,0 +1,6 @@ +export type Key = keyof any +export type Object = { [key in K]: V } + +export function toObject(value: [K, V][]): Object { + return Object.fromEntries(value) as Object; +}