import { length, map } from '@typed/list' import { Stream } from '@motorcycle/types' import { combineArray } from './combineArray' /** * Takes an object of streams and returns a Stream of an object. * * @name combineObj(obj: { [K in keyof Obj]: Stream }): Stream * @example * import { combineObj, now } from '@motorcycle/stream' * * const obj = { a: now(1), b: now(2), c: now(3) } * * const stream: Stream<{ a: number, b: number, c: number }> = combineObj(obj) */ export function combineObj( object: { readonly [K in keyof Obj]: Stream } ): Stream { const objectKeys = Object.keys(object) as Array const sources = map(key => object[key], objectKeys) return combineArray((...values: Array) => { const valuesMap = {} as Obj for (let i = 0; i < length(values); ++i) valuesMap[objectKeys[i]] = values[i] return valuesMap }, sources) }