url: remove array.reduce usage · nodejs/node@77f72e0
@@ -6,8 +6,6 @@ const {
66 ArrayPrototypeJoin,
77 ArrayPrototypeMap,
88 ArrayPrototypePush,
9- ArrayPrototypeReduce,
10- ArrayPrototypeSlice,
119 Boolean,
1210 Int8Array,
1311 IteratorPrototype,
@@ -281,25 +279,19 @@ class URLSearchParamsIterator {
281279}
282280const index = this.#index;
283281const values = getURLSearchParamsList(this.#target);
284-const output = ArrayPrototypeReduce(
285-ArrayPrototypeSlice(values, index),
286-(prev, cur, i) => {
287-const key = i % 2 === 0;
288-if (this.#kind === 'key' && key) {
289-ArrayPrototypePush(prev, cur);
290-} else if (this.#kind === 'value' && !key) {
291-ArrayPrototypePush(prev, cur);
292-} else if (this.#kind === 'key+value' && !key) {
293-ArrayPrototypePush(prev, [values[index + i - 1], cur]);
294-}
295-return prev;
296-},
297-[],
298-);
299-const breakLn = StringPrototypeIncludes(inspect(output, innerOpts), '\n');
282+const output = [];
283+for (let i = index; i < values.length; i++) {
284+const isKey = ((i - index) % 2) === 0;
285+if (this.#kind === 'key') {
286+if (isKey) ArrayPrototypePush(output, values[i]);
287+} else if (!isKey) {
288+ArrayPrototypePush(output, this.#kind === 'value' ? values[i] : [values[i - 1], values[i]]);
289+}
290+}
291+const hasBreak = StringPrototypeIncludes(inspect(output, innerOpts), '\n');
300292const outputStrs = ArrayPrototypeMap(output, (p) => inspect(p, innerOpts));
301293let outputStr;
302-if (breakLn) {
294+if (hasBreak) {
303295outputStr = `\n ${ArrayPrototypeJoin(outputStrs, ',\n ')}`;
304296} else {
305297outputStr = ` ${ArrayPrototypeJoin(outputStrs, ', ')}`;
@@ -456,11 +448,10 @@ class URLSearchParams {
456448output,
457449`${innerInspect(list[i])} => ${innerInspect(list[i + 1])}`);
458450459-const length = ArrayPrototypeReduce(
460-output,
461-(prev, cur) => prev + removeColors(cur).length + separator.length,
462--separator.length,
463-);
451+let length = -separator.length;
452+for (let i = 0; i < output.length; i++) {
453+length += removeColors(output[i]).length + separator.length;
454+}
464455if (length > ctx.breakLength) {
465456return `${this.constructor.name} {\n` +
466457` ${ArrayPrototypeJoin(output, ',\n ')} }`;