diff --git a/lib/serializer.js b/lib/serializer.js index 387e8927..1ad588a1 100644 --- a/lib/serializer.js +++ b/lib/serializer.js @@ -1,7 +1,7 @@ 'use strict' // eslint-disable-next-line -const STR_ESCAPE = /[\u0000-\u001f\u0022\u005c\ud800-\udfff]|[\ud800-\udbff](?![\udc00-\udfff])|(?:[^\ud800-\udbff]|^)[\udc00-\udfff]/ +const STR_ESCAPE = /[\u0000-\u001f\u0022\u005c\ud800-\udfff]/ module.exports = class Serializer { constructor (options) { @@ -25,13 +25,12 @@ module.exports = class Serializer { asInteger (i) { if (typeof i === 'number') { - if (i === Infinity || i === -Infinity) { - throw new Error(`The value "${i}" cannot be converted to an integer.`) - } if (Number.isInteger(i)) { return '' + i } - if (Number.isNaN(i)) { + // check if number is Infinity or NaN + // eslint-disable-next-line no-self-compare + if (i === Infinity || i === -Infinity || i !== i) { throw new Error(`The value "${i}" cannot be converted to an integer.`) } return this.parseInteger(i) @@ -52,7 +51,9 @@ module.exports = class Serializer { asNumber (i) { const num = Number(i) - if (Number.isNaN(num)) { + // check if number is NaN + // eslint-disable-next-line no-self-compare + if (num !== num) { throw new Error(`The value "${i}" cannot be converted to a number.`) } else if (!Number.isFinite(num)) { return 'null' @@ -128,10 +129,6 @@ module.exports = class Serializer { // eslint-disable-next-line for (var i = 0; i < len; i++) { point = str.charCodeAt(i) - if (point < 32 || (point >= 0xD800 && point <= 0xDFFF)) { - // The current character is non-printable characters or a surrogate. - return JSON.stringify(str) - } if ( point === 0x22 || // '"' point === 0x5c // '\' @@ -139,6 +136,9 @@ module.exports = class Serializer { last === -1 && (last = 0) result += str.slice(last, i) + '\\' last = i + } else if (point < 32 || (point >= 0xD800 && point <= 0xDFFF)) { + // The current character is non-printable characters or a surrogate. + return JSON.stringify(str) } } diff --git a/package.json b/package.json index 7bf62f94..27c7d017 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fast-json-stringify", - "version": "5.13.0", + "version": "5.14.0", "description": "Stringify your JSON at max speed", "main": "index.js", "type": "commonjs", @@ -46,13 +46,13 @@ "simple-git": "^3.7.1", "standard": "^17.0.0", "tap": "^16.0.1", - "tsd": "^0.30.3", + "tsd": "^0.31.0", "webpack": "^5.40.0", "fast-json-stringify": "." }, "dependencies": { "ajv": "^8.10.0", - "ajv-formats": "^2.1.1", + "ajv-formats": "^3.0.1", "fast-deep-equal": "^3.1.3", "fast-uri": "^2.1.0", "rfdc": "^1.2.0",