Skip to content

Commit b75af15

Browse files
joyeecheungtargos
authored andcommitted
lib: move format and formatWithOptions into internal/util/inspect.js
So these can be required without requiring the whole `util.js`. PR-URL: #26468 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent cf1117a commit b75af15

File tree

2 files changed

+137
-131
lines changed

2 files changed

+137
-131
lines changed

lib/internal/util/inspect.js

+132-1
Original file line numberDiff line numberDiff line change
@@ -1354,8 +1354,139 @@ function reduceToSingleString(ctx, output, base, braces, combine = false) {
13541354
return `${braces[0]}${ln}${join(output, `,\n${indentation} `)} ${braces[1]}`;
13551355
}
13561356

1357+
const emptyOptions = {};
1358+
function format(...args) {
1359+
return formatWithOptions(emptyOptions, ...args);
1360+
}
1361+
1362+
let CIRCULAR_ERROR_MESSAGE;
1363+
function tryStringify(arg) {
1364+
try {
1365+
return JSON.stringify(arg);
1366+
} catch (err) {
1367+
// Populate the circular error message lazily
1368+
if (!CIRCULAR_ERROR_MESSAGE) {
1369+
try {
1370+
const a = {}; a.a = a; JSON.stringify(a);
1371+
} catch (err) {
1372+
CIRCULAR_ERROR_MESSAGE = err.message;
1373+
}
1374+
}
1375+
if (err.name === 'TypeError' && err.message === CIRCULAR_ERROR_MESSAGE)
1376+
return '[Circular]';
1377+
throw err;
1378+
}
1379+
}
1380+
1381+
function formatWithOptions(inspectOptions, f) {
1382+
let i, tempStr;
1383+
if (typeof f !== 'string') {
1384+
if (arguments.length === 1) return '';
1385+
let res = '';
1386+
for (i = 1; i < arguments.length - 1; i++) {
1387+
res += inspect(arguments[i], inspectOptions);
1388+
res += ' ';
1389+
}
1390+
res += inspect(arguments[i], inspectOptions);
1391+
return res;
1392+
}
1393+
1394+
if (arguments.length === 2) return f;
1395+
1396+
let str = '';
1397+
let a = 2;
1398+
let lastPos = 0;
1399+
for (i = 0; i < f.length - 1; i++) {
1400+
if (f.charCodeAt(i) === 37) { // '%'
1401+
const nextChar = f.charCodeAt(++i);
1402+
if (a !== arguments.length) {
1403+
switch (nextChar) {
1404+
case 115: // 's'
1405+
tempStr = String(arguments[a++]);
1406+
break;
1407+
case 106: // 'j'
1408+
tempStr = tryStringify(arguments[a++]);
1409+
break;
1410+
case 100: // 'd'
1411+
const tempNum = arguments[a++];
1412+
// eslint-disable-next-line valid-typeof
1413+
if (typeof tempNum === 'bigint') {
1414+
tempStr = `${tempNum}n`;
1415+
} else if (typeof tempNum === 'symbol') {
1416+
tempStr = 'NaN';
1417+
} else {
1418+
tempStr = `${Number(tempNum)}`;
1419+
}
1420+
break;
1421+
case 79: // 'O'
1422+
tempStr = inspect(arguments[a++], inspectOptions);
1423+
break;
1424+
case 111: // 'o'
1425+
{
1426+
const opts = {
1427+
showHidden: true,
1428+
showProxy: true,
1429+
depth: 4,
1430+
...inspectOptions
1431+
};
1432+
tempStr = inspect(arguments[a++], opts);
1433+
break;
1434+
}
1435+
case 105: // 'i'
1436+
const tempInteger = arguments[a++];
1437+
// eslint-disable-next-line valid-typeof
1438+
if (typeof tempInteger === 'bigint') {
1439+
tempStr = `${tempInteger}n`;
1440+
} else if (typeof tempInteger === 'symbol') {
1441+
tempStr = 'NaN';
1442+
} else {
1443+
tempStr = `${parseInt(tempInteger)}`;
1444+
}
1445+
break;
1446+
case 102: // 'f'
1447+
const tempFloat = arguments[a++];
1448+
if (typeof tempFloat === 'symbol') {
1449+
tempStr = 'NaN';
1450+
} else {
1451+
tempStr = `${parseFloat(tempFloat)}`;
1452+
}
1453+
break;
1454+
case 37: // '%'
1455+
str += f.slice(lastPos, i);
1456+
lastPos = i + 1;
1457+
continue;
1458+
default: // Any other character is not a correct placeholder
1459+
continue;
1460+
}
1461+
if (lastPos !== i - 1)
1462+
str += f.slice(lastPos, i - 1);
1463+
str += tempStr;
1464+
lastPos = i + 1;
1465+
} else if (nextChar === 37) {
1466+
str += f.slice(lastPos, i);
1467+
lastPos = i + 1;
1468+
}
1469+
}
1470+
}
1471+
if (lastPos === 0)
1472+
str = f;
1473+
else if (lastPos < f.length)
1474+
str += f.slice(lastPos);
1475+
while (a < arguments.length) {
1476+
const x = arguments[a++];
1477+
if ((typeof x !== 'object' && typeof x !== 'symbol') || x === null) {
1478+
str += ` ${x}`;
1479+
} else {
1480+
str += ` ${inspect(x, inspectOptions)}`;
1481+
}
1482+
}
1483+
return str;
1484+
}
1485+
13571486
module.exports = {
13581487
inspect,
13591488
formatProperty,
1360-
kObjectType
1489+
kObjectType,
1490+
format,
1491+
formatWithOptions
13611492
};

lib/util.js

+5-130
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222
'use strict';
2323

2424
const errors = require('internal/errors');
25-
const { inspect } = require('internal/util/inspect');
25+
const {
26+
format,
27+
formatWithOptions,
28+
inspect
29+
} = require('internal/util/inspect');
2630
const {
2731
ERR_FALSY_VALUE_REJECTION,
2832
ERR_INVALID_ARG_TYPE,
@@ -46,137 +50,8 @@ function uncurryThis(func) {
4650
}
4751
const objectToString = uncurryThis(Object.prototype.toString);
4852

49-
let CIRCULAR_ERROR_MESSAGE;
5053
let internalDeepEqual;
5154

52-
function tryStringify(arg) {
53-
try {
54-
return JSON.stringify(arg);
55-
} catch (err) {
56-
// Populate the circular error message lazily
57-
if (!CIRCULAR_ERROR_MESSAGE) {
58-
try {
59-
const a = {}; a.a = a; JSON.stringify(a);
60-
} catch (err) {
61-
CIRCULAR_ERROR_MESSAGE = err.message;
62-
}
63-
}
64-
if (err.name === 'TypeError' && err.message === CIRCULAR_ERROR_MESSAGE)
65-
return '[Circular]';
66-
throw err;
67-
}
68-
}
69-
70-
const emptyOptions = {};
71-
function format(...args) {
72-
return formatWithOptions(emptyOptions, ...args);
73-
}
74-
75-
function formatWithOptions(inspectOptions, f) {
76-
let i, tempStr;
77-
if (typeof f !== 'string') {
78-
if (arguments.length === 1) return '';
79-
let res = '';
80-
for (i = 1; i < arguments.length - 1; i++) {
81-
res += inspect(arguments[i], inspectOptions);
82-
res += ' ';
83-
}
84-
res += inspect(arguments[i], inspectOptions);
85-
return res;
86-
}
87-
88-
if (arguments.length === 2) return f;
89-
90-
let str = '';
91-
let a = 2;
92-
let lastPos = 0;
93-
for (i = 0; i < f.length - 1; i++) {
94-
if (f.charCodeAt(i) === 37) { // '%'
95-
const nextChar = f.charCodeAt(++i);
96-
if (a !== arguments.length) {
97-
switch (nextChar) {
98-
case 115: // 's'
99-
tempStr = String(arguments[a++]);
100-
break;
101-
case 106: // 'j'
102-
tempStr = tryStringify(arguments[a++]);
103-
break;
104-
case 100: // 'd'
105-
const tempNum = arguments[a++];
106-
// eslint-disable-next-line valid-typeof
107-
if (typeof tempNum === 'bigint') {
108-
tempStr = `${tempNum}n`;
109-
} else if (typeof tempNum === 'symbol') {
110-
tempStr = 'NaN';
111-
} else {
112-
tempStr = `${Number(tempNum)}`;
113-
}
114-
break;
115-
case 79: // 'O'
116-
tempStr = inspect(arguments[a++], inspectOptions);
117-
break;
118-
case 111: // 'o'
119-
{
120-
const opts = {
121-
showHidden: true,
122-
showProxy: true,
123-
depth: 4,
124-
...inspectOptions
125-
};
126-
tempStr = inspect(arguments[a++], opts);
127-
break;
128-
}
129-
case 105: // 'i'
130-
const tempInteger = arguments[a++];
131-
// eslint-disable-next-line valid-typeof
132-
if (typeof tempInteger === 'bigint') {
133-
tempStr = `${tempInteger}n`;
134-
} else if (typeof tempInteger === 'symbol') {
135-
tempStr = 'NaN';
136-
} else {
137-
tempStr = `${parseInt(tempInteger)}`;
138-
}
139-
break;
140-
case 102: // 'f'
141-
const tempFloat = arguments[a++];
142-
if (typeof tempFloat === 'symbol') {
143-
tempStr = 'NaN';
144-
} else {
145-
tempStr = `${parseFloat(tempFloat)}`;
146-
}
147-
break;
148-
case 37: // '%'
149-
str += f.slice(lastPos, i);
150-
lastPos = i + 1;
151-
continue;
152-
default: // Any other character is not a correct placeholder
153-
continue;
154-
}
155-
if (lastPos !== i - 1)
156-
str += f.slice(lastPos, i - 1);
157-
str += tempStr;
158-
lastPos = i + 1;
159-
} else if (nextChar === 37) {
160-
str += f.slice(lastPos, i);
161-
lastPos = i + 1;
162-
}
163-
}
164-
}
165-
if (lastPos === 0)
166-
str = f;
167-
else if (lastPos < f.length)
168-
str += f.slice(lastPos);
169-
while (a < arguments.length) {
170-
const x = arguments[a++];
171-
if ((typeof x !== 'object' && typeof x !== 'symbol') || x === null) {
172-
str += ` ${x}`;
173-
} else {
174-
str += ` ${inspect(x, inspectOptions)}`;
175-
}
176-
}
177-
return str;
178-
}
179-
18055
const debugs = {};
18156
let debugEnvRegex = /^$/;
18257
if (process.env.NODE_DEBUG) {

0 commit comments

Comments
 (0)