Skip to content

Commit

Permalink
修改代码格式
Browse files Browse the repository at this point in the history
  • Loading branch information
ufologist committed Mar 1, 2019
1 parent a3df6cc commit 0aa1022
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/qsman.js
Expand Up @@ -62,25 +62,29 @@ class QsMan {
set(name, value) {
var kvMap = {};
kvMap[name] = value;

// 解析出 value 值
var valueQueryStringKvs = parseQueryString(kvMap);

var found = this._queryStringKvs.filter(function(kv) {
return kv.key === name;
});
if (found.length > 0) {
// 记录要设置的参数的位置
var kvIndex = this._queryStringKvs.indexOf(found[0]);

this.delete(name);
// 在追加的位置切一刀(将需要替换的元素排除掉)
var left = this._queryStringKvs.slice(0, kvIndex);
var right = this._queryStringKvs.slice(kvIndex + 1);

// 在左边追加参数
for (var i = 0, length = valueQueryStringKvs.length; i < length; i++) {
left.push(valueQueryStringKvs[i]);
}

this._queryStringKvs = [].concat(left, right);
}
else {
} else {
this.append(kvMap);
}
return this;
Expand Down Expand Up @@ -133,15 +137,15 @@ class QsMan {
sort() {
this._queryStringKvs.sort(function(kv1, kv2) {
var compareResult;

if (kv1.key > kv2.key) {
compareResult = 1;
}
else if (kv1.key < kv2.key) {
} else if (kv1.key < kv2.key) {
compareResult = -1;
}
else {
} else {
compareResult = 0;
}

return compareResult;
});
return this;
Expand All @@ -155,21 +159,25 @@ class QsMan {
toString(includeUndefinedValue) {
var url = '';
var queryStringKvs = this._queryStringKvs;

// 排除空值的 KEY
queryStringKvs = queryStringKvs.filter(function(kv) {
return kv.key;
});

var _includeUndefinedValue = typeof includeUndefinedValue === 'undefined' ? true : includeUndefinedValue;
if (!_includeUndefinedValue) {
queryStringKvs = queryStringKvs.filter(function(kv) {
return typeof kv.value !== 'undefined';
});
}

var queryString = queryStringKvs.map(function(kv) {
var key = typeof kv.key !== 'undefined' ? encodeURIComponent(kv.key) : '';
var value = typeof kv.value !== 'undefined' ? encodeURIComponent(kv.value) : '';
return typeof kv.value !== 'undefined' ? (key + '=' + value) : key;
}).join('&');

// 重组 URL
url = stripUrlQueryString(this._url);
if (queryString) {
Expand All @@ -179,6 +187,7 @@ class QsMan {
if (hash) {
url = url + '#' + hash;
}

return url;
}
/**
Expand All @@ -205,13 +214,11 @@ class QsMan {
var item = kvMap[kv.key];
if (typeof item === 'undefined') {
kvMap[kv.key] = kv.value;
}
else { // 找到多个
} else { // 找到多个
if (Object.prototype.toString.apply(item) === '[object Array]') {
item.push(kv.value);
kvMap[kv.key] = item;
}
else {
} else {
kvMap[kv.key] = [item, kv.value];
}
}
Expand Down

0 comments on commit 0aa1022

Please sign in to comment.