Skip to content

Commit

Permalink
[Fix] Number: Make sure string values are trimmed before attempting…
Browse files Browse the repository at this point in the history
… to parse.
  • Loading branch information
ljharb committed Nov 13, 2015
1 parent 3cfcb22 commit be78c15
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
3 changes: 1 addition & 2 deletions es6-shim.js
Expand Up @@ -1179,14 +1179,13 @@
var NumberShim = function Number(value) {
var primValue = Type.primitive(value) ? value : toPrimitive(value, 'number');
if (typeof primValue === 'string') {
primValue = _call(trimShim, primValue);
if (isBinary(primValue)) {
primValue = parseInt(_strSlice(primValue, 2), 2);
} else if (isOctal(primValue)) {
primValue = parseInt(_strSlice(primValue, 2), 8);
} else if (hasNonWS(primValue) || isBadHex(primValue)) {
primValue = NaN;
} else {
primValue = _call(trimShim, primValue);
}
}
var receiver = this;
Expand Down
9 changes: 9 additions & 0 deletions test/number.js
Expand Up @@ -380,8 +380,17 @@ describe('Number', function () {

it('works with binary literals in string form', function () {
expect(Number('0b1')).to.equal(1);
expect(Number(' 0b1')).to.equal(1);
expect(Number('0b1 ')).to.equal(1);

expect(Number('0b10')).to.equal(2);
expect(Number(' 0b10')).to.equal(2);
expect(Number('0b10 ')).to.equal(2);

expect(Number('0b11')).to.equal(3);
expect(Number(' 0b11')).to.equal(3);
expect(Number('0b11 ')).to.equal(3);

expect(Number({ toString: function () { return '0b100'; }, valueOf: function () { return '0b101'; } })).to.equal(5);
});

Expand Down

0 comments on commit be78c15

Please sign in to comment.