Skip to content
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fixup: generalize, remove special handling for Error, Buffer, and RegExp
  • Loading branch information
Trott committed Oct 12, 2015
commit a5b09a5740fb3697ad1ed8b02da61de786d071c6
44 changes: 21 additions & 23 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
'use strict';

// UTILITY
const compare = process.binding('buffer').compare;
const util = require('util');
const Buffer = require('buffer').Buffer;
const pSlice = Array.prototype.slice;

// 1. The assert module provides functions that throw
Expand Down Expand Up @@ -146,24 +144,12 @@ function _deepEqual(actual, expected, strict) {
// 7.1. All identical values are equivalent, as determined by ===.
if (actual === expected) {
return true;
} else if (actual instanceof Buffer && expected instanceof Buffer) {
return compare(actual, expected) === 0;

// 7.2. If the expected value is a Date object, the actual value is
// equivalent if it is also a Date object that refers to the same time.
} else if (util.isDate(actual) && util.isDate(expected)) {
return actual.getTime() === expected.getTime();

// 7.3 If the expected value is a RegExp object, the actual value is
// equivalent if it is also a RegExp object with the same source and
// properties (`global`, `multiline`, `lastIndex`, `ignoreCase`).
} else if (util.isRegExp(actual) && util.isRegExp(expected)) {
return actual.source === expected.source &&
actual.global === expected.global &&
actual.multiline === expected.multiline &&
actual.lastIndex === expected.lastIndex &&
actual.ignoreCase === expected.ignoreCase;

// 7.4. Other pairs that do not both pass typeof value == 'object',
// equivalence is determined by ==.
} else if ((actual === null || typeof actual !== 'object') &&
Expand All @@ -185,6 +171,10 @@ function isArguments(object) {
return Object.prototype.toString.call(object) == '[object Arguments]';
}

function isArrayOrString(object) {
return object instanceof Array || object instanceof String;
}

function objEquiv(a, b, strict) {
if (a === null || a === undefined || b === null || b === undefined)
return false;
Expand All @@ -202,18 +192,26 @@ function objEquiv(a, b, strict) {
b = pSlice.call(b);
return _deepEqual(a, b, strict);
}
var ka, kb, key, i;

function _getKeys(obj) {
if (obj instanceof Error) {
return Object.getOwnPropertyNames(obj);
var ka = Object.getOwnPropertyNames(a),
kb = Object.getOwnPropertyNames(b),
key, i;

// when comparing String/Array to non-Strings/non-Array, ignore length prop
const aLengthIndex = ka.indexOf('length');
const bLengthIndex = kb.indexOf('length');
const aHasLength = (aLengthIndex !== -1);
const bHasLength = (bLengthIndex !== -1);

if (isArrayOrString(a) !== isArrayOrString(b)) {
if (aHasLength !== bHasLength) {
if (aHasLength) {
ka.splice(bLengthIndex);
} else {
kb.splice(aLengthIndex);
}
}
return Object.keys(obj);
}

ka = _getKeys(a);
kb = _getKeys(b);

// having the same number of owned properties (keys incorporates
// hasOwnProperty)
if (ka.length !== kb.length)
Expand Down