diff --git a/index.js b/index.js index 60186e3..6a655d1 100644 --- a/index.js +++ b/index.js @@ -9,7 +9,7 @@ See the accompanying LICENSE file for terms. // Generate an internal UID to make the regexp pattern harder to guess. var UID_LENGTH = 16; var UID = generateUID(); -var PLACE_HOLDER_REGEXP = new RegExp('(\\\\)?"@__(F|R|D|M|S|A|U|I|B|L)-' + UID + '-(\\d+)__@"', 'g'); +var PLACE_HOLDER_REGEXP = new RegExp('(\\\\)?"@__(F|R|D|M|S|A|U|I|B|L|N)-' + UID + '-(\\d+)__@"', 'g'); // Not global: `.test()` on a `/g` regexp advances `lastIndex`, so a second // call could start past a `[native code]` match and wrongly report a native @@ -144,6 +144,7 @@ module.exports = function serialize(obj, options) { var arrays = []; var undefs = []; var infinities= []; + var negativeZeros = []; var bigInts = []; var urls = []; @@ -151,15 +152,15 @@ module.exports = function serialize(obj, options) { // which are later replaced by their string representation. function replacer(key, value) { - if (!value && value !== undefined && value !== BigInt(0)) { - return value; - } - // If the value is an object w/ a toJSON method, toJSON is called before // the replacer runs, so we use this[key] to get the non-toJSONed value. var origValue = this[key]; var type = typeof origValue; + if (!value && value !== undefined && value !== BigInt(0) && !Object.is(origValue, -0)) { + return value; + } + if (type === 'object') { if(origValue instanceof RegExp) { return '@__R-' + UID + '-' + (regexps.push(origValue) - 1) + '__@'; @@ -205,6 +206,10 @@ module.exports = function serialize(obj, options) { return '@__I-' + UID + '-' + (infinities.push(origValue) - 1) + '__@'; } + if (type === 'number' && Object.is(origValue, -0)) { + return '@__N-' + UID + '-' + (negativeZeros.push(origValue) - 1) + '__@'; + } + if (type === 'bigint') { return '@__B-' + UID + '-' + (bigInts.push(origValue) - 1) + '__@'; } @@ -295,7 +300,7 @@ module.exports = function serialize(obj, options) { str = str.replace(UNSAFE_CHARS_REGEXP, escapeUnsafeChars); } - if (functions.length === 0 && regexps.length === 0 && dates.length === 0 && maps.length === 0 && sets.length === 0 && arrays.length === 0 && undefs.length === 0 && infinities.length === 0 && bigInts.length === 0 && urls.length === 0) { + if (functions.length === 0 && regexps.length === 0 && dates.length === 0 && maps.length === 0 && sets.length === 0 && arrays.length === 0 && undefs.length === 0 && infinities.length === 0 && negativeZeros.length === 0 && bigInts.length === 0 && urls.length === 0) { return str; } @@ -349,6 +354,10 @@ module.exports = function serialize(obj, options) { return infinities[valueIndex]; } + if (type === 'N') { + return '-0'; + } + if (type === 'B') { return "BigInt(\"" + bigInts[valueIndex] + "\")"; } diff --git a/test/unit/serialize.js b/test/unit/serialize.js index 1b10700..f950ad1 100644 --- a/test/unit/serialize.js +++ b/test/unit/serialize.js @@ -570,6 +570,29 @@ describe('serialize( obj )', function () { }); }); + describe('negative zero', function () { + it('should serialize -0', function () { + strictEqual(serialize(-0), '-0'); + strictEqual(serialize({ t: -0 }), '{"t":-0}'); + strictEqual(serialize({ t: [-0, 0] }), '{"t":[-0,0]}'); + }); + + it('should deserialize -0', function () { + var d = eval('(' + serialize(-0) + ')'); + strictEqual(Object.is(d, -0), true); + var o = eval('(' + serialize({ t: -0 }) + ')'); + strictEqual(Object.is(o.t, -0), true); + var a = eval('(' + serialize({ t: [-0, 0] }) + ')'); + strictEqual(Object.is(a.t[0], -0), true); + strictEqual(Object.is(a.t[1], 0), true); + }); + + it('should not change the sign of regular zero', function () { + strictEqual(serialize(0), '0'); + strictEqual(Object.is(eval(serialize(0)), 0), true); + }); + }); + describe('BigInt', function () { it('should serialize BigInt', function () { var b = BigInt(9999);