diff --git a/index.js b/index.js index b0f6abe..841bb7e 100644 --- a/index.js +++ b/index.js @@ -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) + '__@'; } @@ -209,7 +209,7 @@ module.exports = function serialize(obj, options) { } if (type === 'I') { - return 'Infinity'; + return infinities[valueIndex]; } var fn = functions[valueIndex]; diff --git a/test/unit/serialize.js b/test/unit/serialize.js index b160cdc..52e98d4 100644 --- a/test/unit/serialize.js +++ b/test/unit/serialize.js @@ -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 () {