Skip to content

Commit 98c05e5

Browse files
committed
Merge pull request #7 from teppeis/es6-object-keys
Fix deepEqual for ES6 Object.keys
2 parents d08b0bf + fd1cd62 commit 98c05e5

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

assert.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,10 @@ function objEquiv(a, b) {
225225
return false;
226226
// an identical 'prototype' property.
227227
if (a.prototype !== b.prototype) return false;
228-
//~~~I've managed to break Object.keys through screwy arguments passing.
229-
// Converting to array solves the problem.
228+
// if one is a primitive, the other must be same
229+
if (util.isPrimitive(a) || util.isPrimitive(b)) {
230+
return a === b;
231+
}
230232
var aIsArgs = isArguments(a),
231233
bIsArgs = isArguments(b);
232234
if ((aIsArgs && !bIsArgs) || (!aIsArgs && bIsArgs))
@@ -236,13 +238,9 @@ function objEquiv(a, b) {
236238
b = pSlice.call(b);
237239
return _deepEqual(a, b);
238240
}
239-
try {
240-
var ka = objectKeys(a),
241-
kb = objectKeys(b),
242-
key, i;
243-
} catch (e) {//happens when one is a string literal and the other isn't
244-
return false;
245-
}
241+
var ka = objectKeys(a),
242+
kb = objectKeys(b),
243+
key, i;
246244
// having the same number of owned properties (keys incorporates
247245
// hasOwnProperty)
248246
if (ka.length != kb.length)

test.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,25 @@ test('assert.deepEqual - instances', function () {
158158
nameBuilder2.prototype = Object;
159159
nb2 = new nameBuilder2('Ryan', 'Dahl');
160160
assert.throws(makeBlock(assert.deepEqual, nb1, nb2), assert.AssertionError);
161+
});
162+
163+
test('assert.deepEqual - ES6 primitives', function () {
164+
assert.throws(makeBlock(assert.deepEqual, null, {}), assert.AssertionError);
165+
assert.throws(makeBlock(assert.deepEqual, undefined, {}), assert.AssertionError);
166+
assert.throws(makeBlock(assert.deepEqual, 'a', ['a']), assert.AssertionError);
167+
assert.throws(makeBlock(assert.deepEqual, 'a', {0: 'a'}), assert.AssertionError);
168+
assert.throws(makeBlock(assert.deepEqual, 1, {}), assert.AssertionError);
169+
assert.throws(makeBlock(assert.deepEqual, true, {}), assert.AssertionError);
170+
if (typeof Symbol === 'symbol') {
171+
assert.throws(makeBlock(assert.deepEqual, Symbol(), {}), assert.AssertionError);
172+
}
173+
});
161174

162-
// String literal + object blew up my implementation...
163-
assert.throws(makeBlock(assert.deepEqual, 'a', {}), assert.AssertionError);
175+
test('assert.deepEqual - object wrappers', function () {
176+
assert.doesNotThrow(makeBlock(assert.deepEqual, new String('a'), ['a']));
177+
assert.doesNotThrow(makeBlock(assert.deepEqual, new String('a'), {0: 'a'}));
178+
assert.doesNotThrow(makeBlock(assert.deepEqual, new Number(1), {}));
179+
assert.doesNotThrow(makeBlock(assert.deepEqual, new Boolean(true), {}));
164180
});
165181

166182
function thrower(errorConstructor) {

0 commit comments

Comments
 (0)