Skip to content

Commit

Permalink
Merge pull request #25 from meeber/symbol-to-string-tag-exists
Browse files Browse the repository at this point in the history
fix: check if Symbol.toStringTag exists before using
  • Loading branch information
lucasfcosta committed May 16, 2016
2 parents f2bf640 + ce7ce31 commit 0da5088
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ var setExists = typeof Set !== 'undefined';
var weakMapExists = typeof WeakMap !== 'undefined';
var weakSetExists = typeof WeakSet !== 'undefined';
var dataViewExists = typeof DataView !== 'undefined';
var symbolIteratorExists = symbolExists && Symbol.iterator;
var symbolIteratorExists = symbolExists && typeof Symbol.iterator !== 'undefined';
var symbolToStringTagExists = symbolExists && typeof Symbol.toStringTag !== 'undefined';
var setEntriesExists = setExists && typeof Set.prototype.entries === 'function';
var mapEntriesExists = mapExists && typeof Map.prototype.entries === 'function';
var setIteratorPrototype = getPrototypeOfExists && setEntriesExists && Object.getPrototypeOf(new Set().entries());
Expand Down Expand Up @@ -195,7 +196,7 @@ module.exports = function typeDetect(obj) {
}
}

if (getPrototypeOfExists && (symbolExists === false || typeof obj[Symbol.toStringTag] === 'undefined')) {
if (getPrototypeOfExists && (symbolToStringTagExists === false || typeof obj[Symbol.toStringTag] === 'undefined')) {
var objPrototype = Object.getPrototypeOf(obj);
/* ! Speed optimisation
* Pre:
Expand Down
11 changes: 11 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ describe('Generic', function () {
assert(type(Object.create(Object.prototype)) === 'object');
});

// See: https://github.com/chaijs/type-detect/pull/25
it('object with .undefined property getter', function () {
var foo = {};
Object.defineProperty(foo, 'undefined', {
get: function () {
throw Error('Should never happen');
},
});
assert(type(foo) === 'object');
});

it('boolean', function () {
assert(type(true) === 'boolean');
assert(type(false) === 'boolean');
Expand Down

0 comments on commit 0da5088

Please sign in to comment.