40 lines
706 B
JavaScript
40 lines
706 B
JavaScript
class UrlBuilder {
|
|
constructor(url) {
|
|
this.orginurl = url;
|
|
this.baseurl = url;
|
|
this.checked = false;
|
|
}
|
|
|
|
addKV(key, value) {
|
|
if (typeof (value) == 'undefined') {
|
|
return this;
|
|
}
|
|
this._checkurl();
|
|
let str = encodeURIComponent(key) + '=' + encodeURIComponent(value);
|
|
this.baseurl += str;
|
|
return this;
|
|
}
|
|
|
|
clear() {
|
|
this.baseurl = this.orginurl;
|
|
this.checked = false;
|
|
}
|
|
|
|
_checkurl() {
|
|
if (!this.checked) {
|
|
if (this.baseurl.indexOf('?') === -1) {
|
|
this.baseurl += '?';
|
|
} else {
|
|
this.baseurl += '&';
|
|
}
|
|
this.checked = true;
|
|
} else {
|
|
this.baseurl += '&';
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
module.exports = UrlBuilder;
|
|
|