1 | import _curry2 from "./internal/_curry2.js";
|
---|
2 | import _isArray from "./internal/_isArray.js";
|
---|
3 | import _map from "./internal/_map.js";
|
---|
4 | import _assoc from "./internal/_assoc.js";
|
---|
5 | /**
|
---|
6 | *
|
---|
7 | * Deconstructs an array field from the input documents to output a document for each element.
|
---|
8 | * Each output document is the input document with the value of the array field replaced by the element.
|
---|
9 | *
|
---|
10 | * @func
|
---|
11 | * @memberOf R
|
---|
12 | * @since v0.28.0
|
---|
13 | * @category Object
|
---|
14 | * @sig String -> {k: [v]} -> [{k: v}]
|
---|
15 | * @param {String} key The key to determine which property of the object should be unwind
|
---|
16 | * @param {Object} object The object containing list under property named as key which is to unwind
|
---|
17 | * @return {List} A new list of object containing the value of input key having list replaced by each element in the object.
|
---|
18 | * @example
|
---|
19 | *
|
---|
20 | * R.unwind('hobbies', {
|
---|
21 | * name: 'alice',
|
---|
22 | * hobbies: ['Golf', 'Hacking'],
|
---|
23 | * colors: ['red', 'green'],
|
---|
24 | * });
|
---|
25 | * // [
|
---|
26 | * // { name: 'alice', hobbies: 'Golf', colors: ['red', 'green'] },
|
---|
27 | * // { name: 'alice', hobbies: 'Hacking', colors: ['red', 'green'] }
|
---|
28 | * // ]
|
---|
29 | */
|
---|
30 |
|
---|
31 | var unwind =
|
---|
32 | /*#__PURE__*/
|
---|
33 | _curry2(function (key, object) {
|
---|
34 | // If key is not in object or key is not as a list in object
|
---|
35 | if (!(key in object && _isArray(object[key]))) {
|
---|
36 | return [object];
|
---|
37 | } // Map over object[key] which is a list and assoc each element with key
|
---|
38 |
|
---|
39 |
|
---|
40 | return _map(function (item) {
|
---|
41 | return _assoc(key, item, object);
|
---|
42 | }, object[key]);
|
---|
43 | });
|
---|
44 |
|
---|
45 | export default unwind; |
---|