forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassoc.js
37 lines (31 loc) · 1.2 KB
/
assoc.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
var assert = require('assert');
var R = require('..');
var eq = require('./shared/eq');
describe('assoc', function() {
it('makes a shallow clone of an object, overriding only the specified property', function() {
var obj1 = {a: 1, b: {c: 2, d: 3}, e: 4, f: 5};
var obj2 = R.assoc('e', {x: 42}, obj1);
eq(obj2, {a: 1, b: {c: 2, d: 3}, e: {x: 42}, f: 5});
// Note: reference equality below!
assert.strictEqual(obj2.a, obj1.a);
assert.strictEqual(obj2.b, obj1.b);
assert.strictEqual(obj2.f, obj1.f);
});
it('is the equivalent of clone and set if the property is not on the original', function() {
var obj1 = {a: 1, b: {c: 2, d: 3}, e: 4, f: 5};
var obj2 = R.assoc('z', {x: 42}, obj1);
eq(obj2, {a: 1, b: {c: 2, d: 3}, e: 4, f: 5, z: {x: 42}});
// Note: reference equality below!
assert.strictEqual(obj2.a, obj1.a);
assert.strictEqual(obj2.b, obj1.b);
assert.strictEqual(obj2.f, obj1.f);
});
it('is curried', function() {
var obj1 = {a: 1, b: {c: 2, d: 3}, e: 4, f: 5};
var expected = {a: 1, b: {c: 2, d: 3}, e: {x: 42}, f: 5};
var f = R.assoc('e');
var g = f({x: 42});
eq(f({x: 42}, obj1), expected);
eq(g(obj1), expected);
});
});