Implement toObject
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@infendro/collection",
|
"name": "@infendro/collection",
|
||||||
"version": "0.0.1",
|
"version": "0.0.2",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc"
|
"build": "tsc"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -78,10 +78,12 @@ import {unzip} from "./collection/unzip";
|
|||||||
import {windowed} from "./collection/windowed";
|
import {windowed} from "./collection/windowed";
|
||||||
import {withIndex} from "./collection/withIndex";
|
import {withIndex} from "./collection/withIndex";
|
||||||
import {zip} from "./collection/zip";
|
import {zip} from "./collection/zip";
|
||||||
|
import {Key, Object, toObject} from "./collection/toObject";
|
||||||
|
|
||||||
export type Collection<T> = {
|
export type Collection<T> = {
|
||||||
[Symbol.iterator](): Iterator<T>
|
[Symbol.iterator](): Iterator<T>
|
||||||
|
|
||||||
|
toObject<K extends Key, V>(this: Collection<[K, V]>): Object<K, V | undefined>
|
||||||
toArray(): T[]
|
toArray(): T[]
|
||||||
toSet(): Set<T>
|
toSet(): Set<T>
|
||||||
toMap<K, V>(this: Collection<[K, V]>): Map<K, V>
|
toMap<K, V>(this: Collection<[K, V]>): Map<K, V>
|
||||||
@@ -190,6 +192,9 @@ function wrap<T>(value: T[]): Collection<T> {
|
|||||||
[Symbol.iterator]() {
|
[Symbol.iterator]() {
|
||||||
return value[Symbol.iterator]();
|
return value[Symbol.iterator]();
|
||||||
},
|
},
|
||||||
|
toObject<K extends Key, V>() {
|
||||||
|
return toObject(value as [K, V][]);
|
||||||
|
},
|
||||||
toArray() {
|
toArray() {
|
||||||
return toArray(value);
|
return toArray(value);
|
||||||
},
|
},
|
||||||
|
|||||||
6
src/collection/toObject.ts
Normal file
6
src/collection/toObject.ts
Normal 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>;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user