Skip to content

Commit

Permalink
url: refactor lib/internal/url.js
Browse files Browse the repository at this point in the history
* set an identifier for the separator rather than using multiple
  instances of the same literal
* consistent arrow function body formatting

PR-URL: #10912
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Italo A. Casas <me@italoacasas.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
  • Loading branch information
Trott authored and italoacasas committed Jan 30, 2017
1 parent 90a9917 commit c0c1a4c
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,7 @@ URLSearchParams.prototype[Symbol.iterator] = URLSearchParams.prototype.entries;

URLSearchParams.prototype[util.inspect.custom] =
function inspect(recurseTimes, ctx) {
const separator = ', ';
const innerOpts = Object.assign({}, ctx);
if (recurseTimes !== null) {
innerOpts.depth = recurseTimes - 1;
Expand All @@ -872,13 +873,14 @@ URLSearchParams.prototype[util.inspect.custom] =
output.push(`${innerInspect(list[i])} => ${innerInspect(list[i + 1])}`);

const colorRe = /\u001b\[\d\d?m/g;
const length = output.reduce((prev, cur) => {
return prev + cur.replace(colorRe, '').length + ', '.length;
}, -', '.length);
const length = output.reduce(
(prev, cur) => prev + cur.replace(colorRe, '').length + separator.length,
-separator.length
);
if (length > ctx.breakLength) {
return `${this.constructor.name} {\n ${output.join(',\n ')} }`;
} else if (output.length) {
return `${this.constructor.name} { ${output.join(', ')} }`;
return `${this.constructor.name} { ${output.join(separator)} }`;
} else {
return `${this.constructor.name} {}`;
}
Expand Down

0 comments on commit c0c1a4c

Please sign in to comment.