forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodifyPath.js
52 lines (44 loc) · 2.15 KB
/
modifyPath.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
42
43
44
45
46
47
48
49
50
51
52
var assert = require('assert');
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
describe('modifyPath', function() {
it('creates a new object by applying the `transformation` function to the given `properties` of the `object`', function() {
var object = {a: 'Tomato', b: { c: { d: [100, 101, 102] } }, e: { f: 'g', h: [1, 2, 3] }};
var expected = {a: 'Tomato', b: { c: { d: [100, 102, 102] } }, e: { f: 'g', h: [1, 2, 3] }};
var created = R.modifyPath(['b', 'c', 'd', 1], R.add(1), object);
eq(created, expected);
// Note: reference equality below!
assert.strictEqual(object.a, created.a);
assert.strictEqual(object.e, created.e);
assert.strictEqual(object.e.h, created.e.h);
});
it('returns the original object if object does not contain the key', function() {
var object = {a: 'Tomato', b: { c: { d: [100, 101, 102] } }, e: { f: 'g', h: [1, 2, 3] }};
var created = R.modifyPath(['b', 'nonexistent', 'd', 1], R.add(1), object);
eq(created, object);
// Note: reference equality below!
assert.strictEqual(object, created);
});
it('modifies the original object if path is empty array', function() {
var object = {a: 'Tomato', b: { c: { d: [100, 101, 102] } }, e: { f: 'g', h: [1, 2, 3] }};
var expected = {a: 'Tomato', b: 'Potato', e: { f: 'g', h: [1, 2, 3] }};
var created = R.modifyPath([], R.assoc('b', 'Potato'), object);
eq(created, expected);
});
it('is not destructive', function() {
var object = {a: 'Tomato', b: { c: { d: [100, 101, 102] } }, e: { f: 'g', h: [1, 2, 3] }};
var expected = {a: 'Tomato', b: { c: { d: [100, 101, 102] } }, e: { f: 'g', h: [1, 2, 3] }};
R.modifyPath(['b', 'c', 'd', 1], R.add(1), object);
eq(object, expected);
});
it('throws error for non-function transformations', function() {
var object = {a: 'Tomato', b: { c: { d: [100, 101, 102] } }, e: { f: 'g', h: [1, 2, 3] }};
assert.throws(
function() { R.modifyPath(['b', 'c', 'd', 1], 2, object) ;},
function(err) {
return err.constructor === TypeError &&
err.message === 'fn is not a function';
}
);
});
});