UNPKG

1.47 kBJavaScriptView Raw
1var _curry3 =
2/*#__PURE__*/
3require("./internal/_curry3");
4
5var _has =
6/*#__PURE__*/
7require("./internal/_has");
8/**
9 * Creates a new object with the own properties of the two provided objects. If
10 * a key exists in both objects, the provided function is applied to the key
11 * and the values associated with the key in each object, with the result being
12 * used as the value associated with the key in the returned object.
13 *
14 * @func
15 * @memberOf R
16 * @since v0.19.0
17 * @category Object
18 * @sig ((String, a, a) -> a) -> {a} -> {a} -> {a}
19 * @param {Function} fn
20 * @param {Object} l
21 * @param {Object} r
22 * @return {Object}
23 * @see R.mergeDeepWithKey, R.merge, R.mergeWith
24 * @example
25 *
26 * let concatValues = (k, l, r) => k == 'values' ? R.concat(l, r) : r
27 * R.mergeWithKey(concatValues,
28 * { a: true, thing: 'foo', values: [10, 20] },
29 * { b: true, thing: 'bar', values: [15, 35] });
30 * //=> { a: true, b: true, thing: 'bar', values: [10, 20, 15, 35] }
31 * @symb R.mergeWithKey(f, { x: 1, y: 2 }, { y: 5, z: 3 }) = { x: 1, y: f('y', 2, 5), z: 3 }
32 */
33
34
35var mergeWithKey =
36/*#__PURE__*/
37_curry3(function mergeWithKey(fn, l, r) {
38 var result = {};
39 var k;
40
41 for (k in l) {
42 if (_has(k, l)) {
43 result[k] = _has(k, r) ? fn(k, l[k], r[k]) : l[k];
44 }
45 }
46
47 for (k in r) {
48 if (_has(k, r) && !_has(k, result)) {
49 result[k] = r[k];
50 }
51 }
52
53 return result;
54});
55
56module.exports = mergeWithKey;
\No newline at end of file