From 783ae8d25820f84f52b20db49e2e824b692caceb Mon Sep 17 00:00:00 2001 From: EarlyRiser42 Date: Mon, 29 Jul 2024 01:05:09 -0400 Subject: [PATCH] url: improve resolveObject with ObjectAssign --- lib/url.js | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/lib/url.js b/lib/url.js index 312750e86d600b..8fa8553c2b3f30 100644 --- a/lib/url.js +++ b/lib/url.js @@ -24,6 +24,7 @@ const { Boolean, Int8Array, + ObjectAssign, ObjectKeys, StringPrototypeCharCodeAt, decodeURIComponent, @@ -735,11 +736,7 @@ Url.prototype.resolveObject = function resolveObject(relative) { } const result = new Url(); - const tkeys = ObjectKeys(this); - for (let tk = 0; tk < tkeys.length; tk++) { - const tkey = tkeys[tk]; - result[tkey] = this[tkey]; - } + ObjectAssign(result, this); // Hash is always overridden, no matter what. // even href="" will remove it. @@ -754,12 +751,13 @@ Url.prototype.resolveObject = function resolveObject(relative) { // Hrefs like //foo/bar always cut to the protocol. if (relative.slashes && !relative.protocol) { // Take everything except the protocol from relative - const rkeys = ObjectKeys(relative); - for (let rk = 0; rk < rkeys.length; rk++) { - const rkey = rkeys[rk]; - if (rkey !== 'protocol') - result[rkey] = relative[rkey]; - } + const relativeWithoutProtocol = ObjectKeys(relative).reduce((acc, key) => { + if (key !== 'protocol') { + acc[key] = relative[key]; + } + return acc; + }, {}); + ObjectAssign(result, relativeWithoutProtocol); // urlParse appends trailing / to urls like http://www.example.com if (slashedProtocol.has(result.protocol) && @@ -781,11 +779,7 @@ Url.prototype.resolveObject = function resolveObject(relative) { // because that's known to be hostless. // anything else is assumed to be absolute. if (!slashedProtocol.has(relative.protocol)) { - const keys = ObjectKeys(relative); - for (let v = 0; v < keys.length; v++) { - const k = keys[v]; - result[k] = relative[k]; - } + ObjectAssign(result, relative); result.href = result.format(); return result; }