Skip to content

Commit 8878890

Browse files
committed
Add support for NaN
1 parent 5838b91 commit 8878890

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

index.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var randomBytes = require('randombytes');
1111
// Generate an internal UID to make the regexp pattern harder to guess.
1212
var UID_LENGTH = 16;
1313
var UID = generateUID();
14-
var PLACE_HOLDER_REGEXP = new RegExp('(\\\\)?"@__(F|R|D|M|S|A|U|I|B|L)-' + UID + '-(\\d+)__@"', 'g');
14+
var PLACE_HOLDER_REGEXP = new RegExp('(\\\\)?"@__(F|R|D|M|S|A|U|I|B|L|N)-' + UID + '-(\\d+)__@"', 'g');
1515

1616
var IS_NATIVE_CODE_REGEXP = /\{\s*\[native code\]\s*\}/g;
1717
var IS_PURE_FUNCTION = /function.*?\(/;
@@ -73,6 +73,7 @@ module.exports = function serialize(obj, options) {
7373
var infinities= [];
7474
var bigInts = [];
7575
var urls = [];
76+
var nans = [];
7677

7778
// Returns placeholders for functions and regexps (identified by index)
7879
// which are later replaced by their string representation.
@@ -83,6 +84,10 @@ module.exports = function serialize(obj, options) {
8384
deleteFunctions(value);
8485
}
8586

87+
if (typeof value === 'number' && isNaN(value)) {
88+
return '@__N-' + UID + '-' + (nans.push(value) - 1) + '__@';
89+
}
90+
8691
if (!value && value !== undefined && value !== BigInt(0)) {
8792
return value;
8893
}
@@ -210,7 +215,7 @@ module.exports = function serialize(obj, options) {
210215
str = str.replace(UNSAFE_CHARS_REGEXP, escapeUnsafeChars);
211216
}
212217

213-
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) {
218+
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 && nans.length === 0) {
214219
return str;
215220
}
216221

@@ -261,6 +266,10 @@ module.exports = function serialize(obj, options) {
261266
return "new URL(" + serialize(urls[valueIndex].toString(), options) + ")";
262267
}
263268

269+
if (type === 'N') {
270+
return 'NaN';
271+
}
272+
264273
var fn = functions[valueIndex];
265274

266275
return serializeFunc(fn);

test/unit/serialize.js

+13
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,19 @@ describe('serialize( obj )', function () {
554554
});
555555
});
556556

557+
describe('NaN', function () {
558+
it('should serialize NaN', function () {
559+
expect(serialize(NaN)).to.equal('NaN');
560+
expect(serialize({t: [NaN]})).to.be.a('string').equal('{"t":[NaN]}');
561+
});
562+
563+
it('should deserialize NaN', function () {
564+
var d = eval(serialize(NaN));
565+
expect(d).to.be.a('Number');
566+
expect(isNaN(d)).to.equal(true);
567+
});
568+
});
569+
557570
describe('backwards-compatability', function () {
558571
it('should accept `space` as the second argument', function () {
559572
expect(serialize([1], 0)).to.equal('[1]');

0 commit comments

Comments
 (0)