Skip to content

Commit 076e367

Browse files
author
Phillip Clark
committed
Merge pull request flitbit#36 from drinks/object-type-checking
Stricter type checking among `object` types
2 parents 1bd7cfc + b0dada9 commit 076e367

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

index.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
22
* deep-diff.
33
* Licensed under the MIT License.
4-
*/
4+
*/
55
/*jshint indent:2, laxcomma:true*/
66
;(function (undefined) {
77
"use strict";
@@ -79,6 +79,26 @@
7979
return arr;
8080
}
8181

82+
function realTypeOf(subject) {
83+
var type = typeof subject;
84+
if (type !== 'object') {
85+
return type;
86+
}
87+
88+
if (subject === Math) {
89+
return 'math';
90+
} else if (subject === null) {
91+
return 'null';
92+
} else if (Array.isArray(subject)) {
93+
return 'array';
94+
} else if (subject instanceof Date) {
95+
return 'date';
96+
} else if (/^\/.*\//.test(subject.toString())) {
97+
return 'regexp';
98+
}
99+
return 'object';
100+
}
101+
82102
function deepDiff(lhs, rhs, changes, prefilter, path, key, stack) {
83103
path = path || [];
84104
var currentPath = path.slice(0);
@@ -94,7 +114,7 @@
94114
}
95115
} else if (rtype === 'undefined') {
96116
changes(new DiffDeleted(currentPath, lhs));
97-
} else if (ltype !== rtype) {
117+
} else if (realTypeOf(lhs) !== realTypeOf(rhs)) {
98118
changes(new DiffEdit(currentPath, lhs, rhs));
99119
} else if (lhs instanceof Date && rhs instanceof Date && ((lhs - rhs) !== 0)) {
100120
changes(new DiffEdit(currentPath, lhs, rhs));

test/tests.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,33 @@ describe('deep-diff', function () {
2020
expect(deep.diff(empty, {})).to.be.an('undefined');
2121
});
2222

23+
describe('when compared to a different type of keyless object', function () {
24+
var ctor = function () {
25+
this.foo = 'bar';
26+
};
27+
var comparandTuples = [['an array', { key: [] }],
28+
['an object', { key: {}}],
29+
['a date', { key: new Date() }],
30+
['a null', { key: null }],
31+
['a regexp literal', {key: /a/}],
32+
['Math', {key: Math}]];
33+
34+
comparandTuples.forEach(function (lhsTuple) {
35+
comparandTuples.forEach(function (rhsTuple) {
36+
if (lhsTuple[0] === rhsTuple[0]) {
37+
return;
38+
}
39+
it('shows differences when comparing ' + lhsTuple[0] + ' to ' + rhsTuple[0], function () {
40+
var diff = deep.diff(lhsTuple[1], rhsTuple[1]);
41+
expect(diff).to.be.ok();
42+
expect(diff.length).to.be(1);
43+
expect(diff[0]).to.have.property('kind');
44+
expect(diff[0].kind).to.be('E');
45+
});
46+
});
47+
});
48+
});
49+
2350
describe('when compared with an object having other properties', function () {
2451
var comparand = { other: 'property', another: 13.13 };
2552
var diff = deep.diff(empty, comparand);
@@ -72,6 +99,14 @@ describe('deep-diff', function () {
7299
expect(diff[0].kind).to.be('E');
73100
});
74101

102+
it ('shows the property as edited when compared to an array', function () {
103+
var diff = deep.diff(lhs, ['one']);
104+
expect(diff).to.be.ok();
105+
expect(diff.length).to.be(1);
106+
expect(diff[0]).to.have.property('kind');
107+
expect(diff[0].kind).to.be('E');
108+
});
109+
75110
});
76111

77112
describe('A target that has null value', function () {

0 commit comments

Comments
 (0)