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

fix: handling of negative numbers with non-decimal formatting #73

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
27 changes: 16 additions & 11 deletions jsesc.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,14 @@ const jsesc = (argument, options) => {
return JSON.stringify(Number(argument));
}

let negative = false;
if (argument < 0) {
negative = true;
// Use the BigInt constructor instead of `-1n` so that we still support
// older node versions.
argument *= isBigInt(argument) ? BigInt('-1') : -1;
}

let result;
if (useDecNumbers) {
result = String(argument);
Expand All @@ -226,18 +234,15 @@ const jsesc = (argument, options) => {
}

if (isBigInt(argument)) {
return result + 'n';
result += 'n';
}

if (negative) {
result = '-' + result;
}

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));
}
return argument + 'n';
} else if (!isObject(argument)) {
} else if (!isObject(argument)) {
if (json) {
// For some values (e.g. `undefined`, `function` objects),
// `JSON.stringify(value)` returns `undefined` (which isn’t valid
Expand Down Expand Up @@ -332,6 +337,6 @@ const jsesc = (argument, options) => {
return result;
};

jsesc.version = '3.0.2';
jsesc.version = '3.1.0';

module.exports = jsesc;
25 changes: 15 additions & 10 deletions src/jsesc.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,14 @@ const jsesc = (argument, options) => {
return JSON.stringify(Number(argument));
}

let negative = false;
if (argument < 0) {
negative = true;
// Use the BigInt constructor instead of `-1n` so that we still support
// older node versions.
argument *= isBigInt(argument) ? BigInt('-1') : -1;
}

let result;
if (useDecNumbers) {
result = String(argument);
Expand All @@ -226,18 +234,15 @@ const jsesc = (argument, options) => {
}

if (isBigInt(argument)) {
return result + 'n';
result += 'n';
}

if (negative) {
result = '-' + result;
}

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));
}
return argument + 'n';
} else if (!isObject(argument)) {
} else if (!isObject(argument)) {
if (json) {
// For some values (e.g. `undefined`, `function` objects),
// `JSON.stringify(value)` returns `undefined` (which isn’t valid
Expand Down
16 changes: 10 additions & 6 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ describe('common usage', function() {
assert.equal(
jsesc([
0x42,
-0x42,
0x1337,
50n,
100n,
Expand All @@ -153,12 +154,13 @@ describe('common usage', function() {
], {
'numbers': 'binary'
}),
'[0b1000010,0b1001100110111,0b110010n,0b1100100n,0b-1101111010000001111001000000101000000011010010111100111101001111111110000000011101111111011111101010011001011110010110001101000100001011101010001110011010101110001101101010100101000000010100101100100010111010100011101101101010111011011011000110010010000100001111100011110111010001100010100110000111110001000110111100101100001001110011001111011110010010100100101110001001111001000011010110000111011110001000111111001011101000101000111111100011110000111011101000010000001100001110000010110100110000101011001100000001010100100101111010111001011100111001111111000101000110101111001010010001010001111000101010111111101110000111001101101100000100001100000110000110010001110010101111100000010011110001000100001101010010010010010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000n,0b1101111010000001111001000000101000000011010010111100111101001111111110000000011101111111011111101010011001011110010110001101000100001011101010001110011010101110001101101010100101000000010100101100100010111010100011101101101010111011011011000110010010000100001111100011110111010001100010100110000111110001000110111100101100001001110011001111011110010010100100101110001001111001000011010110000111011110001000111111001011101000101000111111100011110000111011101000010000001100001110000010110100110000101011001100000001010100100101111010111001011100111001111111000101000110101111001010010001010001111000101010111111101110000111001101101100000100001100000110000110010001110010101111100000010011110001000100001101010010010010010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000n]',
'[0b1000010,-0b1000010,0b1001100110111,0b110010n,0b1100100n,-0b1101111010000001111001000000101000000011010010111100111101001111111110000000011101111111011111101010011001011110010110001101000100001011101010001110011010101110001101101010100101000000010100101100100010111010100011101101101010111011011011000110010010000100001111100011110111010001100010100110000111110001000110111100101100001001110011001111011110010010100100101110001001111001000011010110000111011110001000111111001011101000101000111111100011110000111011101000010000001100001110000010110100110000101011001100000001010100100101111010111001011100111001111111000101000110101111001010010001010001111000101010111111101110000111001101101100000100001100000110000110010001110010101111100000010011110001000100001101010010010010010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000n,0b1101111010000001111001000000101000000011010010111100111101001111111110000000011101111111011111101010011001011110010110001101000100001011101010001110011010101110001101101010100101000000010100101100100010111010100011101101101010111011011011000110010010000100001111100011110111010001100010100110000111110001000110111100101100001001110011001111011110010010100100101110001001111001000011010110000111011110001000111111001011101000101000111111100011110000111011101000010000001100001110000010110100110000101011001100000001010100100101111010111001011100111001111111000101000110101111001010010001010001111000101010111111101110000111001101101100000100001100000110000110010001110010101111100000010011110001000100001101010010010010010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000n]',
'`numbers: \'binary\'`'
);
assert.equal(
jsesc([
0x42,
-0x42,
0x1337,
50n,
100n,
Expand All @@ -172,12 +174,13 @@ describe('common usage', function() {
'numbers': 'binary',
'json': true
}),
'[66,4919,50,100,null,null,null,null]',
'[66,-66,4919,50,100,null,null,null,null]',
'`json: true` takes precedence over `numbers: \'binary\'`'
);
assert.equal(
jsesc([
0x42,
-0x42,
0x1337,
50n,
100n,
Expand All @@ -188,12 +191,13 @@ describe('common usage', function() {
], {
'numbers': 'octal'
}),
'[0o102,0o11467,0o62n,0o144n,0o-15720171005003227475177600357737523136261504135216325615524500245442724355527333062204174367214246076106745411631736224456117103260736107713505077436073502014160264605314012445727134717705065712212170527756071554041406062162574023610415222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000n,0o15720171005003227475177600357737523136261504135216325615524500245442724355527333062204174367214246076106745411631736224456117103260736107713505077436073502014160264605314012445727134717705065712212170527756071554041406062162574023610415222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000n]',
'[0o102,-0o102,0o11467,0o62n,0o144n,-0o15720171005003227475177600357737523136261504135216325615524500245442724355527333062204174367214246076106745411631736224456117103260736107713505077436073502014160264605314012445727134717705065712212170527756071554041406062162574023610415222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000n,0o15720171005003227475177600357737523136261504135216325615524500245442724355527333062204174367214246076106745411631736224456117103260736107713505077436073502014160264605314012445727134717705065712212170527756071554041406062162574023610415222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000n]',
'`numbers: \'octal\'`'
);
assert.equal(
jsesc([
0x42,
-0x42,
0x1337,
50n,
100n,
Expand All @@ -204,7 +208,7 @@ describe('common usage', function() {
], {
'numbers': 'hexadecimal'
}),
'[0x42,0x1337,0x32n,0x64n,0x-37A0790280D2F3D3FE01DFDFA997963442EA39AB8DAA5014B22EA3B6AEDB19210F8F7462987C46F2C2733DE4A4B89E43587788FCBA28FE3C3BA1030E0B4C2B301525EB9739FC51AF291478ABFB8736C10C186472BE04F110D492400000000000000000000000000000000000000000000000000000000000000000000000000000n,0x37A0790280D2F3D3FE01DFDFA997963442EA39AB8DAA5014B22EA3B6AEDB19210F8F7462987C46F2C2733DE4A4B89E43587788FCBA28FE3C3BA1030E0B4C2B301525EB9739FC51AF291478ABFB8736C10C186472BE04F110D492400000000000000000000000000000000000000000000000000000000000000000000000000000n]',
'[0x42,-0x42,0x1337,0x32n,0x64n,-0x37A0790280D2F3D3FE01DFDFA997963442EA39AB8DAA5014B22EA3B6AEDB19210F8F7462987C46F2C2733DE4A4B89E43587788FCBA28FE3C3BA1030E0B4C2B301525EB9739FC51AF291478ABFB8736C10C186472BE04F110D492400000000000000000000000000000000000000000000000000000000000000000000000000000n,0x37A0790280D2F3D3FE01DFDFA997963442EA39AB8DAA5014B22EA3B6AEDB19210F8F7462987C46F2C2733DE4A4B89E43587788FCBA28FE3C3BA1030E0B4C2B301525EB9739FC51AF291478ABFB8736C10C186472BE04F110D492400000000000000000000000000000000000000000000000000000000000000000000000000000n]',
'`numbers: \'hexadecimal\'`'
);
assert.equal(
Expand Down Expand Up @@ -528,11 +532,11 @@ describe('common usage', function() {
'`toJSON` methods are not called when `json: false`'
);
assert.equal(
jsesc([42, 42n], {
jsesc([42, 42n, -42, -42n], {
'numbers': 'hexadecimal',
'lowercaseHex': true
}),
'[0x2a,0x2an]',
'[0x2a,0x2an,-0x2a,-0x2an]',
'Hexadecimal integeral literals are lowercase when `lowercaseHex: true`'
);
assert.equal(
Expand Down