Skip to content

Commit 0da5088

Browse files
committed
Merge pull request #25 from meeber/symbol-to-string-tag-exists
fix: check if Symbol.toStringTag exists before using
2 parents f2bf640 + ce7ce31 commit 0da5088

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ var setExists = typeof Set !== 'undefined';
1616
var weakMapExists = typeof WeakMap !== 'undefined';
1717
var weakSetExists = typeof WeakSet !== 'undefined';
1818
var dataViewExists = typeof DataView !== 'undefined';
19-
var symbolIteratorExists = symbolExists && Symbol.iterator;
19+
var symbolIteratorExists = symbolExists && typeof Symbol.iterator !== 'undefined';
20+
var symbolToStringTagExists = symbolExists && typeof Symbol.toStringTag !== 'undefined';
2021
var setEntriesExists = setExists && typeof Set.prototype.entries === 'function';
2122
var mapEntriesExists = mapExists && typeof Map.prototype.entries === 'function';
2223
var setIteratorPrototype = getPrototypeOfExists && setEntriesExists && Object.getPrototypeOf(new Set().entries());
@@ -195,7 +196,7 @@ module.exports = function typeDetect(obj) {
195196
}
196197
}
197198

198-
if (getPrototypeOfExists && (symbolExists === false || typeof obj[Symbol.toStringTag] === 'undefined')) {
199+
if (getPrototypeOfExists && (symbolToStringTagExists === false || typeof obj[Symbol.toStringTag] === 'undefined')) {
199200
var objPrototype = Object.getPrototypeOf(obj);
200201
/* ! Speed optimisation
201202
* Pre:

test/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ describe('Generic', function () {
6060
assert(type(Object.create(Object.prototype)) === 'object');
6161
});
6262

63+
// See: https://github.com/chaijs/type-detect/pull/25
64+
it('object with .undefined property getter', function () {
65+
var foo = {};
66+
Object.defineProperty(foo, 'undefined', {
67+
get: function () {
68+
throw Error('Should never happen');
69+
},
70+
});
71+
assert(type(foo) === 'object');
72+
});
73+
6374
it('boolean', function () {
6475
assert(type(true) === 'boolean');
6576
assert(type(false) === 'boolean');

0 commit comments

Comments
 (0)