Skip to content

Commit

Permalink
[Breaking] comparing arrays and objects should always fail, per node‘…
Browse files Browse the repository at this point in the history
…s assert.deepEqual

Fixes #12.

Relates to tape-testing/tape#186
  • Loading branch information
ljharb committed Jul 31, 2019
1 parent 558eecf commit 6c792e4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var isArguments = require('is-arguments');
var is = require('object-is');
var isRegex = require('is-regex');
var flags = require('regexp.prototype.flags');
var isArray = require('isarray');

function deepEqual(actual, expected, options) {
var opts = options || {};
Expand All @@ -12,6 +13,12 @@ function deepEqual(actual, expected, options) {
return true;
}

var actualIsArray = isArray(actual);
var expectedIsArray = isArray(expected);
if (actualIsArray !== expectedIsArray) {
return false;
}

var actualIsRegex = isRegex(actual);
var expectedIsRegex = isRegex(expected);
if (actualIsRegex || expectedIsRegex) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"dependencies": {
"is-arguments": "^1.0.4",
"is-regex": "^1.0.4",
"isarray": "^2.0.5",
"object-is": "^1.0.1",
"object-keys": "^1.1.1",
"regexp.prototype.flags": "^1.2.0"
Expand Down
16 changes: 8 additions & 8 deletions test/cmp.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,23 +284,23 @@ test('regexen', function (t) {
});

test('arrays and objects', function (t) {
t.ok(equal([], {}), 'empty array and empty object are equal');
t.ok(equal({}, []), 'empty object and empty array are equal');
t.notOk(equal([], {}), 'empty array and empty object are not equal');
t.notOk(equal({}, []), 'empty object and empty array are not equal');

t.ok(equal([], {}, { strict: true }), 'strict: empty array and empty object are equal');
t.ok(equal({}, [], { strict: true }), 'strict: empty object and empty array are equal');
t.notOk(equal([], {}, { strict: true }), 'strict: empty array and empty object are not equal');
t.notOk(equal({}, [], { strict: true }), 'strict: empty object and empty array are not equal');

t.notOk(equal([], { length: 0 }), 'empty array and empty arraylike object are not equal');
t.notOk(equal({ length: 0 }, []), 'empty arraylike object and empty array are not equal');

t.notOk(equal([], { length: 0 }, { strict: true }), 'strict: empty array and empty arraylike object are not equal');
t.notOk(equal({ length: 0 }, [], { strict: true }), 'strict: empty arraylike object and empty array are not equal');

t.ok(equal([1], { 0: 1 }), 'array and object are equal');
t.ok(equal({ 0: 1 }, [1]), 'object and array are equal');
t.notOk(equal([1], { 0: 1 }), 'array and object are not equal');
t.notOk(equal({ 0: 1 }, [1]), 'object and array are not equal');

t.ok(equal([1], { 0: 1 }, { strict: true }), 'strict: array and object are equal');
t.ok(equal({ 0: 1 }, [1], { strict: true }), 'strict: object and array are equal');
t.notOk(equal([1], { 0: 1 }, { strict: true }), 'strict: array and object are not equal');
t.notOk(equal({ 0: 1 }, [1], { strict: true }), 'strict: object and array are not equal');

t.end();
});

0 comments on commit 6c792e4

Please sign in to comment.