Skip to content

Commit a5b09a5

Browse files
committed
fixup: generalize, remove special handling for Error, Buffer, and RegExp
1 parent f242258 commit a5b09a5

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

lib/assert.js

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@
2525
'use strict';
2626

2727
// UTILITY
28-
const compare = process.binding('buffer').compare;
2928
const util = require('util');
30-
const Buffer = require('buffer').Buffer;
3129
const pSlice = Array.prototype.slice;
3230

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

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

157-
// 7.3 If the expected value is a RegExp object, the actual value is
158-
// equivalent if it is also a RegExp object with the same source and
159-
// properties (`global`, `multiline`, `lastIndex`, `ignoreCase`).
160-
} else if (util.isRegExp(actual) && util.isRegExp(expected)) {
161-
return actual.source === expected.source &&
162-
actual.global === expected.global &&
163-
actual.multiline === expected.multiline &&
164-
actual.lastIndex === expected.lastIndex &&
165-
actual.ignoreCase === expected.ignoreCase;
166-
167153
// 7.4. Other pairs that do not both pass typeof value == 'object',
168154
// equivalence is determined by ==.
169155
} else if ((actual === null || typeof actual !== 'object') &&
@@ -185,6 +171,10 @@ function isArguments(object) {
185171
return Object.prototype.toString.call(object) == '[object Arguments]';
186172
}
187173

174+
function isArrayOrString(object) {
175+
return object instanceof Array || object instanceof String;
176+
}
177+
188178
function objEquiv(a, b, strict) {
189179
if (a === null || a === undefined || b === null || b === undefined)
190180
return false;
@@ -202,18 +192,26 @@ function objEquiv(a, b, strict) {
202192
b = pSlice.call(b);
203193
return _deepEqual(a, b, strict);
204194
}
205-
var ka, kb, key, i;
206-
207-
function _getKeys(obj) {
208-
if (obj instanceof Error) {
209-
return Object.getOwnPropertyNames(obj);
195+
var ka = Object.getOwnPropertyNames(a),
196+
kb = Object.getOwnPropertyNames(b),
197+
key, i;
198+
199+
// when comparing String/Array to non-Strings/non-Array, ignore length prop
200+
const aLengthIndex = ka.indexOf('length');
201+
const bLengthIndex = kb.indexOf('length');
202+
const aHasLength = (aLengthIndex !== -1);
203+
const bHasLength = (bLengthIndex !== -1);
204+
205+
if (isArrayOrString(a) !== isArrayOrString(b)) {
206+
if (aHasLength !== bHasLength) {
207+
if (aHasLength) {
208+
ka.splice(bLengthIndex);
209+
} else {
210+
kb.splice(aLengthIndex);
211+
}
210212
}
211-
return Object.keys(obj);
212213
}
213214

214-
ka = _getKeys(a);
215-
kb = _getKeys(b);
216-
217215
// having the same number of owned properties (keys incorporates
218216
// hasOwnProperty)
219217
if (ka.length !== kb.length)

0 commit comments

Comments
 (0)