Skip to content

Commit 8a67d62

Browse files
committed
Fixed bug when setPatch deep properties when parents are undefined
1 parent 17b80f7 commit 8a67d62

File tree

8 files changed

+391
-29
lines changed

8 files changed

+391
-29
lines changed

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dop",
3-
"version": "0.22.0",
3+
"version": "0.22.1",
44
"main": "./dist/dop.nodejs.js",
55
"browser": "./dist/dop.js",
66
"unpkg": "./dist/dop.min.js",
@@ -38,6 +38,7 @@
3838
},
3939
"devDependencies": {
4040
"coveralls": "^2.11.4",
41+
"electron": "^1.4.12",
4142
"express": "^4.13.4",
4243
"faucet": "^0.0.1",
4344
"grunt": "^0.4.5",
@@ -48,10 +49,10 @@
4849
"grunt-contrib-watch": "^0.6.1",
4950
"grunt-optimize-js": "^0.6.0",
5051
"js-combinatorics": "^0.5.2",
52+
"lodash.merge": "^4.6.0",
5153
"tap": "^7.1.2",
5254
"tape": "^4.6.3",
53-
"tape-run": "^2.1.4",
54-
"electron": "^1.4.12"
55+
"tape-run": "^2.1.4"
5556
},
5657
"scripts": {
5758
"grunt": "grunt",

src/core/objects/setPatch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ dop.core.setPatchMutator = function(destiny, prop, value, typeofValue, path) {
3434
dop.set(destiny, prop, dop.util.clone(value[1]));
3535

3636
// Array mutations
37-
else {
37+
else if (isArray(destiny[prop])) {
3838
if (!isArray(typeInstruction))
3939
value = [value];
4040

src/util/path.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,24 @@ dop.util.pathRecursive = function (source, callback, destiny, mutator, circular,
2727
skip = mutator(destiny, prop, value, typeofValue, path);
2828

2929
// Objects or arrays
30-
if ((typeofValue=='object' || typeofValue=='array') && skip !== true && value!==source && circular.indexOf(value)==-1) {
30+
if (
31+
(typeofValue=='object' || typeofValue=='array') &&
32+
skip !== true &&
33+
value!==source &&
34+
circular.indexOf(value)==-1 &&
35+
(hasDestiny && destiny[prop]!==undefined)
36+
) {
3137
circular.push(value);
32-
dop.util.pathRecursive(value, callback, hasDestiny?destiny[prop]:undefined, mutator, circular, path, hasCallback, hasDestiny);
38+
dop.util.pathRecursive(
39+
value,
40+
callback,
41+
hasDestiny ? destiny[prop] : undefined,
42+
mutator,
43+
circular,
44+
path,
45+
hasCallback,
46+
hasDestiny
47+
);
3348
}
3449

3550
path.pop();

test/getpatchs.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var decode = dop.decode;
77

88

99

10-
function applyPatch(collector) {
10+
function getPatch(collector) {
1111
var patch = dop.core.getPatch(collector.mutations);
1212
var patchServer = decode(encode(patch));
1313
collector.destroy();
@@ -36,7 +36,7 @@ test(header+'Adding property', function(t) {
3636

3737
var collector = dop.collect();
3838
set(objectServer, 'one', 1);
39-
var patchGenerated = applyPatch(collector);
39+
var patchGenerated = getPatch(collector);
4040
t.equal(collector.mutations.length, mutationsExpected, 'Mutations expecteds: '+collector.mutations.length);
4141
maketest(t, patchGenerated, patchExpected);
4242
});
@@ -49,7 +49,7 @@ test(header+'Changing property', function(t) {
4949

5050
var collector = dop.collect();
5151
set(objectServer, 'one', 11);
52-
var patchGenerated = applyPatch(collector);
52+
var patchGenerated = getPatch(collector);
5353
t.equal(collector.mutations.length, 1, 'Mutations expecteds: '+collector.mutations.length);
5454
maketest(t, patchGenerated, patchExpected);
5555
});
@@ -62,7 +62,7 @@ test(header+'Changing property with the same value', function(t) {
6262

6363
var collector = dop.collect();
6464
set(objectServer, 'one', 11);
65-
var patchGenerated = applyPatch(collector);
65+
var patchGenerated = getPatch(collector);
6666
t.equal(collector.mutations.length, mutationsExpected, 'Mutations expecteds: '+collector.mutations.length);
6767
maketest(t, patchGenerated, patchExpected);
6868
});
@@ -75,7 +75,7 @@ test(header+'Deleting property', function(t) {
7575

7676
var collector = dop.collect();
7777
del(objectServer, 'one');
78-
var patchGenerated = applyPatch(collector);
78+
var patchGenerated = getPatch(collector);
7979
t.equal(collector.mutations.length, mutationsExpected, 'Mutations expecteds: '+collector.mutations.length);
8080
maketest(t, patchGenerated, patchExpected);
8181
});
@@ -89,7 +89,7 @@ test(header+'Change and delete a removed item', function(t) {
8989
set(objectServer, 'one', 'Changeddd');
9090
del(objectServer, 'one');
9191
set(objectServer, 'two', 2);
92-
var patchGenerated = applyPatch(collector);
92+
var patchGenerated = getPatch(collector);
9393
t.equal(collector.mutations.length, mutationsExpected, 'Mutations expecteds: '+collector.mutations.length);
9494
maketest(t, patchGenerated, patchExpected);
9595
});
@@ -105,7 +105,7 @@ test(header+'Setting property array', function(t) {
105105
set(object.array, 5, "three");
106106

107107

108-
var patchGenerated = applyPatch(collector);
108+
var patchGenerated = getPatch(collector);
109109
t.equal(collector.mutations.length, mutationsExpected, 'Mutations expecteds: '+collector.mutations.length);
110110
maketest(t, patchGenerated, patchExpected);
111111
});
@@ -134,7 +134,7 @@ test(header+'Setting an array and mutating it', function(t) {
134134
set(object.array[2], 'B1', 'string');
135135

136136

137-
var patchGenerated = applyPatch(collector);
137+
var patchGenerated = getPatch(collector);
138138
t.equal(collector.mutations.length, mutationsExpected, 'Mutations expecteds: '+collector.mutations.length);
139139
maketest(t, patchGenerated, patchExpected);
140140
});
@@ -156,7 +156,7 @@ test(header+'Mutating array then mutating nested objects', function(t) {
156156

157157

158158

159-
var patchGenerated = applyPatch(collector);
159+
var patchGenerated = getPatch(collector);
160160
t.equal(collector.mutations.length, mutationsExpected, 'Mutations expecteds: '+collector.mutations.length);
161161
maketest(t, patchGenerated, patchExpected);
162162
});
@@ -177,7 +177,7 @@ test(header+'Mutating nested objects then mutating parent array', function(t) {
177177

178178

179179

180-
var patchGenerated = applyPatch(collector);
180+
var patchGenerated = getPatch(collector);
181181
t.equal(collector.mutations.length, mutationsExpected, 'Mutations expecteds: '+collector.mutations.length);
182182
maketest(t, patchGenerated, patchExpected);
183183
});
@@ -197,7 +197,7 @@ test(header+'Mutating array twice', function(t) {
197197
object.array.sort();
198198

199199

200-
var patchGenerated = applyPatch(collector);
200+
var patchGenerated = getPatch(collector);
201201
t.equal(collector.mutations.length, mutationsExpected, 'Mutations expecteds: '+collector.mutations.length);
202202
maketest(t, patchGenerated, patchExpected);
203203

@@ -217,7 +217,7 @@ test(header+'Mutating array and mutating array deeper', function(t) {
217217
object.array[0].reverse();
218218

219219

220-
var patchGenerated = applyPatch(collector);
220+
var patchGenerated = getPatch(collector);
221221
t.equal(collector.mutations.length, mutationsExpected, 'Mutations expecteds: '+collector.mutations.length);
222222
maketest(t, patchGenerated, patchExpected);
223223

@@ -236,7 +236,7 @@ test(header+'Mutating array deeper and mutating container', function(t) {
236236
object.array.reverse();
237237

238238

239-
var patchGenerated = applyPatch(collector);
239+
var patchGenerated = getPatch(collector);
240240
t.equal(collector.mutations.length, mutationsExpected, 'Mutations expecteds: '+collector.mutations.length);
241241
maketest(t, patchGenerated, patchExpected);
242242
})

test/merge.js

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
var test = require('tape');
2+
var dop = require('./.proxy').create();
3+
var merge = require('lodash.merge');
4+
var Combinatorics = require('js-combinatorics');
5+
6+
7+
8+
var objects = {}
9+
objects.empty = function(){return {}}
10+
11+
objects.types = function(){return {
12+
string:'string',
13+
boolean:true,
14+
number:-123,
15+
Infinity:-Infinity,
16+
float:1.234153454354341,
17+
long:12313214234312324353454534534,
18+
null:null,
19+
// // from here breaks
20+
// undefined:undefined,
21+
// NaN:NaN,
22+
// symbol:Symbol('sym'),
23+
// date: new Date(),
24+
// regexp: /molamazo/g,
25+
// function: function(){console.log(arguments)}
26+
}}
27+
objects.obj1 = function(){return {
28+
a: 11,
29+
b: 12,
30+
array: [1,2,3,{abc:123}],
31+
d: {
32+
d1: 13,
33+
d2: {
34+
d21: 123,
35+
d22: {
36+
d221: 12,
37+
d223: {
38+
hola: 'hola',
39+
undefined: 'undefined'
40+
}
41+
}
42+
}
43+
},
44+
arrobj: ['a','b','c','d'],
45+
f: 5,
46+
g: 123
47+
}}
48+
objects.obj2 = function(){return {
49+
b: 3,
50+
c: 5,
51+
obj: {lolo:111},
52+
// fun: function(){},
53+
arr: [1,2,3,{La:123}],
54+
array: [567],
55+
arrobj: {0:1,1:2},
56+
d: {
57+
d2: {
58+
d22: {
59+
d222: 25,
60+
d223: {
61+
hola:'mundo',
62+
// undefined: undefined // lodash ignores undefined values
63+
}
64+
}
65+
}
66+
}
67+
}};
68+
69+
var all = ['empty', 'types', 'obj1', 'obj2']
70+
var argsCases = [], cmb, a
71+
72+
for (var i=1; i<all.length; i++) {
73+
cmb = Combinatorics.baseN(all, i+1);
74+
while(a = cmb.next()) argsCases.push(a)//argsCases.push(a, a.concat({},'string',{},1,null,undefined,new Date(),/test/,new gify(),true,{},[{}]));
75+
}
76+
77+
78+
test('All cases', function(t) {
79+
80+
for (i=0; i<argsCases.length; i++) {
81+
var data1 = argsCases[i].map(function(item) {
82+
return objects[item]()
83+
})
84+
var data2 = argsCases[i].map(function(item) {
85+
return objects[item]()
86+
})
87+
var o1 = dop.util.merge.apply(this, data1)
88+
var o2 = merge.apply(this, data2)
89+
t.deepEqual(o1, o2, argsCases[i].join('(), '))
90+
}
91+
92+
t.end()
93+
})
94+
95+
96+
// test(' {}, obj1() ', function(t) {
97+
// var o1 = dop.util.merge({}, obj1())
98+
// var o2 = merge({}, obj1())
99+
// t.deepEqual(o1, o2)
100+
// t.end()
101+
// })
102+
103+
// test(' {}, obj2() ', function(t) {
104+
// var o1 = dop.util.merge({}, obj2())
105+
// var o2 = merge({}, obj2())
106+
// t.deepEqual(o1, o2)
107+
// t.end()
108+
// })
109+
110+
111+
// test(' {}, types() ', function(t) {
112+
// var o1 = dop.util.merge({}, types())
113+
// var o2 = merge({}, types())
114+
// t.deepEqual(o1, o2)
115+
// t.end()
116+
// })
117+
118+
119+
120+
121+
122+
123+
124+
125+
126+
127+
128+
129+
130+
131+
132+
133+
// test(' {}, obj1(), obj1() ', function(t) {
134+
// var o1 = dop.util.merge({}, obj1(), obj1())
135+
// var o2 = merge({}, obj1(), obj1())
136+
// t.deepEqual(o1, o2)
137+
// t.end()
138+
// })
139+
140+
// // test(' {}, obj1(), obj2() ', function(t) {
141+
// // var o1 = dop.util.merge({}, obj1(), obj2())
142+
// // var o2 = merge({}, obj1(), obj2())
143+
// // t.deepEqual(o1, o2)
144+
// // t.end()
145+
// // })
146+
147+
148+
// test(' {}, obj1(), types() ', function(t) {
149+
// var o1 = dop.util.merge({}, obj1(), types())
150+
// var o2 = merge({}, obj1(), types())
151+
// t.deepEqual(o1, o2)
152+
// t.end()
153+
// })
154+
155+
156+
157+
158+
159+
160+
161+
162+
// test(' {}, obj2(), obj1() ', function(t) {
163+
// var o1 = dop.util.merge({}, obj2(), obj1())
164+
// var o2 = merge({}, obj2(), obj1())
165+
// t.deepEqual(o1, o2)
166+
// t.end()
167+
// })
168+
169+
// test(' {}, obj2(), obj2() ', function(t) {
170+
// var o1 = dop.util.merge({}, obj2(), obj2())
171+
// var o2 = merge({}, obj2(), obj2())
172+
// t.deepEqual(o1, o2)
173+
// t.end()
174+
// })
175+
176+
177+
// test(' {}, obj2(), types() ', function(t) {
178+
// var o1 = dop.util.merge({}, obj2(), types())
179+
// var o2 = merge({}, obj2(), types())
180+
// t.deepEqual(o1, o2)
181+
// t.end()
182+
// })
183+
184+
185+
186+
187+
188+
189+
// test(' {}, types(), obj1() ', function(t) {
190+
// var o1 = dop.util.merge({}, types(), obj1())
191+
// var o2 = merge({}, types(), obj1())
192+
// t.deepEqual(o1, o2)
193+
// t.end()
194+
// })
195+
196+
// test(' {}, types(), obj2() ', function(t) {
197+
// var o1 = dop.util.merge({}, types(), obj2())
198+
// var o2 = merge({}, types(), obj2())
199+
// t.deepEqual(o1, o2)
200+
// t.end()
201+
// })
202+
203+
204+
// test(' {}, types(), types() ', function(t) {
205+
// var o1 = dop.util.merge({}, types(), types())
206+
// var o2 = merge({}, types(), types())
207+
// t.deepEqual(o1, o2)
208+
// t.end()
209+
// })

0 commit comments

Comments
 (0)