Skip to content

Commit f3373de

Browse files
committed
Allow nested propsdiffs for plain objects only
1 parent 31b4b52 commit f3373de

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed

index.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ var array = require('x-is-array');
44
var object = require('is-object');
55
var proto = require('getprototypeof');
66

7+
var objectProto = Object.prototype;
8+
79
var ignoredKeys = ['type', 'children', 'value'];
810

911
module.exports = diff;
@@ -112,17 +114,17 @@ function diffObjects(left, right) {
112114
continue;
113115
}
114116

115-
if (object(leftValue) && object(rightValue)) {
116-
if (proto(rightValue) === proto(leftValue)) {
117-
objectDiff = diffObjects(leftValue, rightValue);
117+
if (
118+
object(leftValue) &&
119+
object(rightValue) &&
120+
proto(rightValue) === objectProto &&
121+
proto(leftValue) === objectProto
122+
) {
123+
objectDiff = diffObjects(leftValue, rightValue);
118124

119-
if (objectDiff) {
120-
diff = diff || {};
121-
diff[leftKey] = objectDiff;
122-
}
123-
} else {
125+
if (objectDiff) {
124126
diff = diff || {};
125-
diff[leftKey] = rightValue;
127+
diff[leftKey] = objectDiff;
126128
}
127129
} else {
128130
diff = diff || {};

readme.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,7 @@ Patches represent changes. They come with three properties:
157157
In the diff:
158158

159159
* If a key is removed, the key’s value is set to `undefined`.
160-
* If the new value is of the same non-primitive type as the new value
161-
(their prototype’s are equal, thus both array or both object), the key’s
160+
* If the new value and the old value are both plain objects, the key’s
162161
value is set to a `PropsDiff` of both values.
163162
* In all other cases, the key’s value is set to the new value.
164163

test.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,6 @@ test('unist-diff', function (t) {
8686
);
8787
})();
8888

89-
(function () {
90-
var alpha = {type: 'alpha'};
91-
var left = {type: 'bravo', charlie: [1, 2, 3], children: [alpha]};
92-
var right = {type: 'bravo', charlie: [1, 2, 3], children: [alpha]};
93-
94-
st.deepEqual(
95-
diff(left, right),
96-
{left: left},
97-
'should not return a patch for deep equal arrays'
98-
);
99-
})();
100-
10189
(function () {
10290
var alpha = {type: 'alpha'};
10391
var left = {type: 'bravo', data: {charlie: 'delta', echo: true, foxtrot: 1, golf: null}, children: [alpha]};
@@ -167,6 +155,25 @@ test('unist-diff', function (t) {
167155
);
168156
})();
169157

158+
(function () {
159+
var alpha = {type: 'alpha'};
160+
var left = {type: 'bravo', charlie: [1, 2, 3], children: [alpha]};
161+
var right = {type: 'bravo', charlie: [1, 2, 3], children: [alpha]};
162+
163+
st.deepEqual(
164+
diff(left, right),
165+
{
166+
0: {
167+
type: 'props',
168+
left: left,
169+
right: {charlie: [1, 2, 3]}
170+
},
171+
left: left
172+
},
173+
'should return a patch for deep equal arrays'
174+
);
175+
})();
176+
170177
(function () {
171178
var alpha = {type: 'alpha'};
172179
var left = {type: 'bravo', charlie: [1, 2, 3], children: [alpha]};

0 commit comments

Comments
 (0)