Skip to content

Commit

Permalink
url: improve resolveObject with ObjectAssign
Browse files Browse the repository at this point in the history
  • Loading branch information
EarlyRiser42 committed Jul 29, 2024
1 parent 5210cd8 commit 783ae8d
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
const {
Boolean,
Int8Array,
ObjectAssign,
ObjectKeys,
StringPrototypeCharCodeAt,
decodeURIComponent,
Expand Down Expand Up @@ -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.
Expand All @@ -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) &&
Expand All @@ -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;
}
Expand Down

0 comments on commit 783ae8d

Please sign in to comment.