Skip to content

Commit

Permalink
fix: favor extended array toStringTag where available
Browse files Browse the repository at this point in the history
  • Loading branch information
dvlsg authored and keithamus committed Apr 18, 2017
1 parent 0bb99f7 commit 18d22c6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

Expand Down
1 change: 0 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
21 changes: 21 additions & 0 deletions test/tostringtag-extras.js
Original file line number Diff line number Diff line change
@@ -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"');
});


});

0 comments on commit 18d22c6

Please sign in to comment.