Skip to content

Commit

Permalink
[Fix] add awareness of Symbol.toStringTag
Browse files Browse the repository at this point in the history
Fixes #20
  • Loading branch information
ljharb committed Nov 2, 2018
1 parent bf8d275 commit 59ebcba
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ assert.equal(isArguments([]), false);
}())
```

## Caveats
If you have modified an actual `arguments` object by giving it a `Symbol.toStringTag` property, then this package will return `false`.

## Tests
Simply clone the repo, `npm install`, and run `npm test`

Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
'use strict';

var hasToStringTag = typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol';
var toStr = Object.prototype.toString;

var isStandardArguments = function isArguments(value) {
if (hasToStringTag && value && Symbol.toStringTag in value) {
return false;
}
return toStr.call(value) === '[object Arguments]';
};

Expand Down
15 changes: 15 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var test = require('tape');
var isArguments = require('./');
var hasToStringTag = typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol';

test('primitives', function (t) {
t.notOk(isArguments([]), 'array is not arguments');
Expand All @@ -26,3 +27,17 @@ test('old-style arguments object', function (t) {
t.ok(isLegacyArguments(fakeOldArguments), 'old-style arguments is arguments');
t.end();
});

test('Symbol.toStringTag', { skip: !hasToStringTag }, function (t) {
var obj = {};
obj[Symbol.toStringTag] = 'Arguments';
t.notOk(isArguments(obj), 'object with faked toStringTag is not arguments');

var args = (function () {
return arguments;
}());
args[Symbol.toStringTag] = 'Arguments';
t.notOk(isArguments(obj), 'real arguments with faked toStringTag is not arguments');

t.end();
});

0 comments on commit 59ebcba

Please sign in to comment.