Skip to content

Commit b25563d

Browse files
injunchoi98targos
authored andcommitted
url: improve resolveObject with ObjectAssign
PR-URL: #54092 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
1 parent 3012d31 commit b25563d

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

lib/url.js

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
const {
2525
Boolean,
2626
Int8Array,
27+
ObjectAssign,
2728
ObjectKeys,
2829
StringPrototypeCharCodeAt,
2930
decodeURIComponent,
@@ -735,11 +736,7 @@ Url.prototype.resolveObject = function resolveObject(relative) {
735736
}
736737

737738
const result = new Url();
738-
const tkeys = ObjectKeys(this);
739-
for (let tk = 0; tk < tkeys.length; tk++) {
740-
const tkey = tkeys[tk];
741-
result[tkey] = this[tkey];
742-
}
739+
ObjectAssign(result, this);
743740

744741
// Hash is always overridden, no matter what.
745742
// even href="" will remove it.
@@ -754,12 +751,13 @@ Url.prototype.resolveObject = function resolveObject(relative) {
754751
// Hrefs like //foo/bar always cut to the protocol.
755752
if (relative.slashes && !relative.protocol) {
756753
// Take everything except the protocol from relative
757-
const rkeys = ObjectKeys(relative);
758-
for (let rk = 0; rk < rkeys.length; rk++) {
759-
const rkey = rkeys[rk];
760-
if (rkey !== 'protocol')
761-
result[rkey] = relative[rkey];
762-
}
754+
const relativeWithoutProtocol = ObjectKeys(relative).reduce((acc, key) => {
755+
if (key !== 'protocol') {
756+
acc[key] = relative[key];
757+
}
758+
return acc;
759+
}, {});
760+
ObjectAssign(result, relativeWithoutProtocol);
763761

764762
// urlParse appends trailing / to urls like http://www.example.com
765763
if (slashedProtocol.has(result.protocol) &&
@@ -781,11 +779,7 @@ Url.prototype.resolveObject = function resolveObject(relative) {
781779
// because that's known to be hostless.
782780
// anything else is assumed to be absolute.
783781
if (!slashedProtocol.has(relative.protocol)) {
784-
const keys = ObjectKeys(relative);
785-
for (let v = 0; v < keys.length; v++) {
786-
const k = keys[v];
787-
result[k] = relative[k];
788-
}
782+
ObjectAssign(result, relative);
789783
result.href = result.format();
790784
return result;
791785
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy