Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -144,22 +144,23 @@ module.exports = function serialize(obj, options) {
var arrays = [];
var undefs = [];
var infinities= [];
var negativeZeros = [];
var bigInts = [];
var urls = [];

// Returns placeholders for functions and regexps (identified by index)
// 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) + '__@';
Expand Down Expand Up @@ -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) + '__@';
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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] + "\")";
}
Expand Down
23 changes: 23 additions & 0 deletions test/unit/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down