source: node_modules/ramda/src/unwind.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

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