Skip to content

Commit f63b62b

Browse files
authored
Merge pull request #95 from keithamus/fix/array-tostringtag
fix: favour extended array toStringTag where available
2 parents 0bb99f7 + 18d22c6 commit f63b62b

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ module.exports = function typeDetect(obj) {
9292
* Post:
9393
* array literal x 22,479,650 ops/sec ±0.96% (81 runs sampled)
9494
*/
95-
if (Array.isArray(obj)) {
95+
if (
96+
Array.isArray(obj) &&
97+
(symbolToStringTagExists === false || typeof obj[Symbol.toStringTag] === 'undefined')
98+
) {
9699
return 'Array';
97100
}
98101

test/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,6 @@ describe('Generic', function () {
259259
Object.prototype.toString = originalObjectToString; // eslint-disable-line no-extend-native
260260
});
261261

262-
263262
it('plain object', function () {
264263
var obj = {};
265264
obj[Symbol.toStringTag] = function () {

test/tostringtag-extras.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
var assert = require('simple-assert');
4+
var type = require('..');
5+
var symbolExists = typeof Symbol === 'function';
6+
var symbolToStringTagExists = symbolExists && typeof Symbol.toStringTag !== 'undefined';
7+
function describeIf(condition) {
8+
return condition ? describe : describe.skip;
9+
}
10+
11+
describeIf(symbolToStringTagExists)('toStringTag extras', function () {
12+
13+
it('supports toStringTag on arrays', function () {
14+
assert(type([]) === 'Array');
15+
var arr = [];
16+
arr[Symbol.toStringTag] = 'foo';
17+
assert(type(arr) === 'foo', 'type(arr) === "foo"');
18+
});
19+
20+
21+
});

0 commit comments

Comments
 (0)