Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for NaN #190

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
13 changes: 11 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var randomBytes = require('randombytes');
// 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');

var IS_NATIVE_CODE_REGEXP = /\{\s*\[native code\]\s*\}/g;
var IS_PURE_FUNCTION = /function.*?\(/;
Expand Down Expand Up @@ -73,6 +73,7 @@ module.exports = function serialize(obj, options) {
var infinities= [];
var bigInts = [];
var urls = [];
var nans = [];

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

if (typeof value === 'number' && isNaN(value)) {
return '@__N-' + UID + '-' + (nans.push(value) - 1) + '__@';
}
Comment on lines +87 to +89
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might be better to combine this if statement with the number if statement.

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


if (!value && value !== undefined && value !== BigInt(0)) {
return value;
}
Expand Down Expand Up @@ -210,7 +215,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 && bigInts.length === 0 && urls.length === 0 && nans.length === 0) {
return str;
}

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

if (type === 'N') {
return 'NaN';
}

var fn = functions[valueIndex];

return serializeFunc(fn);
Expand Down
13 changes: 13 additions & 0 deletions test/unit/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,19 @@ describe('serialize( obj )', function () {
});
});

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

it('should deserialize NaN', function () {
var d = eval(serialize(NaN));
expect(d).to.be.a('Number');
expect(isNaN(d)).to.equal(true);
});
});

describe('backwards-compatability', function () {
it('should accept `space` as the second argument', function () {
expect(serialize([1], 0)).to.equal('[1]');
Expand Down