Skip to content

Commit

Permalink
lib: add internal removeColors helper
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
BridgeAR authored and evanlucas committed Jan 30, 2018
1 parent 987480c commit 14f7f60
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
5 changes: 2 additions & 3 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const {
isHexTable
} = require('internal/querystring');

const { getConstructorOf } = require('internal/util');
const { getConstructorOf, removeColors } = require('internal/util');
const errors = require('internal/errors');
const querystring = require('querystring');

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

var colorRe = /\u001b\[\d\d?m/g;
var length = output.reduce(
(prev, cur) => prev + cur.replace(colorRe, '').length + separator.length,
(prev, cur) => prev + removeColors(cur).length + separator.length,
-separator.length
);
if (length > ctx.breakLength) {
Expand Down
7 changes: 7 additions & 0 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ const noCrypto = !process.versions.openssl;

const experimentalWarnings = new Set();

const colorRegExp = /\u001b\[\d\d?m/g;

function removeColors(str) {
return str.replace(colorRegExp, '');
}

function isError(e) {
return objectToString(e) === '[object Error]' || e instanceof Error;
}
Expand Down Expand Up @@ -297,6 +303,7 @@ module.exports = {
objectToString,
promisify,
spliceOne,
removeColors,

// Symbol used to customize promisify conversion
customPromisifyArgs: kCustomPromisifyArgsSymbol,
Expand Down
6 changes: 3 additions & 3 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ const {
getConstructorOf,
isError,
promisify,
join
join,
removeColors
} = require('internal/util');

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

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

0 comments on commit 14f7f60

Please sign in to comment.