Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: favour extended array toStringTag where available #95

Merged
merged 1 commit into from
Apr 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
Copy link
Contributor

@shvaikalesh shvaikalesh Apr 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer

!(Symbol.toStringTag in obj)

Does not call getter and looks nicer.

) {
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"');
});


});