Skip to content

Commit

Permalink
[Fix] ensure the shimmed parseInt throws with Symbols
Browse files Browse the repository at this point in the history
Fixes #450.
  • Loading branch information
ljharb committed Dec 9, 2017
1 parent cd5cd68 commit c3296bd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
6 changes: 6 additions & 0 deletions es5-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -2003,6 +2003,12 @@
parseInt = (function (origParseInt) {
var hexRegex = /^[-+]?0[xX]/;
return function parseInt(str, radix) {
if (typeof str === 'symbol') {
// handle Symbols in node 8.3/8.4
// eslint-disable-next-line no-implicit-coercion, no-unused-expressions
'' + str; // jscs:ignore disallowImplicitTypeConversion
}

var string = trim(String(str));
var defaultedRadix = $Number(radix) || (hexRegex.test(string) ? 16 : 10);
return origParseInt(string, defaultedRadix);
Expand Down
7 changes: 7 additions & 0 deletions tests/spec/s-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ describe('global methods', function () {
var foo = function foo() {};
var functionsHaveNames = foo.name === 'foo';
var ifFunctionsHaveNamesIt = functionsHaveNames ? it : xit;
var hasSymbols = typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol';
var ifSymbolsIt = hasSymbols ? it : xit;

var is = function (x, y) {
if (x === 0 && y === 0) {
Expand Down Expand Up @@ -74,6 +76,11 @@ describe('global methods', function () {
expect(parseInt(null)).toBeNaN();
expect(parseInt(NaN)).toBeNaN();
});

ifSymbolsIt('throws on symbols', function () {
expect(function () { parseInt(Symbol('')); }).toThrow();
expect(function () { parseInt(Object(Symbol(''))); }).toThrow();
});
/* eslint-enable radix */
});

Expand Down

0 comments on commit c3296bd

Please sign in to comment.