-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathobjectFlatMap.js
36 lines (34 loc) · 1.1 KB
/
objectFlatMap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const isPromise = require('./isPromise')
const objectMap = require('./objectMap')
const objectFlatten = require('./objectFlatten')
/**
* @name objectFlatMap
*
* @synopsis
* ```coffeescript [specscript]
* Stream<T> = { read: ()=>T, write: T=>() }
* Monad<T> = Array<T>|String<T>|Set<T>
* |TypedArray<T>|Stream<T>|Iterator<Promise|T>
* |{ chain: T=>Monad<T> }|{ flatMap: T=>Monad<T> }|Object<T>
* Reducer<T> = (any, T)=>Promise|any
* Foldable<T> = Iterable<T>|AsyncIterable<T>|{ reduce: Reducer<T> }|Object<T>
*
* objectFlatMap<
* T any,
* object Object<T>,
* flatMapper T=>Promise|Monad<T>|Foldable<T>|T,
* >(object, flatMapper) -> Promise|Object<T>
* ```
*
* @description
* Apply a flatMapper to each value of an object, assigning all items of all results into a new object.
*
* @TODO "deeply copies" after objectFlatten changes to deep assignment
*/
const objectFlatMap = function (object, flatMapper) {
const monadObject = objectMap(object, flatMapper)
return isPromise(monadObject)
? monadObject.then(objectFlatten)
: objectFlatten(monadObject)
}
module.exports = objectFlatMap