url: improve resolveObject with ObjectAssign

PR-URL: https://github.com/nodejs/node/pull/54092
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
This commit is contained in:
Early Riser 2024-07-31 15:17:59 +09:00 committed by GitHub
parent e7edcf38cd
commit b4fd1fd63f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -24,6 +24,7 @@
const { const {
Boolean, Boolean,
Int8Array, Int8Array,
ObjectAssign,
ObjectKeys, ObjectKeys,
StringPrototypeCharCodeAt, StringPrototypeCharCodeAt,
decodeURIComponent, decodeURIComponent,
@ -735,11 +736,7 @@ Url.prototype.resolveObject = function resolveObject(relative) {
} }
const result = new Url(); const result = new Url();
const tkeys = ObjectKeys(this); ObjectAssign(result, this);
for (let tk = 0; tk < tkeys.length; tk++) {
const tkey = tkeys[tk];
result[tkey] = this[tkey];
}
// Hash is always overridden, no matter what. // Hash is always overridden, no matter what.
// even href="" will remove it. // even href="" will remove it.
@ -754,12 +751,13 @@ Url.prototype.resolveObject = function resolveObject(relative) {
// Hrefs like //foo/bar always cut to the protocol. // Hrefs like //foo/bar always cut to the protocol.
if (relative.slashes && !relative.protocol) { if (relative.slashes && !relative.protocol) {
// Take everything except the protocol from relative // Take everything except the protocol from relative
const rkeys = ObjectKeys(relative); const relativeWithoutProtocol = ObjectKeys(relative).reduce((acc, key) => {
for (let rk = 0; rk < rkeys.length; rk++) { if (key !== 'protocol') {
const rkey = rkeys[rk]; acc[key] = relative[key];
if (rkey !== 'protocol') }
result[rkey] = relative[rkey]; return acc;
} }, {});
ObjectAssign(result, relativeWithoutProtocol);
// urlParse appends trailing / to urls like http://www.example.com // urlParse appends trailing / to urls like http://www.example.com
if (slashedProtocol.has(result.protocol) && if (slashedProtocol.has(result.protocol) &&
@ -781,11 +779,7 @@ Url.prototype.resolveObject = function resolveObject(relative) {
// because that's known to be hostless. // because that's known to be hostless.
// anything else is assumed to be absolute. // anything else is assumed to be absolute.
if (!slashedProtocol.has(relative.protocol)) { if (!slashedProtocol.has(relative.protocol)) {
const keys = ObjectKeys(relative); ObjectAssign(result, relative);
for (let v = 0; v < keys.length; v++) {
const k = keys[v];
result[k] = relative[k];
}
result.href = result.format(); result.href = result.format();
return result; return result;
} }