@hzab/utils 1.1.4 → 1.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/changelog.md +4 -0
- package/package.json +1 -1
- package/src/url.ts +14 -4
package/changelog.md
CHANGED
package/package.json
CHANGED
package/src/url.ts
CHANGED
|
@@ -36,19 +36,27 @@ export function replaceSearch(url: string, search: string) {
|
|
|
36
36
|
* 把对象转为 query 参数
|
|
37
37
|
* @param query
|
|
38
38
|
*/
|
|
39
|
-
export function objToSearch(query,
|
|
39
|
+
export function objToSearch(query, opt) {
|
|
40
40
|
if (!query || typeof query !== "object") {
|
|
41
41
|
console.warn("Warn objToSearch: 请传入正确的对象");
|
|
42
42
|
return;
|
|
43
43
|
}
|
|
44
|
+
// @deprecated 兼容老的入参写法
|
|
45
|
+
let hasQuery = opt?.hasQuery;
|
|
46
|
+
if (typeof opt === "boolean") {
|
|
47
|
+
hasQuery = opt;
|
|
48
|
+
}
|
|
49
|
+
const { isEncode } = opt || {};
|
|
44
50
|
let search = hasQuery ? "?" : "";
|
|
45
51
|
Object.keys(query).forEach((key) => {
|
|
46
52
|
if (search && search != "?") {
|
|
47
53
|
search += "&";
|
|
48
54
|
}
|
|
49
|
-
|
|
55
|
+
const _key = isEncode ? encodeURIComponent(key) : key;
|
|
56
|
+
search += _key;
|
|
50
57
|
search += "=";
|
|
51
|
-
|
|
58
|
+
const val = isEncode ? encodeURIComponent(query[key]) : query[key];
|
|
59
|
+
search += val;
|
|
52
60
|
});
|
|
53
61
|
return search;
|
|
54
62
|
}
|
|
@@ -117,7 +125,9 @@ export function searchToObj(url) {
|
|
|
117
125
|
const res = {};
|
|
118
126
|
_url.split("&")?.forEach((it) => {
|
|
119
127
|
const arr = it.split("=");
|
|
120
|
-
|
|
128
|
+
const key = decodeURIComponent(arr[0]);
|
|
129
|
+
const val = decodeURIComponent(arr[1] ?? "");
|
|
130
|
+
res[key] = val;
|
|
121
131
|
});
|
|
122
132
|
return res;
|
|
123
133
|
}
|