-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
unwind.js
43 lines (40 loc) · 1.44 KB
/
unwind.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
37
38
39
40
41
42
43
import _curry2 from './internal/_curry2.js';
import _isArray from './internal/_isArray.js';
import _map from './internal/_map.js';
import _assoc from './internal/_assoc.js';
/**
*
* Deconstructs an array field from the input documents to output a document for each element.
* Each output document is the input document with the value of the array field replaced by the element.
*
* @func
* @memberOf R
* @since v0.28.0
* @category Object
* @sig String -> {k: [v]} -> [{k: v}]
* @param {String} key The key to determine which property of the object should be unwind
* @param {Object} object The object containing list under property named as key which is to unwind
* @return {List} A new list of object containing the value of input key having list replaced by each element in the object.
* @example
*
* R.unwind('hobbies', {
* name: 'alice',
* hobbies: ['Golf', 'Hacking'],
* colors: ['red', 'green'],
* });
* // [
* // { name: 'alice', hobbies: 'Golf', colors: ['red', 'green'] },
* // { name: 'alice', hobbies: 'Hacking', colors: ['red', 'green'] }
* // ]
*/
var unwind = _curry2(function(key, object) {
// If key is not in object or key is not as a list in object
if (!(key in object && _isArray(object[key]))) {
return [object];
}
// Map over object[key] which is a list and assoc each element with key
return _map(function(item) {
return _assoc(key, item, object);
}, object[key]);
});
export default unwind;