object.builders.js
3.3 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
$(document).ready(function() {
module("underscore.object.builders");
test("merge", function() {
var o = {'a': 1, 'b': 2};
deepEqual(_.merge(o), {a: 1, b: 2}, 'should return a copy of the object if given only one');
deepEqual(_.merge({'a': 1, 'b': 2}, {b: 42}), {'a': 1, b: 42}, 'should merge two objects');
deepEqual(_.merge({a: 1, b: 2}, {b: 42}, {c: 3}), {a: 1, b: 42, c: 3}, 'should merge three or more objects');
deepEqual(_.merge({a: 1, b: 2}, {b: 42}, {c: 3}, {c: 4}), {a: 1, b: 42, c: 4}, 'should merge three or more objects');
var a = {'a': 1, 'b': 2};
var $ = _.merge(a, {'a': 42});
deepEqual(a, {'a': 1, 'b': 2}, 'should not modify the original');
});
test("renameKeys", function() {
deepEqual(_.renameKeys({'a': 1, 'b': 2}, {'a': 'A'}), {'b': 2, 'A': 1}, 'should rename the keys in the first object to the mapping in the second object');
var a = {'a': 1, 'b': 2};
var $ = _.renameKeys(a, {'a': 'A'});
deepEqual(a, {'a': 1, 'b': 2}, 'should not modify the original');
});
test("snapshot", function() {
var o = {'a': 1, 'b': 2};
var oSnap = _.snapshot(o);
var a = [1,2,3,4];
var aSnap = _.snapshot(a);
var n = [1,{a: 1, b: [1,2,3]},{},4];
var nSnap = _.snapshot(n);
var c = [1,{a: 1, b: [1,2,3]},{},4];
var cSnap = _.snapshot(c);
c[1].b = 42;
deepEqual(o, oSnap, 'should create a deep copy of an object');
deepEqual(a, aSnap, 'should create a deep copy of an array');
deepEqual(n, nSnap, 'should create a deep copy of an array');
deepEqual(nSnap, [1,{a: 1, b: [1,2,3]},{},4], 'should allow changes to the original to not change copies');
});
test("setPath", function() {
var obj = {a: {b: {c: 42, d: 108}}};
var ary = ['a', ['b', ['c', 'd'], 'e']];
var nest = [1, {a: 2, b: [3,4], c: 5}, 6];
deepEqual(_.setPath(obj, 9, ['a', 'b', 'c']), {a: {b: {c: 9, d: 108}}}, '');
deepEqual(_.setPath(ary, 9, [1, 1, 0]), ['a', ['b', [9, 'd'], 'e']], '');
deepEqual(_.setPath(nest, 9, [1, 'b', 1]), [1, {a: 2, b: [3,9], c: 5}, 6], '');
deepEqual(_.setPath(obj, 9, 'a'), {a: 9}, '');
deepEqual(_.setPath(ary, 9, 1), ['a', 9], '');
deepEqual(obj, {a: {b: {c: 42, d: 108}}}, 'should not modify the original object');
deepEqual(ary, ['a', ['b', ['c', 'd'], 'e']], 'should not modify the original array');
deepEqual(nest, [1, {a: 2, b: [3,4], c: 5}, 6], 'should not modify the original nested structure');
});
test("updatePath", function() {
var obj = {a: {b: {c: 42, d: 108}}};
var ary = ['a', ['b', ['c', 'd'], 'e']];
var nest = [1, {a: 2, b: [3,4], c: 5}, 6];
deepEqual(_.updatePath(obj, _.always(9), ['a', 'b', 'c']), {a: {b: {c: 9, d: 108}}}, '');
deepEqual(_.updatePath(ary, _.always(9), [1, 1, 0]), ['a', ['b', [9, 'd'], 'e']], '');
deepEqual(_.updatePath(nest, _.always(9), [1, 'b', 1]), [1, {a: 2, b: [3,9], c: 5}, 6], '');
deepEqual(_.updatePath(obj, _.always(9), 'a'), {a: 9}, '');
deepEqual(_.updatePath(ary, _.always(9), 1), ['a', 9], '');
deepEqual(obj, {a: {b: {c: 42, d: 108}}}, 'should not modify the original object');
deepEqual(ary, ['a', ['b', ['c', 'd'], 'e']], 'should not modify the original array');
deepEqual(nest, [1, {a: 2, b: [3,4], c: 5}, 6], 'should not modify the original nested structure');
});
});