util: refactor util module
* refactor util exports * early capture of prototype methods * use template strings and args consistently PR-URL: https://github.com/nodejs/node/pull/13803 Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
parent
4d1e0862ce
commit
b1355ba6fa
289
lib/util.js
289
lib/util.js
@ -21,13 +21,35 @@
|
|||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const uv = process.binding('uv');
|
|
||||||
const Buffer = require('buffer').Buffer;
|
|
||||||
const internalUtil = require('internal/util');
|
|
||||||
const binding = process.binding('util');
|
|
||||||
const errors = require('internal/errors');
|
const errors = require('internal/errors');
|
||||||
|
|
||||||
const isError = internalUtil.isError;
|
const { errname } = process.binding('uv');
|
||||||
|
|
||||||
|
const {
|
||||||
|
getPromiseDetails,
|
||||||
|
getProxyDetails,
|
||||||
|
isAnyArrayBuffer,
|
||||||
|
isDataView,
|
||||||
|
isExternal,
|
||||||
|
isMap,
|
||||||
|
isMapIterator,
|
||||||
|
isPromise,
|
||||||
|
isSet,
|
||||||
|
isSetIterator,
|
||||||
|
isTypedArray,
|
||||||
|
isRegExp: _isRegExp,
|
||||||
|
isDate: _isDate,
|
||||||
|
kPending,
|
||||||
|
kRejected,
|
||||||
|
} = process.binding('util');
|
||||||
|
|
||||||
|
const {
|
||||||
|
customInspectSymbol,
|
||||||
|
deprecate,
|
||||||
|
getConstructorOf,
|
||||||
|
isError,
|
||||||
|
promisify
|
||||||
|
} = require('internal/util');
|
||||||
|
|
||||||
const inspectDefaultOptions = Object.seal({
|
const inspectDefaultOptions = Object.seal({
|
||||||
showHidden: false,
|
showHidden: false,
|
||||||
@ -41,6 +63,12 @@ const inspectDefaultOptions = Object.seal({
|
|||||||
|
|
||||||
const numbersOnlyRE = /^\d+$/;
|
const numbersOnlyRE = /^\d+$/;
|
||||||
|
|
||||||
|
const objectHasOwnProperty = Object.prototype.hasOwnProperty;
|
||||||
|
const propertyIsEnumerable = Object.prototype.propertyIsEnumerable;
|
||||||
|
const regExpToString = RegExp.prototype.toString;
|
||||||
|
const dateToISOString = Date.prototype.toISOString;
|
||||||
|
const errorToString = Error.prototype.toString;
|
||||||
|
|
||||||
var CIRCULAR_ERROR_MESSAGE;
|
var CIRCULAR_ERROR_MESSAGE;
|
||||||
var Debug;
|
var Debug;
|
||||||
|
|
||||||
@ -62,7 +90,7 @@ function tryStringify(arg) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.format = function(f) {
|
function format(f) {
|
||||||
if (typeof f !== 'string') {
|
if (typeof f !== 'string') {
|
||||||
const objects = new Array(arguments.length);
|
const objects = new Array(arguments.length);
|
||||||
for (var index = 0; index < arguments.length; index++) {
|
for (var index = 0; index < arguments.length; index++) {
|
||||||
@ -132,21 +160,19 @@ exports.format = function(f) {
|
|||||||
while (a < arguments.length) {
|
while (a < arguments.length) {
|
||||||
const x = arguments[a++];
|
const x = arguments[a++];
|
||||||
if (x === null || (typeof x !== 'object' && typeof x !== 'symbol')) {
|
if (x === null || (typeof x !== 'object' && typeof x !== 'symbol')) {
|
||||||
str += ' ' + x;
|
str += ` ${x}`;
|
||||||
} else {
|
} else {
|
||||||
str += ' ' + inspect(x);
|
str += ` ${inspect(x)}`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return str;
|
return str;
|
||||||
};
|
}
|
||||||
|
|
||||||
|
|
||||||
exports.deprecate = internalUtil.deprecate;
|
|
||||||
|
|
||||||
|
|
||||||
var debugs = {};
|
var debugs = {};
|
||||||
var debugEnviron;
|
var debugEnviron;
|
||||||
exports.debuglog = function(set) {
|
|
||||||
|
function debuglog(set) {
|
||||||
if (debugEnviron === undefined) {
|
if (debugEnviron === undefined) {
|
||||||
debugEnviron = new Set(
|
debugEnviron = new Set(
|
||||||
(process.env.NODE_DEBUG || '').split(',').map((s) => s.toUpperCase()));
|
(process.env.NODE_DEBUG || '').split(',').map((s) => s.toUpperCase()));
|
||||||
@ -164,7 +190,7 @@ exports.debuglog = function(set) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return debugs[set];
|
return debugs[set];
|
||||||
};
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -198,6 +224,7 @@ function inspect(obj, opts) {
|
|||||||
if (ctx.maxArrayLength === null) ctx.maxArrayLength = Infinity;
|
if (ctx.maxArrayLength === null) ctx.maxArrayLength = Infinity;
|
||||||
return formatValue(ctx, obj, ctx.depth);
|
return formatValue(ctx, obj, ctx.depth);
|
||||||
}
|
}
|
||||||
|
inspect.custom = customInspectSymbol;
|
||||||
|
|
||||||
Object.defineProperty(inspect, 'defaultOptions', {
|
Object.defineProperty(inspect, 'defaultOptions', {
|
||||||
get: function() {
|
get: function() {
|
||||||
@ -243,11 +270,6 @@ inspect.styles = Object.assign(Object.create(null), {
|
|||||||
'regexp': 'red'
|
'regexp': 'red'
|
||||||
});
|
});
|
||||||
|
|
||||||
const customInspectSymbol = internalUtil.customInspectSymbol;
|
|
||||||
|
|
||||||
exports.inspect = inspect;
|
|
||||||
exports.inspect.custom = customInspectSymbol;
|
|
||||||
|
|
||||||
function stylizeWithColor(str, styleType) {
|
function stylizeWithColor(str, styleType) {
|
||||||
var style = inspect.styles[styleType];
|
var style = inspect.styles[styleType];
|
||||||
|
|
||||||
@ -304,7 +326,7 @@ function formatValue(ctx, value, recurseTimes) {
|
|||||||
// Otherwise, it'll return an array. The first item
|
// Otherwise, it'll return an array. The first item
|
||||||
// is the target, the second item is the handler.
|
// is the target, the second item is the handler.
|
||||||
// We ignore (and do not return) the Proxy isRevoked property.
|
// We ignore (and do not return) the Proxy isRevoked property.
|
||||||
proxy = binding.getProxyDetails(value);
|
proxy = getProxyDetails(value);
|
||||||
if (proxy) {
|
if (proxy) {
|
||||||
// We know for a fact that this isn't a Proxy.
|
// We know for a fact that this isn't a Proxy.
|
||||||
// Mark it as having already been evaluated.
|
// Mark it as having already been evaluated.
|
||||||
@ -322,7 +344,7 @@ function formatValue(ctx, value, recurseTimes) {
|
|||||||
proxyCache.set(value, proxy);
|
proxyCache.set(value, proxy);
|
||||||
}
|
}
|
||||||
if (proxy) {
|
if (proxy) {
|
||||||
return 'Proxy ' + formatValue(ctx, proxy, recurseTimes);
|
return `Proxy ${formatValue(ctx, proxy, recurseTimes)}`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -360,7 +382,7 @@ function formatValue(ctx, value, recurseTimes) {
|
|||||||
var visibleKeys = arrayToHash(keys);
|
var visibleKeys = arrayToHash(keys);
|
||||||
const symbolKeys = Object.getOwnPropertySymbols(value);
|
const symbolKeys = Object.getOwnPropertySymbols(value);
|
||||||
const enumSymbolKeys = symbolKeys
|
const enumSymbolKeys = symbolKeys
|
||||||
.filter((key) => Object.prototype.propertyIsEnumerable.call(value, key));
|
.filter((key) => propertyIsEnumerable.call(value, key));
|
||||||
keys = keys.concat(enumSymbolKeys);
|
keys = keys.concat(enumSymbolKeys);
|
||||||
|
|
||||||
if (ctx.showHidden) {
|
if (ctx.showHidden) {
|
||||||
@ -393,7 +415,7 @@ function formatValue(ctx, value, recurseTimes) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
var constructor = internalUtil.getConstructorOf(value);
|
var constructor = getConstructorOf(value);
|
||||||
|
|
||||||
// Some type of object without properties can be shortcutted.
|
// Some type of object without properties can be shortcutted.
|
||||||
if (keys.length === 0) {
|
if (keys.length === 0) {
|
||||||
@ -403,13 +425,13 @@ function formatValue(ctx, value, recurseTimes) {
|
|||||||
`[${ctorName}${value.name ? `: ${value.name}` : ''}]`, 'special');
|
`[${ctorName}${value.name ? `: ${value.name}` : ''}]`, 'special');
|
||||||
}
|
}
|
||||||
if (isRegExp(value)) {
|
if (isRegExp(value)) {
|
||||||
return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
|
return ctx.stylize(regExpToString.call(value), 'regexp');
|
||||||
}
|
}
|
||||||
if (isDate(value)) {
|
if (isDate(value)) {
|
||||||
if (Number.isNaN(value.getTime())) {
|
if (Number.isNaN(value.getTime())) {
|
||||||
return ctx.stylize(value.toString(), 'date');
|
return ctx.stylize(value.toString(), 'date');
|
||||||
} else {
|
} else {
|
||||||
return ctx.stylize(Date.prototype.toISOString.call(value), 'date');
|
return ctx.stylize(dateToISOString.call(value), 'date');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (isError(value)) {
|
if (isError(value)) {
|
||||||
@ -435,11 +457,11 @@ function formatValue(ctx, value, recurseTimes) {
|
|||||||
// Fast path for ArrayBuffer and SharedArrayBuffer.
|
// Fast path for ArrayBuffer and SharedArrayBuffer.
|
||||||
// Can't do the same for DataView because it has a non-primitive
|
// Can't do the same for DataView because it has a non-primitive
|
||||||
// .buffer property that we need to recurse for.
|
// .buffer property that we need to recurse for.
|
||||||
if (binding.isAnyArrayBuffer(value)) {
|
if (isAnyArrayBuffer(value)) {
|
||||||
return `${constructor.name}` +
|
return `${constructor.name}` +
|
||||||
` { byteLength: ${formatNumber(ctx, value.byteLength)} }`;
|
` { byteLength: ${formatNumber(ctx, value.byteLength)} }`;
|
||||||
}
|
}
|
||||||
if (binding.isExternal(value)) {
|
if (isExternal(value)) {
|
||||||
return ctx.stylize('[External]', 'special');
|
return ctx.stylize('[External]', 'special');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -461,7 +483,7 @@ function formatValue(ctx, value, recurseTimes) {
|
|||||||
braces = ['[', ']'];
|
braces = ['[', ']'];
|
||||||
empty = value.length === 0;
|
empty = value.length === 0;
|
||||||
formatter = formatArray;
|
formatter = formatArray;
|
||||||
} else if (binding.isSet(value)) {
|
} else if (isSet(value)) {
|
||||||
braces = ['{', '}'];
|
braces = ['{', '}'];
|
||||||
// With `showHidden`, `length` will display as a hidden property for
|
// With `showHidden`, `length` will display as a hidden property for
|
||||||
// arrays. For consistency's sake, do the same for `size`, even though this
|
// arrays. For consistency's sake, do the same for `size`, even though this
|
||||||
@ -470,25 +492,25 @@ function formatValue(ctx, value, recurseTimes) {
|
|||||||
keys.unshift('size');
|
keys.unshift('size');
|
||||||
empty = value.size === 0;
|
empty = value.size === 0;
|
||||||
formatter = formatSet;
|
formatter = formatSet;
|
||||||
} else if (binding.isMap(value)) {
|
} else if (isMap(value)) {
|
||||||
braces = ['{', '}'];
|
braces = ['{', '}'];
|
||||||
// Ditto.
|
// Ditto.
|
||||||
if (ctx.showHidden)
|
if (ctx.showHidden)
|
||||||
keys.unshift('size');
|
keys.unshift('size');
|
||||||
empty = value.size === 0;
|
empty = value.size === 0;
|
||||||
formatter = formatMap;
|
formatter = formatMap;
|
||||||
} else if (binding.isAnyArrayBuffer(value)) {
|
} else if (isAnyArrayBuffer(value)) {
|
||||||
braces = ['{', '}'];
|
braces = ['{', '}'];
|
||||||
keys.unshift('byteLength');
|
keys.unshift('byteLength');
|
||||||
visibleKeys.byteLength = true;
|
visibleKeys.byteLength = true;
|
||||||
} else if (binding.isDataView(value)) {
|
} else if (isDataView(value)) {
|
||||||
braces = ['{', '}'];
|
braces = ['{', '}'];
|
||||||
// .buffer goes last, it's not a primitive like the others.
|
// .buffer goes last, it's not a primitive like the others.
|
||||||
keys.unshift('byteLength', 'byteOffset', 'buffer');
|
keys.unshift('byteLength', 'byteOffset', 'buffer');
|
||||||
visibleKeys.byteLength = true;
|
visibleKeys.byteLength = true;
|
||||||
visibleKeys.byteOffset = true;
|
visibleKeys.byteOffset = true;
|
||||||
visibleKeys.buffer = true;
|
visibleKeys.buffer = true;
|
||||||
} else if (binding.isTypedArray(value)) {
|
} else if (isTypedArray(value)) {
|
||||||
braces = ['[', ']'];
|
braces = ['[', ']'];
|
||||||
formatter = formatTypedArray;
|
formatter = formatTypedArray;
|
||||||
if (ctx.showHidden) {
|
if (ctx.showHidden) {
|
||||||
@ -499,15 +521,15 @@ function formatValue(ctx, value, recurseTimes) {
|
|||||||
'byteOffset',
|
'byteOffset',
|
||||||
'buffer');
|
'buffer');
|
||||||
}
|
}
|
||||||
} else if (binding.isPromise(value)) {
|
} else if (isPromise(value)) {
|
||||||
braces = ['{', '}'];
|
braces = ['{', '}'];
|
||||||
formatter = formatPromise;
|
formatter = formatPromise;
|
||||||
} else if (binding.isMapIterator(value)) {
|
} else if (isMapIterator(value)) {
|
||||||
constructor = { name: 'MapIterator' };
|
constructor = { name: 'MapIterator' };
|
||||||
braces = ['{', '}'];
|
braces = ['{', '}'];
|
||||||
empty = false;
|
empty = false;
|
||||||
formatter = formatCollectionIterator;
|
formatter = formatCollectionIterator;
|
||||||
} else if (binding.isSetIterator(value)) {
|
} else if (isSetIterator(value)) {
|
||||||
constructor = { name: 'SetIterator' };
|
constructor = { name: 'SetIterator' };
|
||||||
braces = ['{', '}'];
|
braces = ['{', '}'];
|
||||||
empty = false;
|
empty = false;
|
||||||
@ -530,17 +552,17 @@ function formatValue(ctx, value, recurseTimes) {
|
|||||||
|
|
||||||
// Make RegExps say that they are RegExps
|
// Make RegExps say that they are RegExps
|
||||||
if (isRegExp(value)) {
|
if (isRegExp(value)) {
|
||||||
base = ' ' + RegExp.prototype.toString.call(value);
|
base = ` ${regExpToString.call(value)}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make dates with properties first say the date
|
// Make dates with properties first say the date
|
||||||
if (isDate(value)) {
|
if (isDate(value)) {
|
||||||
base = ' ' + Date.prototype.toISOString.call(value);
|
base = ` ${dateToISOString.call(value)}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make error with message first say the error
|
// Make error with message first say the error
|
||||||
if (isError(value)) {
|
if (isError(value)) {
|
||||||
base = ' ' + formatError(value);
|
base = ` ${formatError(value)}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make boxed primitive Strings look like such
|
// Make boxed primitive Strings look like such
|
||||||
@ -566,12 +588,12 @@ function formatValue(ctx, value, recurseTimes) {
|
|||||||
braces[0] = `${constructor.name} ${braces[0]}`;
|
braces[0] = `${constructor.name} ${braces[0]}`;
|
||||||
|
|
||||||
if (empty === true) {
|
if (empty === true) {
|
||||||
return braces[0] + base + braces[1];
|
return `${braces[0]}${base}${braces[1]}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (recurseTimes < 0) {
|
if (recurseTimes < 0) {
|
||||||
if (isRegExp(value)) {
|
if (isRegExp(value)) {
|
||||||
return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
|
return ctx.stylize(regExpToString.call(value), 'regexp');
|
||||||
} else if (Array.isArray(value)) {
|
} else if (Array.isArray(value)) {
|
||||||
return ctx.stylize('[Array]', 'special');
|
return ctx.stylize('[Array]', 'special');
|
||||||
} else {
|
} else {
|
||||||
@ -593,7 +615,7 @@ function formatNumber(ctx, value) {
|
|||||||
// Format -0 as '-0'. Strict equality won't distinguish 0 from -0.
|
// Format -0 as '-0'. Strict equality won't distinguish 0 from -0.
|
||||||
if (Object.is(value, -0))
|
if (Object.is(value, -0))
|
||||||
return ctx.stylize('-0', 'number');
|
return ctx.stylize('-0', 'number');
|
||||||
return ctx.stylize('' + value, 'number');
|
return ctx.stylize(`${value}`, 'number');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -608,18 +630,16 @@ function formatPrimitive(ctx, value) {
|
|||||||
var type = typeof value;
|
var type = typeof value;
|
||||||
|
|
||||||
if (type === 'string') {
|
if (type === 'string') {
|
||||||
var simple = '\'' +
|
var simple = JSON.stringify(value)
|
||||||
JSON.stringify(value)
|
|
||||||
.replace(/^"|"$/g, '')
|
.replace(/^"|"$/g, '')
|
||||||
.replace(/'/g, "\\'")
|
.replace(/'/g, "\\'")
|
||||||
.replace(/\\"/g, '"') +
|
.replace(/\\"/g, '"');
|
||||||
'\'';
|
return ctx.stylize(`'${simple}'`, 'string');
|
||||||
return ctx.stylize(simple, 'string');
|
|
||||||
}
|
}
|
||||||
if (type === 'number')
|
if (type === 'number')
|
||||||
return formatNumber(ctx, value);
|
return formatNumber(ctx, value);
|
||||||
if (type === 'boolean')
|
if (type === 'boolean')
|
||||||
return ctx.stylize('' + value, 'boolean');
|
return ctx.stylize(`${value}`, 'boolean');
|
||||||
// es6 symbol primitive
|
// es6 symbol primitive
|
||||||
if (type === 'symbol')
|
if (type === 'symbol')
|
||||||
return ctx.stylize(value.toString(), 'symbol');
|
return ctx.stylize(value.toString(), 'symbol');
|
||||||
@ -636,7 +656,7 @@ function formatPrimitiveNoColor(ctx, value) {
|
|||||||
|
|
||||||
|
|
||||||
function formatError(value) {
|
function formatError(value) {
|
||||||
return value.stack || `[${Error.prototype.toString.call(value)}]`;
|
return value.stack || `[${errorToString.call(value)}]`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -747,15 +767,15 @@ function formatCollectionIterator(ctx, value, recurseTimes, visibleKeys, keys) {
|
|||||||
|
|
||||||
function formatPromise(ctx, value, recurseTimes, visibleKeys, keys) {
|
function formatPromise(ctx, value, recurseTimes, visibleKeys, keys) {
|
||||||
const output = [];
|
const output = [];
|
||||||
const [state, result] = binding.getPromiseDetails(value);
|
const [state, result] = getPromiseDetails(value);
|
||||||
|
|
||||||
if (state === binding.kPending) {
|
if (state === kPending) {
|
||||||
output.push('<pending>');
|
output.push('<pending>');
|
||||||
} else {
|
} else {
|
||||||
var nextRecurseTimes = recurseTimes === null ? null : recurseTimes - 1;
|
var nextRecurseTimes = recurseTimes === null ? null : recurseTimes - 1;
|
||||||
var str = formatValue(ctx, result, nextRecurseTimes);
|
var str = formatValue(ctx, result, nextRecurseTimes);
|
||||||
if (state === binding.kRejected) {
|
if (state === kRejected) {
|
||||||
output.push('<rejected> ' + str);
|
output.push(`<rejected> ${str}`);
|
||||||
} else {
|
} else {
|
||||||
output.push(str);
|
output.push(str);
|
||||||
}
|
}
|
||||||
@ -811,7 +831,7 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
|
|||||||
if (array && numbersOnlyRE.test(key)) {
|
if (array && numbersOnlyRE.test(key)) {
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
name = JSON.stringify('' + key);
|
name = JSON.stringify(`${key}`);
|
||||||
if (/^"[a-zA-Z_][a-zA-Z_0-9]*"$/.test(name)) {
|
if (/^"[a-zA-Z_][a-zA-Z_0-9]*"$/.test(name)) {
|
||||||
name = name.substr(1, name.length - 2);
|
name = name.substr(1, name.length - 2);
|
||||||
name = ctx.stylize(name, 'name');
|
name = ctx.stylize(name, 'name');
|
||||||
@ -838,85 +858,64 @@ function reduceToSingleString(output, base, braces, breakLength) {
|
|||||||
// If the opening "brace" is too large, like in the case of "Set {",
|
// If the opening "brace" is too large, like in the case of "Set {",
|
||||||
// we need to force the first item to be on the next line or the
|
// we need to force the first item to be on the next line or the
|
||||||
// items will not line up correctly.
|
// items will not line up correctly.
|
||||||
(base === '' && braces[0].length === 1 ? '' : base + '\n ') +
|
(base === '' && braces[0].length === 1 ? '' : `${base}\n `) +
|
||||||
` ${output.join(',\n ')} ${braces[1]}`;
|
` ${output.join(',\n ')} ${braces[1]}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return `${braces[0]}${base} ${output.join(', ')} ${braces[1]}`;
|
return `${braces[0]}${base} ${output.join(', ')} ${braces[1]}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// NOTE: These type checking functions intentionally don't use `instanceof`
|
|
||||||
// because it is fragile and can be easily faked with `Object.create()`.
|
|
||||||
exports.isArray = Array.isArray;
|
|
||||||
|
|
||||||
function isBoolean(arg) {
|
function isBoolean(arg) {
|
||||||
return typeof arg === 'boolean';
|
return typeof arg === 'boolean';
|
||||||
}
|
}
|
||||||
exports.isBoolean = isBoolean;
|
|
||||||
|
|
||||||
function isNull(arg) {
|
function isNull(arg) {
|
||||||
return arg === null;
|
return arg === null;
|
||||||
}
|
}
|
||||||
exports.isNull = isNull;
|
|
||||||
|
|
||||||
function isNullOrUndefined(arg) {
|
function isNullOrUndefined(arg) {
|
||||||
return arg === null || arg === undefined;
|
return arg === null || arg === undefined;
|
||||||
}
|
}
|
||||||
exports.isNullOrUndefined = isNullOrUndefined;
|
|
||||||
|
|
||||||
function isNumber(arg) {
|
function isNumber(arg) {
|
||||||
return typeof arg === 'number';
|
return typeof arg === 'number';
|
||||||
}
|
}
|
||||||
exports.isNumber = isNumber;
|
|
||||||
|
|
||||||
function isString(arg) {
|
function isString(arg) {
|
||||||
return typeof arg === 'string';
|
return typeof arg === 'string';
|
||||||
}
|
}
|
||||||
exports.isString = isString;
|
|
||||||
|
|
||||||
function isSymbol(arg) {
|
function isSymbol(arg) {
|
||||||
return typeof arg === 'symbol';
|
return typeof arg === 'symbol';
|
||||||
}
|
}
|
||||||
exports.isSymbol = isSymbol;
|
|
||||||
|
|
||||||
function isUndefined(arg) {
|
function isUndefined(arg) {
|
||||||
return arg === undefined;
|
return arg === undefined;
|
||||||
}
|
}
|
||||||
exports.isUndefined = isUndefined;
|
|
||||||
|
|
||||||
function isRegExp(re) {
|
function isRegExp(re) {
|
||||||
return binding.isRegExp(re);
|
return _isRegExp(re);
|
||||||
}
|
}
|
||||||
exports.isRegExp = isRegExp;
|
|
||||||
|
|
||||||
function isObject(arg) {
|
function isObject(arg) {
|
||||||
return arg !== null && typeof arg === 'object';
|
return arg !== null && typeof arg === 'object';
|
||||||
}
|
}
|
||||||
exports.isObject = isObject;
|
|
||||||
|
|
||||||
function isDate(d) {
|
function isDate(d) {
|
||||||
return binding.isDate(d);
|
return _isDate(d);
|
||||||
}
|
}
|
||||||
exports.isDate = isDate;
|
|
||||||
|
|
||||||
exports.isError = isError;
|
|
||||||
|
|
||||||
function isFunction(arg) {
|
function isFunction(arg) {
|
||||||
return typeof arg === 'function';
|
return typeof arg === 'function';
|
||||||
}
|
}
|
||||||
exports.isFunction = isFunction;
|
|
||||||
|
|
||||||
function isPrimitive(arg) {
|
function isPrimitive(arg) {
|
||||||
return arg === null ||
|
return arg === null ||
|
||||||
typeof arg !== 'object' && typeof arg !== 'function';
|
typeof arg !== 'object' && typeof arg !== 'function';
|
||||||
}
|
}
|
||||||
exports.isPrimitive = isPrimitive;
|
|
||||||
|
|
||||||
exports.isBuffer = Buffer.isBuffer;
|
|
||||||
|
|
||||||
function pad(n) {
|
function pad(n) {
|
||||||
return n < 10 ? '0' + n.toString(10) : n.toString(10);
|
return n < 10 ? `0${n.toString(10)}` : n.toString(10);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -934,9 +933,9 @@ function timestamp() {
|
|||||||
|
|
||||||
|
|
||||||
// log is just a thin wrapper to console.log that prepends a timestamp
|
// log is just a thin wrapper to console.log that prepends a timestamp
|
||||||
exports.log = function() {
|
function log() {
|
||||||
console.log('%s - %s', timestamp(), exports.format.apply(exports, arguments));
|
console.log('%s - %s', timestamp(), exports.format.apply(exports, arguments));
|
||||||
};
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -954,7 +953,7 @@ exports.log = function() {
|
|||||||
* @throws {TypeError} Will error if either constructor is null, or if
|
* @throws {TypeError} Will error if either constructor is null, or if
|
||||||
* the super constructor lacks a prototype.
|
* the super constructor lacks a prototype.
|
||||||
*/
|
*/
|
||||||
exports.inherits = function(ctor, superCtor) {
|
function inherits(ctor, superCtor) {
|
||||||
|
|
||||||
if (ctor === undefined || ctor === null)
|
if (ctor === undefined || ctor === null)
|
||||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'ctor', 'function');
|
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'ctor', 'function');
|
||||||
@ -968,9 +967,9 @@ exports.inherits = function(ctor, superCtor) {
|
|||||||
}
|
}
|
||||||
ctor.super_ = superCtor;
|
ctor.super_ = superCtor;
|
||||||
Object.setPrototypeOf(ctor.prototype, superCtor.prototype);
|
Object.setPrototypeOf(ctor.prototype, superCtor.prototype);
|
||||||
};
|
}
|
||||||
|
|
||||||
exports._extend = function(target, source) {
|
function _extend(target, source) {
|
||||||
// Don't do anything if source isn't an object
|
// Don't do anything if source isn't an object
|
||||||
if (source === null || typeof source !== 'object') return target;
|
if (source === null || typeof source !== 'object') return target;
|
||||||
|
|
||||||
@ -980,59 +979,55 @@ exports._extend = function(target, source) {
|
|||||||
target[keys[i]] = source[keys[i]];
|
target[keys[i]] = source[keys[i]];
|
||||||
}
|
}
|
||||||
return target;
|
return target;
|
||||||
};
|
}
|
||||||
|
|
||||||
function hasOwnProperty(obj, prop) {
|
function hasOwnProperty(obj, prop) {
|
||||||
return Object.prototype.hasOwnProperty.call(obj, prop);
|
return objectHasOwnProperty.call(obj, prop);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Deprecated old stuff.
|
// Deprecated old stuff.
|
||||||
|
|
||||||
exports.print = internalUtil.deprecate(function() {
|
function print(...args) {
|
||||||
for (var i = 0, len = arguments.length; i < len; ++i) {
|
for (var i = 0, len = args.length; i < len; ++i) {
|
||||||
process.stdout.write(String(arguments[i]));
|
process.stdout.write(String(args[i]));
|
||||||
}
|
}
|
||||||
}, 'util.print is deprecated. Use console.log instead.', 'DEP0026');
|
}
|
||||||
|
|
||||||
|
function puts(...args) {
|
||||||
exports.puts = internalUtil.deprecate(function() {
|
for (var i = 0, len = args.length; i < len; ++i) {
|
||||||
for (var i = 0, len = arguments.length; i < len; ++i) {
|
process.stdout.write(`${args[i]}\n`);
|
||||||
process.stdout.write(arguments[i] + '\n');
|
|
||||||
}
|
}
|
||||||
}, 'util.puts is deprecated. Use console.log instead.', 'DEP0027');
|
}
|
||||||
|
|
||||||
|
function debug(x) {
|
||||||
exports.debug = internalUtil.deprecate(function(x) {
|
|
||||||
process.stderr.write(`DEBUG: ${x}\n`);
|
process.stderr.write(`DEBUG: ${x}\n`);
|
||||||
}, 'util.debug is deprecated. Use console.error instead.', 'DEP0028');
|
}
|
||||||
|
|
||||||
|
function error(...args) {
|
||||||
exports.error = internalUtil.deprecate(function(x) {
|
for (var i = 0, len = args.length; i < len; ++i) {
|
||||||
for (var i = 0, len = arguments.length; i < len; ++i) {
|
process.stderr.write(`${args[i]}\n`);
|
||||||
process.stderr.write(arguments[i] + '\n');
|
|
||||||
}
|
}
|
||||||
}, 'util.error is deprecated. Use console.error instead.', 'DEP0029');
|
}
|
||||||
|
|
||||||
|
function _errnoException(err, syscall, original) {
|
||||||
exports._errnoException = function(err, syscall, original) {
|
var name = errname(err);
|
||||||
var errname = uv.errname(err);
|
var message = `${syscall} ${name}`;
|
||||||
var message = `${syscall} ${errname}`;
|
|
||||||
if (original)
|
if (original)
|
||||||
message += ' ' + original;
|
message += ` ${original}`;
|
||||||
var e = new Error(message);
|
var e = new Error(message);
|
||||||
e.code = errname;
|
e.code = name;
|
||||||
e.errno = errname;
|
e.errno = name;
|
||||||
e.syscall = syscall;
|
e.syscall = syscall;
|
||||||
return e;
|
return e;
|
||||||
};
|
}
|
||||||
|
|
||||||
|
|
||||||
exports._exceptionWithHostPort = function(err,
|
function _exceptionWithHostPort(err,
|
||||||
syscall,
|
syscall,
|
||||||
address,
|
address,
|
||||||
port,
|
port,
|
||||||
additional) {
|
additional) {
|
||||||
var details;
|
var details;
|
||||||
if (port && port > 0) {
|
if (port && port > 0) {
|
||||||
details = `${address}:${port}`;
|
details = `${address}:${port}`;
|
||||||
@ -1049,14 +1044,12 @@ exports._exceptionWithHostPort = function(err,
|
|||||||
ex.port = port;
|
ex.port = port;
|
||||||
}
|
}
|
||||||
return ex;
|
return ex;
|
||||||
};
|
}
|
||||||
|
|
||||||
// process.versions needs a custom function as some values are lazy-evaluated.
|
// process.versions needs a custom function as some values are lazy-evaluated.
|
||||||
process.versions[exports.inspect.custom] =
|
process.versions[inspect.custom] =
|
||||||
() => exports.format(JSON.parse(JSON.stringify(process.versions)));
|
() => exports.format(JSON.parse(JSON.stringify(process.versions)));
|
||||||
|
|
||||||
exports.promisify = internalUtil.promisify;
|
|
||||||
|
|
||||||
function callbackifyOnRejected(reason, cb) {
|
function callbackifyOnRejected(reason, cb) {
|
||||||
// `!reason` guard inspired by bluebird (Ref: https://goo.gl/t5IS6M).
|
// `!reason` guard inspired by bluebird (Ref: https://goo.gl/t5IS6M).
|
||||||
// Because `null` is a special error value in callbacks which means "no error
|
// Because `null` is a special error value in callbacks which means "no error
|
||||||
@ -1105,4 +1098,60 @@ function callbackify(original) {
|
|||||||
return callbackified;
|
return callbackified;
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.callbackify = callbackify;
|
// Keep the `exports =` so that various functions can still be monkeypatched
|
||||||
|
module.exports = exports = {
|
||||||
|
_errnoException,
|
||||||
|
_exceptionWithHostPort,
|
||||||
|
_extend,
|
||||||
|
callbackify,
|
||||||
|
debuglog,
|
||||||
|
deprecate,
|
||||||
|
format,
|
||||||
|
inherits,
|
||||||
|
inspect,
|
||||||
|
isArray: Array.isArray,
|
||||||
|
isBoolean,
|
||||||
|
isNull,
|
||||||
|
isNullOrUndefined,
|
||||||
|
isNumber,
|
||||||
|
isString,
|
||||||
|
isSymbol,
|
||||||
|
isUndefined,
|
||||||
|
isRegExp,
|
||||||
|
isObject,
|
||||||
|
isDate,
|
||||||
|
isError,
|
||||||
|
isFunction,
|
||||||
|
isPrimitive,
|
||||||
|
log,
|
||||||
|
promisify,
|
||||||
|
|
||||||
|
// Deprecated Old Stuff
|
||||||
|
debug: deprecate(debug,
|
||||||
|
'util.debug is deprecated. Use console.error instead.',
|
||||||
|
'DEP0028'),
|
||||||
|
error: deprecate(error,
|
||||||
|
'util.error is deprecated. Use console.error instead.',
|
||||||
|
'DEP0029'),
|
||||||
|
print: deprecate(print,
|
||||||
|
'util.print is deprecated. Use console.log instead.',
|
||||||
|
'DEP0026'),
|
||||||
|
puts: deprecate(puts,
|
||||||
|
'util.puts is deprecated. Use console.log instead.',
|
||||||
|
'DEP0027')
|
||||||
|
};
|
||||||
|
|
||||||
|
// Avoid a circular dependency
|
||||||
|
var isBuffer;
|
||||||
|
Object.defineProperty(exports, 'isBuffer', {
|
||||||
|
configurable: true,
|
||||||
|
enumerable: true,
|
||||||
|
get() {
|
||||||
|
if (!isBuffer)
|
||||||
|
isBuffer = require('buffer').Buffer.isBuffer;
|
||||||
|
return isBuffer;
|
||||||
|
},
|
||||||
|
set(val) {
|
||||||
|
isBuffer = val;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user