Skip to content

Commit 14f7f60

Browse files
BridgeARevanlucas
authored andcommittedJan 30, 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 <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 987480c commit 14f7f60

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
@@ -12,6 +12,12 @@ const noCrypto = !process.versions.openssl;
1212

1313
const experimentalWarnings = new Set();
1414

15+
const colorRegExp = /\u001b\[\d\d?m/g;
16+
17+
function removeColors(str) {
18+
return str.replace(colorRegExp, '');
19+
}
20+
1521
function isError(e) {
1622
return objectToString(e) === '[object Error]' || e instanceof Error;
1723
}
@@ -297,6 +303,7 @@ module.exports = {
297303
objectToString,
298304
promisify,
299305
spliceOne,
306+
removeColors,
300307

301308
// Symbol used to customize promisify conversion
302309
customPromisifyArgs: kCustomPromisifyArgsSymbol,

‎lib/util.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ const {
5858
getConstructorOf,
5959
isError,
6060
promisify,
61-
join
61+
join,
62+
removeColors
6263
} = require('internal/util');
6364

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

9090
// Escaped special characters. Use empty strings to fill up unused entries.
@@ -843,7 +843,7 @@ function reduceToSingleString(ctx, output, base, braces, addLn) {
843843
var length = 0;
844844
for (var i = 0; i < output.length && length <= breakLength; i++) {
845845
if (ctx.colors) {
846-
length += output[i].replace(colorRegExp, '').length + 1;
846+
length += removeColors(output[i]).length + 1;
847847
} else {
848848
length += output[i].length + 1;
849849
}

0 commit comments

Comments
 (0)
Please sign in to comment.