Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 19bff31

Browse files
committedJan 24, 2018
lib: add internal removeColors helper
Instead of having three times the same RegExp, just use a helper. PR-URL: #17615 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 2d9e876 commit 19bff31

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed
 

‎lib/internal/url.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const {
66
isHexTable
77
} = require('internal/querystring');
88

9-
const { getConstructorOf } = require('internal/util');
9+
const { getConstructorOf, removeColors } = require('internal/util');
1010
const errors = require('internal/errors');
1111
const querystring = require('querystring');
1212

@@ -181,9 +181,8 @@ class URLSearchParams {
181181
for (var i = 0; i < list.length; i += 2)
182182
output.push(`${innerInspect(list[i])} => ${innerInspect(list[i + 1])}`);
183183

184-
var colorRe = /\u001b\[\d\d?m/g;
185184
var length = output.reduce(
186-
(prev, cur) => prev + cur.replace(colorRe, '').length + separator.length,
185+
(prev, cur) => prev + removeColors(cur).length + separator.length,
187186
-separator.length
188187
);
189188
if (length > ctx.breakLength) {

‎lib/internal/util.js

+7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ const noCrypto = !process.versions.openssl;
1818

1919
const experimentalWarnings = new Set();
2020

21+
const colorRegExp = /\u001b\[\d\d?m/g;
22+
23+
function removeColors(str) {
24+
return str.replace(colorRegExp, '');
25+
}
26+
2127
function isError(e) {
2228
return objectToString(e) === '[object Error]' || e instanceof Error;
2329
}
@@ -362,6 +368,7 @@ module.exports = {
362368
objectToString,
363369
promisify,
364370
spliceOne,
371+
removeColors,
365372

366373
// Symbol used to customize promisify conversion
367374
customPromisifyArgs: kCustomPromisifyArgsSymbol,

‎lib/util.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ const {
5959
getIdentificationOf,
6060
isError,
6161
promisify,
62-
join
62+
join,
63+
removeColors
6364
} = require('internal/util');
6465

6566
const inspectDefaultOptions = Object.seal({
@@ -85,7 +86,6 @@ const strEscapeSequencesRegExp = /[\x00-\x1f\x27\x5c]/;
8586
const strEscapeSequencesReplacer = /[\x00-\x1f\x27\x5c]/g;
8687
/* eslint-enable */
8788
const keyStrRegExp = /^[a-zA-Z_][a-zA-Z_0-9]*$/;
88-
const colorRegExp = /\u001b\[\d\d?m/g;
8989
const numberRegExp = /^(0|[1-9][0-9]*)$/;
9090

9191
const readableRegExps = {};
@@ -905,7 +905,7 @@ function reduceToSingleString(ctx, output, base, braces, addLn) {
905905
var length = 0;
906906
for (; i < output.length && length <= breakLength; i++) {
907907
if (ctx.colors) {
908-
length += output[i].replace(colorRegExp, '').length + 1;
908+
length += removeColors(output[i]).length + 1;
909909
} else {
910910
length += output[i].length + 1;
911911
}

0 commit comments

Comments
 (0)
Please sign in to comment.