lib: add internal removeColors helper
Instead of having three times the same RegExp, just use a helper. PR-URL: https://github.com/nodejs/node/pull/17615 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
parent
2d9e87695e
commit
19bff313be
@ -6,7 +6,7 @@ const {
|
|||||||
isHexTable
|
isHexTable
|
||||||
} = require('internal/querystring');
|
} = require('internal/querystring');
|
||||||
|
|
||||||
const { getConstructorOf } = require('internal/util');
|
const { getConstructorOf, removeColors } = require('internal/util');
|
||||||
const errors = require('internal/errors');
|
const errors = require('internal/errors');
|
||||||
const querystring = require('querystring');
|
const querystring = require('querystring');
|
||||||
|
|
||||||
@ -181,9 +181,8 @@ class URLSearchParams {
|
|||||||
for (var i = 0; i < list.length; i += 2)
|
for (var i = 0; i < list.length; i += 2)
|
||||||
output.push(`${innerInspect(list[i])} => ${innerInspect(list[i + 1])}`);
|
output.push(`${innerInspect(list[i])} => ${innerInspect(list[i + 1])}`);
|
||||||
|
|
||||||
var colorRe = /\u001b\[\d\d?m/g;
|
|
||||||
var length = output.reduce(
|
var length = output.reduce(
|
||||||
(prev, cur) => prev + cur.replace(colorRe, '').length + separator.length,
|
(prev, cur) => prev + removeColors(cur).length + separator.length,
|
||||||
-separator.length
|
-separator.length
|
||||||
);
|
);
|
||||||
if (length > ctx.breakLength) {
|
if (length > ctx.breakLength) {
|
||||||
|
@ -18,6 +18,12 @@ const noCrypto = !process.versions.openssl;
|
|||||||
|
|
||||||
const experimentalWarnings = new Set();
|
const experimentalWarnings = new Set();
|
||||||
|
|
||||||
|
const colorRegExp = /\u001b\[\d\d?m/g;
|
||||||
|
|
||||||
|
function removeColors(str) {
|
||||||
|
return str.replace(colorRegExp, '');
|
||||||
|
}
|
||||||
|
|
||||||
function isError(e) {
|
function isError(e) {
|
||||||
return objectToString(e) === '[object Error]' || e instanceof Error;
|
return objectToString(e) === '[object Error]' || e instanceof Error;
|
||||||
}
|
}
|
||||||
@ -362,6 +368,7 @@ module.exports = {
|
|||||||
objectToString,
|
objectToString,
|
||||||
promisify,
|
promisify,
|
||||||
spliceOne,
|
spliceOne,
|
||||||
|
removeColors,
|
||||||
|
|
||||||
// Symbol used to customize promisify conversion
|
// Symbol used to customize promisify conversion
|
||||||
customPromisifyArgs: kCustomPromisifyArgsSymbol,
|
customPromisifyArgs: kCustomPromisifyArgsSymbol,
|
||||||
|
@ -59,7 +59,8 @@ const {
|
|||||||
getIdentificationOf,
|
getIdentificationOf,
|
||||||
isError,
|
isError,
|
||||||
promisify,
|
promisify,
|
||||||
join
|
join,
|
||||||
|
removeColors
|
||||||
} = require('internal/util');
|
} = require('internal/util');
|
||||||
|
|
||||||
const inspectDefaultOptions = Object.seal({
|
const inspectDefaultOptions = Object.seal({
|
||||||
@ -85,7 +86,6 @@ const strEscapeSequencesRegExp = /[\x00-\x1f\x27\x5c]/;
|
|||||||
const strEscapeSequencesReplacer = /[\x00-\x1f\x27\x5c]/g;
|
const strEscapeSequencesReplacer = /[\x00-\x1f\x27\x5c]/g;
|
||||||
/* eslint-enable */
|
/* eslint-enable */
|
||||||
const keyStrRegExp = /^[a-zA-Z_][a-zA-Z_0-9]*$/;
|
const keyStrRegExp = /^[a-zA-Z_][a-zA-Z_0-9]*$/;
|
||||||
const colorRegExp = /\u001b\[\d\d?m/g;
|
|
||||||
const numberRegExp = /^(0|[1-9][0-9]*)$/;
|
const numberRegExp = /^(0|[1-9][0-9]*)$/;
|
||||||
|
|
||||||
const readableRegExps = {};
|
const readableRegExps = {};
|
||||||
@ -905,7 +905,7 @@ function reduceToSingleString(ctx, output, base, braces, addLn) {
|
|||||||
var length = 0;
|
var length = 0;
|
||||||
for (; i < output.length && length <= breakLength; i++) {
|
for (; i < output.length && length <= breakLength; i++) {
|
||||||
if (ctx.colors) {
|
if (ctx.colors) {
|
||||||
length += output[i].replace(colorRegExp, '').length + 1;
|
length += removeColors(output[i]).length + 1;
|
||||||
} else {
|
} else {
|
||||||
length += output[i].length + 1;
|
length += output[i].length + 1;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user