lib: move format and formatWithOptions into internal/util/inspect.js
So these can be required without requiring the whole `util.js`. PR-URL: https://github.com/nodejs/node/pull/26468 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
parent
f617a732f8
commit
21440c8acd
@ -1363,8 +1363,139 @@ function reduceToSingleString(ctx, output, base, braces, combine = false) {
|
||||
return `${braces[0]}${ln}${join(output, `,\n${indentation} `)} ${braces[1]}`;
|
||||
}
|
||||
|
||||
const emptyOptions = {};
|
||||
function format(...args) {
|
||||
return formatWithOptions(emptyOptions, ...args);
|
||||
}
|
||||
|
||||
|
||||
let CIRCULAR_ERROR_MESSAGE;
|
||||
function tryStringify(arg) {
|
||||
try {
|
||||
return JSON.stringify(arg);
|
||||
} catch (err) {
|
||||
// Populate the circular error message lazily
|
||||
if (!CIRCULAR_ERROR_MESSAGE) {
|
||||
try {
|
||||
const a = {}; a.a = a; JSON.stringify(a);
|
||||
} catch (err) {
|
||||
CIRCULAR_ERROR_MESSAGE = err.message;
|
||||
}
|
||||
}
|
||||
if (err.name === 'TypeError' && err.message === CIRCULAR_ERROR_MESSAGE)
|
||||
return '[Circular]';
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
function formatWithOptions(inspectOptions, ...args) {
|
||||
const first = args[0];
|
||||
let a = 0;
|
||||
let str = '';
|
||||
let join = '';
|
||||
|
||||
if (typeof first === 'string') {
|
||||
if (args.length === 1) {
|
||||
return first;
|
||||
}
|
||||
let tempStr;
|
||||
let lastPos = 0;
|
||||
|
||||
for (var i = 0; i < first.length - 1; i++) {
|
||||
if (first.charCodeAt(i) === 37) { // '%'
|
||||
const nextChar = first.charCodeAt(++i);
|
||||
if (a + 1 !== args.length) {
|
||||
switch (nextChar) {
|
||||
case 115: // 's'
|
||||
tempStr = String(args[++a]);
|
||||
break;
|
||||
case 106: // 'j'
|
||||
tempStr = tryStringify(args[++a]);
|
||||
break;
|
||||
case 100: // 'd'
|
||||
const tempNum = args[++a];
|
||||
// eslint-disable-next-line valid-typeof
|
||||
if (typeof tempNum === 'bigint') {
|
||||
tempStr = `${tempNum}n`;
|
||||
} else if (typeof tempNum === 'symbol') {
|
||||
tempStr = 'NaN';
|
||||
} else {
|
||||
tempStr = `${Number(tempNum)}`;
|
||||
}
|
||||
break;
|
||||
case 79: // 'O'
|
||||
tempStr = inspect(args[++a], inspectOptions);
|
||||
break;
|
||||
case 111: // 'o'
|
||||
{
|
||||
tempStr = inspect(args[++a], {
|
||||
...inspectOptions,
|
||||
showHidden: true,
|
||||
showProxy: true,
|
||||
depth: 4
|
||||
});
|
||||
break;
|
||||
}
|
||||
case 105: // 'i'
|
||||
const tempInteger = args[++a];
|
||||
// eslint-disable-next-line valid-typeof
|
||||
if (typeof tempInteger === 'bigint') {
|
||||
tempStr = `${tempInteger}n`;
|
||||
} else if (typeof tempInteger === 'symbol') {
|
||||
tempStr = 'NaN';
|
||||
} else {
|
||||
tempStr = `${parseInt(tempInteger)}`;
|
||||
}
|
||||
break;
|
||||
case 102: // 'f'
|
||||
const tempFloat = args[++a];
|
||||
if (typeof tempFloat === 'symbol') {
|
||||
tempStr = 'NaN';
|
||||
} else {
|
||||
tempStr = `${parseFloat(tempFloat)}`;
|
||||
}
|
||||
break;
|
||||
case 37: // '%'
|
||||
str += first.slice(lastPos, i);
|
||||
lastPos = i + 1;
|
||||
continue;
|
||||
default: // Any other character is not a correct placeholder
|
||||
continue;
|
||||
}
|
||||
if (lastPos !== i - 1) {
|
||||
str += first.slice(lastPos, i - 1);
|
||||
}
|
||||
str += tempStr;
|
||||
lastPos = i + 1;
|
||||
} else if (nextChar === 37) {
|
||||
str += first.slice(lastPos, i);
|
||||
lastPos = i + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (lastPos !== 0) {
|
||||
a++;
|
||||
join = ' ';
|
||||
if (lastPos < first.length) {
|
||||
str += first.slice(lastPos);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while (a < args.length) {
|
||||
const value = args[a];
|
||||
str += join;
|
||||
str += typeof value !== 'string' ? inspect(value, inspectOptions) : value;
|
||||
join = ' ';
|
||||
a++;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
inspect,
|
||||
formatProperty,
|
||||
kObjectType
|
||||
kObjectType,
|
||||
format,
|
||||
formatWithOptions
|
||||
};
|
||||
|
134
lib/util.js
134
lib/util.js
@ -22,7 +22,11 @@
|
||||
'use strict';
|
||||
|
||||
const errors = require('internal/errors');
|
||||
const { inspect } = require('internal/util/inspect');
|
||||
const {
|
||||
format,
|
||||
formatWithOptions,
|
||||
inspect
|
||||
} = require('internal/util/inspect');
|
||||
const {
|
||||
ERR_FALSY_VALUE_REJECTION,
|
||||
ERR_INVALID_ARG_TYPE,
|
||||
@ -46,136 +50,8 @@ function uncurryThis(func) {
|
||||
}
|
||||
const objectToString = uncurryThis(Object.prototype.toString);
|
||||
|
||||
let CIRCULAR_ERROR_MESSAGE;
|
||||
let internalDeepEqual;
|
||||
|
||||
function tryStringify(arg) {
|
||||
try {
|
||||
return JSON.stringify(arg);
|
||||
} catch (err) {
|
||||
// Populate the circular error message lazily
|
||||
if (!CIRCULAR_ERROR_MESSAGE) {
|
||||
try {
|
||||
const a = {}; a.a = a; JSON.stringify(a);
|
||||
} catch (err) {
|
||||
CIRCULAR_ERROR_MESSAGE = err.message;
|
||||
}
|
||||
}
|
||||
if (err.name === 'TypeError' && err.message === CIRCULAR_ERROR_MESSAGE)
|
||||
return '[Circular]';
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
const emptyOptions = {};
|
||||
function format(...args) {
|
||||
return formatWithOptions(emptyOptions, ...args);
|
||||
}
|
||||
|
||||
function formatWithOptions(inspectOptions, ...args) {
|
||||
const first = args[0];
|
||||
let a = 0;
|
||||
let str = '';
|
||||
let join = '';
|
||||
|
||||
if (typeof first === 'string') {
|
||||
if (args.length === 1) {
|
||||
return first;
|
||||
}
|
||||
let tempStr;
|
||||
let lastPos = 0;
|
||||
|
||||
for (var i = 0; i < first.length - 1; i++) {
|
||||
if (first.charCodeAt(i) === 37) { // '%'
|
||||
const nextChar = first.charCodeAt(++i);
|
||||
if (a + 1 !== args.length) {
|
||||
switch (nextChar) {
|
||||
case 115: // 's'
|
||||
tempStr = String(args[++a]);
|
||||
break;
|
||||
case 106: // 'j'
|
||||
tempStr = tryStringify(args[++a]);
|
||||
break;
|
||||
case 100: // 'd'
|
||||
const tempNum = args[++a];
|
||||
// eslint-disable-next-line valid-typeof
|
||||
if (typeof tempNum === 'bigint') {
|
||||
tempStr = `${tempNum}n`;
|
||||
} else if (typeof tempNum === 'symbol') {
|
||||
tempStr = 'NaN';
|
||||
} else {
|
||||
tempStr = `${Number(tempNum)}`;
|
||||
}
|
||||
break;
|
||||
case 79: // 'O'
|
||||
tempStr = inspect(args[++a], inspectOptions);
|
||||
break;
|
||||
case 111: // 'o'
|
||||
{
|
||||
tempStr = inspect(args[++a], {
|
||||
...inspectOptions,
|
||||
showHidden: true,
|
||||
showProxy: true,
|
||||
depth: 4
|
||||
});
|
||||
break;
|
||||
}
|
||||
case 105: // 'i'
|
||||
const tempInteger = args[++a];
|
||||
// eslint-disable-next-line valid-typeof
|
||||
if (typeof tempInteger === 'bigint') {
|
||||
tempStr = `${tempInteger}n`;
|
||||
} else if (typeof tempInteger === 'symbol') {
|
||||
tempStr = 'NaN';
|
||||
} else {
|
||||
tempStr = `${parseInt(tempInteger)}`;
|
||||
}
|
||||
break;
|
||||
case 102: // 'f'
|
||||
const tempFloat = args[++a];
|
||||
if (typeof tempFloat === 'symbol') {
|
||||
tempStr = 'NaN';
|
||||
} else {
|
||||
tempStr = `${parseFloat(tempFloat)}`;
|
||||
}
|
||||
break;
|
||||
case 37: // '%'
|
||||
str += first.slice(lastPos, i);
|
||||
lastPos = i + 1;
|
||||
continue;
|
||||
default: // Any other character is not a correct placeholder
|
||||
continue;
|
||||
}
|
||||
if (lastPos !== i - 1) {
|
||||
str += first.slice(lastPos, i - 1);
|
||||
}
|
||||
str += tempStr;
|
||||
lastPos = i + 1;
|
||||
} else if (nextChar === 37) {
|
||||
str += first.slice(lastPos, i);
|
||||
lastPos = i + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (lastPos !== 0) {
|
||||
a++;
|
||||
join = ' ';
|
||||
if (lastPos < first.length) {
|
||||
str += first.slice(lastPos);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while (a < args.length) {
|
||||
const value = args[a];
|
||||
str += join;
|
||||
str += typeof value !== 'string' ? inspect(value, inspectOptions) : value;
|
||||
join = ' ';
|
||||
a++;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
const debugs = {};
|
||||
let debugEnvRegex = /^$/;
|
||||
if (process.env.NODE_DEBUG) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user