unwind.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. var unwind =
  31. /*#__PURE__*/
  32. _curry2(function (key, object) {
  33. // If key is not in object or key is not as a list in object
  34. if (!(key in object && _isArray(object[key]))) {
  35. return [object];
  36. } // Map over object[key] which is a list and assoc each element with key
  37. return _map(function (item) {
  38. return _assoc(key, item, object);
  39. }, object[key]);
  40. });
  41. export default unwind;