Skip to content

Commit

Permalink
Add missing -Infinity support
Browse files Browse the repository at this point in the history
  • Loading branch information
vthibault committed Feb 12, 2020
1 parent 18e2c56 commit 4599ec6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ module.exports = function serialize(obj, options) {
return '@__U-' + UID + '-' + (undefs.push(origValue) - 1) + '__@';
}

if (origValue === Infinity) {
if (type === 'number' && !isNaN(origValue) && !isFinite(origValue)) {
return '@__I-' + UID + '-' + (infinities.push(origValue) - 1) + '__@';
}

Expand Down Expand Up @@ -209,7 +209,7 @@ module.exports = function serialize(obj, options) {
}

if (type === 'I') {
return 'Infinity';
return infinities[valueIndex];
}

var fn = functions[valueIndex];
Expand Down
10 changes: 10 additions & 0 deletions test/unit/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,16 @@ describe('serialize( obj )', function () {
var d = eval(serialize(Infinity));
expect(d).to.equal(Infinity);
});

it('should serialize -Infinity', function () {
expect(serialize(-Infinity)).to.equal('-Infinity');
expect(serialize({t: [-Infinity]})).to.be.a('string').equal('{"t":[-Infinity]}');
});

it('should deserialize -Infinity', function () {
var d = eval(serialize(-Infinity));
expect(d).to.equal(-Infinity);
});
});

describe('XSS', function () {
Expand Down

0 comments on commit 4599ec6

Please sign in to comment.