mergeDeepWithKey.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import _curry3 from "./internal/_curry3.js";
  2. import _isObject from "./internal/_isObject.js";
  3. import mergeWithKey from "./mergeWithKey.js";
  4. /**
  5. * Creates a new object with the own properties of the two provided objects.
  6. * If a key exists in both objects:
  7. * - and both associated values are also objects then the values will be
  8. * recursively merged.
  9. * - otherwise the provided function is applied to the key and associated values
  10. * using the resulting value as the new value associated with the key.
  11. * If a key only exists in one object, the value will be associated with the key
  12. * of the resulting object.
  13. *
  14. * @func
  15. * @memberOf R
  16. * @since v0.24.0
  17. * @category Object
  18. * @sig ((String, a, a) -> a) -> {a} -> {a} -> {a}
  19. * @param {Function} fn
  20. * @param {Object} lObj
  21. * @param {Object} rObj
  22. * @return {Object}
  23. * @see R.mergeWithKey, R.mergeDeepWith
  24. * @example
  25. *
  26. * let concatValues = (k, l, r) => k == 'values' ? R.concat(l, r) : r
  27. * R.mergeDeepWithKey(concatValues,
  28. * { a: true, c: { thing: 'foo', values: [10, 20] }},
  29. * { b: true, c: { thing: 'bar', values: [15, 35] }});
  30. * //=> { a: true, b: true, c: { thing: 'bar', values: [10, 20, 15, 35] }}
  31. */
  32. var mergeDeepWithKey =
  33. /*#__PURE__*/
  34. _curry3(function mergeDeepWithKey(fn, lObj, rObj) {
  35. return mergeWithKey(function (k, lVal, rVal) {
  36. if (_isObject(lVal) && _isObject(rVal)) {
  37. return mergeDeepWithKey(fn, lVal, rVal);
  38. } else {
  39. return fn(k, lVal, rVal);
  40. }
  41. }, lObj, rObj);
  42. });
  43. export default mergeDeepWithKey;