url: refactor lib/internal/url.js

* set an identifier for the separator rather than using multiple
  instances of the same literal
* consistent arrow function body formatting

PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Rich Trott 2017-01-19 17:18:13 -08:00
parent 80c72c6d55
commit 6b6123cfbe

View File

@ -862,6 +862,7 @@ URLSearchParams.prototype[Symbol.iterator] = URLSearchParams.prototype.entries;
URLSearchParams.prototype[util.inspect.custom] = URLSearchParams.prototype[util.inspect.custom] =
function inspect(recurseTimes, ctx) { function inspect(recurseTimes, ctx) {
const separator = ', ';
const innerOpts = Object.assign({}, ctx); const innerOpts = Object.assign({}, ctx);
if (recurseTimes !== null) { if (recurseTimes !== null) {
innerOpts.depth = recurseTimes - 1; innerOpts.depth = recurseTimes - 1;
@ -874,13 +875,14 @@ URLSearchParams.prototype[util.inspect.custom] =
output.push(`${innerInspect(list[i])} => ${innerInspect(list[i + 1])}`); output.push(`${innerInspect(list[i])} => ${innerInspect(list[i + 1])}`);
const colorRe = /\u001b\[\d\d?m/g; const colorRe = /\u001b\[\d\d?m/g;
const length = output.reduce((prev, cur) => { const length = output.reduce(
return prev + cur.replace(colorRe, '').length + ', '.length; (prev, cur) => prev + cur.replace(colorRe, '').length + separator.length,
}, -', '.length); -separator.length
);
if (length > ctx.breakLength) { if (length > ctx.breakLength) {
return `${this.constructor.name} {\n ${output.join(',\n ')} }`; return `${this.constructor.name} {\n ${output.join(',\n ')} }`;
} else if (output.length) { } else if (output.length) {
return `${this.constructor.name} { ${output.join(', ')} }`; return `${this.constructor.name} { ${output.join(separator)} }`;
} else { } else {
return `${this.constructor.name} {}`; return `${this.constructor.name} {}`;
} }