aboutsummaryrefslogtreecommitdiff
path: root/lib/util.js
diff options
context:
space:
mode:
authorJoyee Cheung <joyeec9h3@gmail.com>2019-03-06 12:33:09 +0100
committerJoyee Cheung <joyeec9h3@gmail.com>2019-03-08 13:57:34 +0100
commit21440c8acdad635abc3020aa50e4c2e6d64d8c63 (patch)
tree12bcebd93cfc2f5660250ada598dd9fa7c1e589c /lib/util.js
parentf617a732f86289f39e4f2ce41564b0f35b00a9cf (diff)
downloadandroid-node-v8-21440c8acdad635abc3020aa50e4c2e6d64d8c63.tar.gz
android-node-v8-21440c8acdad635abc3020aa50e4c2e6d64d8c63.tar.bz2
android-node-v8-21440c8acdad635abc3020aa50e4c2e6d64d8c63.zip
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>
Diffstat (limited to 'lib/util.js')
-rw-r--r--lib/util.js134
1 files changed, 5 insertions, 129 deletions
diff --git a/lib/util.js b/lib/util.js
index f422a1f410..98e41e5a37 100644
--- a/lib/util.js
+++ b/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) {