Skip to content

Commit

Permalink
[Fix] Boxed Symbols should be coerced to primitives before testing fo…
Browse files Browse the repository at this point in the history
…r equality.
  • Loading branch information
ljharb committed Sep 27, 2015
1 parent ba4dcee commit 0ed5277
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ var isGenerator = require('is-generator-function');
var isNumber = require('is-number-object');
var isRegex = require('is-regex');
var isString = require('is-string');
var isSymbol = require('is-symbol');
var isCallable = require('is-callable');

var symbolValue = typeof Symbol === 'function' ? Symbol.prototype.valueOf : null;

var getPrototypeOf = Object.getPrototypeOf;
if (!getPrototypeOf) {
/* eslint-disable no-proto */
Expand Down Expand Up @@ -92,6 +95,13 @@ module.exports = function isEqual(value, other) {
return index <= 0;
}

var valueIsSym = isSymbol(value);
var otherIsSym = isSymbol(other);
if (valueIsSym !== otherIsSym) { return false; }
if (valueIsSym && otherIsSym) {
return symbolValue.call(value) === symbolValue.call(other);
}

var valueIsGen = isGenerator(value);
var otherIsGen = isGenerator(other);
if (valueIsGen !== otherIsGen) { return false; }
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"is-generator-function": "^1.0.3",
"is-number-object": "^1.0.3",
"is-regex": "^1.0.3",
"is-string": "^1.0.4"
"is-string": "^1.0.4",
"is-symbol": "^1.0.1"
},
"devDependencies": {
"tape": "^4.2.0",
Expand Down
3 changes: 3 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,11 @@ var hasSymbols = typeof Symbol === 'function' && typeof Symbol('foo') === 'symbo
test('symbols', { skip: !hasSymbols }, function (t) {
var foo = 'foo';
var fooSym = Symbol(foo);
var objectFooSym = Object(fooSym);
t.ok(isEqual(fooSym, fooSym), 'Symbol("foo") is equal to itself');
t.ok(isEqual(fooSym, objectFooSym), 'Symbol("foo") is equal to the object form of itself');
t.notOk(isEqual(Symbol(foo), Symbol(foo)), 'Symbol("foo") is not equal to Symbol("foo"), even when the string is the same instance');
t.notOk(isEqual(Symbol(foo), Object(Symbol(foo))), 'Symbol("foo") is not equal to Object(Symbol("foo")), even when the string is the same instance');

t.end();
});

0 comments on commit 0ed5277

Please sign in to comment.