Comparing version
43
jsesc.js
@@ -58,2 +58,5 @@ 'use strict'; | ||
}; | ||
const isBigInt = (value) => { | ||
return typeof value == 'bigint'; | ||
}; | ||
const isFunction = (value) => { | ||
@@ -201,11 +204,16 @@ return typeof value == 'function'; | ||
(compact ? '' : oldIndent) + ']'; | ||
} else if (isNumber(argument)) { | ||
} else if (isNumber(argument) || isBigInt(argument)) { | ||
if (json) { | ||
// Some number values (e.g. `Infinity`) cannot be represented in JSON. | ||
return JSON.stringify(argument); | ||
// `BigInt` values less than `-Number.MAX_VALUE` or greater than | ||
// `Number.MAX_VALUE` cannot be represented in JSON so they will become | ||
// `-Infinity` or `Infinity`, respectively, and then become `null` when | ||
// stringified. | ||
return JSON.stringify(Number(argument)); | ||
} | ||
let result; | ||
if (useDecNumbers) { | ||
return String(argument); | ||
} | ||
if (useHexNumbers) { | ||
result = String(argument); | ||
} else if (useHexNumbers) { | ||
let hexadecimal = argument.toString(16); | ||
@@ -215,11 +223,22 @@ if (!lowercaseHex) { | ||
} | ||
return '0x' + hexadecimal; | ||
result = '0x' + hexadecimal; | ||
} else if (useBinNumbers) { | ||
result = '0b' + argument.toString(2); | ||
} else if (useOctNumbers) { | ||
result = '0o' + argument.toString(8); | ||
} | ||
if (useBinNumbers) { | ||
return '0b' + argument.toString(2); | ||
if (isBigInt(argument)) { | ||
return result + 'n'; | ||
} | ||
return result; | ||
} else if (isBigInt(argument)) { | ||
if (json) { | ||
// `BigInt` values less than `-Number.MAX_VALUE` or greater than | ||
// `Number.MAX_VALUE` will become `-Infinity` or `Infinity`, | ||
// respectively, and cannot be represented in JSON. | ||
return JSON.stringify(Number(argument)); | ||
} | ||
if (useOctNumbers) { | ||
return '0o' + argument.toString(8); | ||
} | ||
} else if (!isObject(argument)) { | ||
return argument + 'n'; | ||
} else if (!isObject(argument)) { | ||
if (json) { | ||
@@ -226,0 +245,0 @@ // For some values (e.g. `undefined`, `function` objects), |
{ | ||
"name": "jsesc", | ||
"version": "3.0.2", | ||
"version": "3.1.0", | ||
"description": "Given some data, jsesc returns the shortest possible stringified & ASCII-safe representation of that data.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://mths.be/jsesc", |
@@ -1,2 +0,2 @@ | ||
# jsesc [](https://travis-ci.org/mathiasbynens/jsesc) [](https://coveralls.io/r/mathiasbynens/jsesc) | ||
# jsesc | ||
@@ -3,0 +3,0 @@ Given some data, _jsesc_ returns a stringified representation of that data. jsesc is similar to `JSON.stringify()` except: |
32280
1.75%311
5.78%