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

1 file changed

Lines changed: 10 additions & 16 deletions

File tree

‎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
constresult=newUrl();
738-
consttkeys=ObjectKeys(this);
739-
for(lettk=0;tk<tkeys.length;tk++){
740-
consttkey=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-
constrkeys=ObjectKeys(relative);
758-
for(letrk=0;rk<rkeys.length;rk++){
759-
constrkey=rkeys[rk];
760-
if(rkey!=='protocol')
761-
result[rkey]=relative[rkey];
762-
}
754+
constrelativeWithoutProtocol=ObjectKeys(relative).reduce((acc,key)=>{
755+
if(key!=='protocol'){
756+
acc[key]=relative[key];
757+
}
758+
returnacc;
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-
constkeys=ObjectKeys(relative);
785-
for(letv=0;v<keys.length;v++){
786-
constk=keys[v];
787-
result[k]=relative[k];
788-
}
782+
ObjectAssign(result,relative);
789783
result.href=result.format();
790784
returnresult;
791785
}

0 commit comments

Comments
 (0)