Skip to content

Commit

Permalink
lib: move format and formatWithOptions into internal/util/inspect.js
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
joyeecheung committed Mar 8, 2019
1 parent f617a73 commit 21440c8
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 130 deletions.
133 changes: 132 additions & 1 deletion lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 5 additions & 129 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand Down

0 comments on commit 21440c8

Please sign in to comment.