Skip to content

Commit 21440c8

Browse files
committed
lib: move format and formatWithOptions into internal/util/inspect.js
So these can be required without requiring the whole `util.js`. PR-URL: nodejs#26468 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent f617a73 commit 21440c8

File tree

2 files changed

+137
-130
lines changed

2 files changed

+137
-130
lines changed

lib/internal/util/inspect.js

Lines changed: 132 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1363,8 +1363,139 @@ function reduceToSingleString(ctx, output, base, braces, combine = false) {
13631363
return `${braces[0]}${ln}${join(output, `,\n${indentation} `)} ${braces[1]}`;
13641364
}
13651365

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

lib/util.js

Lines changed: 5 additions & 129 deletions
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,136 +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, ...args) {
76-
const first = args[0];
77-
let a = 0;
78-
let str = '';
79-
let join = '';
80-
81-
if (typeof first === 'string') {
82-
if (args.length === 1) {
83-
return first;
84-
}
85-
let tempStr;
86-
let lastPos = 0;
87-
88-
for (var i = 0; i < first.length - 1; i++) {
89-
if (first.charCodeAt(i) === 37) { // '%'
90-
const nextChar = first.charCodeAt(++i);
91-
if (a + 1 !== args.length) {
92-
switch (nextChar) {
93-
case 115: // 's'
94-
tempStr = String(args[++a]);
95-
break;
96-
case 106: // 'j'
97-
tempStr = tryStringify(args[++a]);
98-
break;
99-
case 100: // 'd'
100-
const tempNum = args[++a];
101-
// eslint-disable-next-line valid-typeof
102-
if (typeof tempNum === 'bigint') {
103-
tempStr = `${tempNum}n`;
104-
} else if (typeof tempNum === 'symbol') {
105-
tempStr = 'NaN';
106-
} else {
107-
tempStr = `${Number(tempNum)}`;
108-
}
109-
break;
110-
case 79: // 'O'
111-
tempStr = inspect(args[++a], inspectOptions);
112-
break;
113-
case 111: // 'o'
114-
{
115-
tempStr = inspect(args[++a], {
116-
...inspectOptions,
117-
showHidden: true,
118-
showProxy: true,
119-
depth: 4
120-
});
121-
break;
122-
}
123-
case 105: // 'i'
124-
const tempInteger = args[++a];
125-
// eslint-disable-next-line valid-typeof
126-
if (typeof tempInteger === 'bigint') {
127-
tempStr = `${tempInteger}n`;
128-
} else if (typeof tempInteger === 'symbol') {
129-
tempStr = 'NaN';
130-
} else {
131-
tempStr = `${parseInt(tempInteger)}`;
132-
}
133-
break;
134-
case 102: // 'f'
135-
const tempFloat = args[++a];
136-
if (typeof tempFloat === 'symbol') {
137-
tempStr = 'NaN';
138-
} else {
139-
tempStr = `${parseFloat(tempFloat)}`;
140-
}
141-
break;
142-
case 37: // '%'
143-
str += first.slice(lastPos, i);
144-
lastPos = i + 1;
145-
continue;
146-
default: // Any other character is not a correct placeholder
147-
continue;
148-
}
149-
if (lastPos !== i - 1) {
150-
str += first.slice(lastPos, i - 1);
151-
}
152-
str += tempStr;
153-
lastPos = i + 1;
154-
} else if (nextChar === 37) {
155-
str += first.slice(lastPos, i);
156-
lastPos = i + 1;
157-
}
158-
}
159-
}
160-
if (lastPos !== 0) {
161-
a++;
162-
join = ' ';
163-
if (lastPos < first.length) {
164-
str += first.slice(lastPos);
165-
}
166-
}
167-
}
168-
169-
while (a < args.length) {
170-
const value = args[a];
171-
str += join;
172-
str += typeof value !== 'string' ? inspect(value, inspectOptions) : value;
173-
join = ' ';
174-
a++;
175-
}
176-
return str;
177-
}
178-
17955
const debugs = {};
18056
let debugEnvRegex = /^$/;
18157
if (process.env.NODE_DEBUG) {

0 commit comments

Comments
 (0)