Skip to content

added array and object support #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,43 @@ function diff( opts, subjects ){
}

function isStrictEqual( a, b ){
/* Date */
if (a instanceof Date && !(b instanceof Date)) return false;
else if (!(a instanceof Date) && b instanceof Date) return false;
else if (a instanceof Date && b instanceof Date)
return a.getTime() === b.getTime();

/* Array */
else if (Array.isArray(a) && !Array.isArray(b)) return false;
else if (!Array.isArray(a) && Array.isArray(b)) return false;
else if (Array.isArray(a) && Array.isArray(b)) {
if (a.length != b.length) return false;
for (var i = 0; i < a.length; i++) {
if (a[i] != b[i]) return false;
}

return true;
}
/* Object */
else if (typeof a === 'object' && typeof b !== 'object') return false;
else if (typeof a !== 'object' && typeof b === 'object') return false;
else if (typeof a == 'object' && typeof b === 'object') {
var keysA = Object.keys(a);
var keysB = Object.keys(b);
if (keysA.length != keysB.length) return false;
for (var i = 0; i < keysA.length; i++) {
if (typeof b[keysA[i]] === 'undefined') return false;
if (!isStrictEqual(a[keysA[i]], b[keysA[i]])) return false;
}

for (var i = 0; i < keysB.length; i++) {
if (typeof a[keysB[i]] === 'undefined') return false;
if (!isStrictEqual(a[keysB[i]], b[keysB[i]])) return false;
}

return true;
}

/* Value */
return a === b;
}
7 changes: 5 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ test(function( t ) {
power: 54,
height: undefined,
level: 1,
skills: ['magic', 'running']
};

var b = {
speed: 4, // unchanged
power: 22, // changed
level: undefined, // changed
weight: 10, // added
skills: ['magic', 'running'] // unchanged
};

t.deepEqual(diff(a, b), {
Expand All @@ -30,6 +32,7 @@ test(function( t ) {
level: 100, // changed
material: 'steel', // added
location: undefined, // added but undefined
skills: ['magic', 'running', 'climbing'] // changed
};

t.deepEqual(diff(a, b, c), {
Expand All @@ -38,6 +41,7 @@ test(function( t ) {
level: 100,
weight: 10,
material: 'steel',
skills: ['magic', 'running', 'climbing']
});

t.deepEqual(diff({}, {}), {});
Expand All @@ -60,8 +64,7 @@ test('Custom equality', function( t ) {
};

t.deepEqual(diff(a, b), {
created: new Date(created),
updated: now,
updated: now
}, 'expected default behavior');

t.deepEqual(diff.custom({
Expand Down