From 18d22c6dbce69a43eb19f753587295ada66d2096 Mon Sep 17 00:00:00 2001 From: Dave Lesage Date: Thu, 14 Jul 2016 18:34:41 -0700 Subject: [PATCH] fix: favor extended array toStringTag where available --- index.js | 5 ++++- test/index.js | 1 - test/tostringtag-extras.js | 21 +++++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 test/tostringtag-extras.js diff --git a/index.js b/index.js index c52cac8..df5ff81 100644 --- a/index.js +++ b/index.js @@ -92,7 +92,10 @@ module.exports = function typeDetect(obj) { * Post: * array literal x 22,479,650 ops/sec ±0.96% (81 runs sampled) */ - if (Array.isArray(obj)) { + if ( + Array.isArray(obj) && + (symbolToStringTagExists === false || typeof obj[Symbol.toStringTag] === 'undefined') + ) { return 'Array'; } diff --git a/test/index.js b/test/index.js index 2b09569..d8d7d08 100644 --- a/test/index.js +++ b/test/index.js @@ -259,7 +259,6 @@ describe('Generic', function () { Object.prototype.toString = originalObjectToString; // eslint-disable-line no-extend-native }); - it('plain object', function () { var obj = {}; obj[Symbol.toStringTag] = function () { diff --git a/test/tostringtag-extras.js b/test/tostringtag-extras.js new file mode 100644 index 0000000..3d7748a --- /dev/null +++ b/test/tostringtag-extras.js @@ -0,0 +1,21 @@ +'use strict'; + +var assert = require('simple-assert'); +var type = require('..'); +var symbolExists = typeof Symbol === 'function'; +var symbolToStringTagExists = symbolExists && typeof Symbol.toStringTag !== 'undefined'; +function describeIf(condition) { + return condition ? describe : describe.skip; +} + +describeIf(symbolToStringTagExists)('toStringTag extras', function () { + + it('supports toStringTag on arrays', function () { + assert(type([]) === 'Array'); + var arr = []; + arr[Symbol.toStringTag] = 'foo'; + assert(type(arr) === 'foo', 'type(arr) === "foo"'); + }); + + +});