forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergeDeepWithKey.js
41 lines (33 loc) · 1.44 KB
/
mergeDeepWithKey.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
var assert = require('assert');
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
describe('mergeDeepWithKey', function() {
function last(k, x, y) { return y; }
it('takes two objects, recursively merges their own properties and returns a new object', function() {
var a = { w: 1, x: 2, y: { z: 3 }};
var b = { a: 4, b: 5, c: { d: 6 }};
eq(R.mergeDeepWithKey(last, a, b), { w: 1, x: 2, y: { z: 3 }, a: 4, b: 5, c: { d: 6 }});
});
it('applies the provided function to the key, the value from the first object' +
' and the value from the second object to determine the value for keys that' +
' exist in both objects', function() {
var a = { a: { b: 'B1', c: 'C' }, y: 'Y' };
var b = { a: { b: 'B2', d: 'D' }, z: 'Z' };
var c = R.mergeDeepWithKey(function(k, a, b) { return k + a + b; }, a, b);
eq(c, { a: { b: 'bB1B2', c: 'C', d: 'D' }, y: 'Y', z: 'Z' });
});
it('is not destructive', function() {
var a = { w: 1, x: { y: 2 }};
var res = R.mergeDeepWithKey(last, a, { x: { y: 3 }});
assert.notStrictEqual(a, res);
assert.notStrictEqual(a.x, res.x);
eq(res, { w: 1, x: { y: 3 }});
});
it('reports only own properties', function() {
var a = { w: 1, x: { y: 2 }};
function Cla() {}
Cla.prototype.y = 5;
eq(R.mergeDeepWithKey(last, { x: new Cla() }, a), { w: 1, x: { y: 2 }});
eq(R.mergeDeepWithKey(last, a, { x: new Cla() }), { w: 1, x: { y: 2 }});
});
});