@hzab/utils 1.1.3 → 1.1.4
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 +5 -0
- package/package.json +1 -1
- package/src/url.ts +145 -134
package/changelog.md
CHANGED
package/package.json
CHANGED
package/src/url.ts
CHANGED
|
@@ -1,134 +1,145 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 替换 search 字符串
|
|
3
|
-
* @param url
|
|
4
|
-
* @param search
|
|
5
|
-
* @returns
|
|
6
|
-
*/
|
|
7
|
-
export function replaceSearch(url, search) {
|
|
8
|
-
if (!url || typeof url !== "string") {
|
|
9
|
-
console.warn("Warn
|
|
10
|
-
return url;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const hashIdx = _url.indexOf("#");
|
|
15
|
-
const queryIdx = _url.indexOf("?");
|
|
16
|
-
let hash = "";
|
|
17
|
-
let resUrl = _url;
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
if (hashIdx >= 0) {
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
search
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
*
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
1
|
+
/**
|
|
2
|
+
* 替换 search 字符串
|
|
3
|
+
* @param url
|
|
4
|
+
* @param search
|
|
5
|
+
* @returns
|
|
6
|
+
*/
|
|
7
|
+
export function replaceSearch(url: string, search: string) {
|
|
8
|
+
if (!url || typeof url !== "string") {
|
|
9
|
+
console.warn("Warn replaceSearch: 请传入正确的 url 字符串");
|
|
10
|
+
return url;
|
|
11
|
+
}
|
|
12
|
+
const _url = fixUrlQuery(url);
|
|
13
|
+
|
|
14
|
+
const hashIdx = _url.indexOf("#");
|
|
15
|
+
const queryIdx = _url.indexOf("?");
|
|
16
|
+
let hash = "";
|
|
17
|
+
let resUrl = _url;
|
|
18
|
+
// 无 query 时也要截掉 hash,否则后面再拼 hash 会重复
|
|
19
|
+
if (queryIdx >= 0) {
|
|
20
|
+
resUrl = _url.slice(0, queryIdx);
|
|
21
|
+
} else if (hashIdx >= 0) {
|
|
22
|
+
resUrl = _url.slice(0, hashIdx);
|
|
23
|
+
}
|
|
24
|
+
if (hashIdx >= 0) {
|
|
25
|
+
hash = _url.slice(hashIdx);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
resUrl += search?.indexOf("?") >= 0 ? "" : "?";
|
|
29
|
+
resUrl += search;
|
|
30
|
+
resUrl += hash;
|
|
31
|
+
|
|
32
|
+
return resUrl;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 把对象转为 query 参数
|
|
37
|
+
* @param query
|
|
38
|
+
*/
|
|
39
|
+
export function objToSearch(query, hasQuery) {
|
|
40
|
+
if (!query || typeof query !== "object") {
|
|
41
|
+
console.warn("Warn objToSearch: 请传入正确的对象");
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
let search = hasQuery ? "?" : "";
|
|
45
|
+
Object.keys(query).forEach((key) => {
|
|
46
|
+
if (search && search != "?") {
|
|
47
|
+
search += "&";
|
|
48
|
+
}
|
|
49
|
+
search += key;
|
|
50
|
+
search += "=";
|
|
51
|
+
search += query[key];
|
|
52
|
+
});
|
|
53
|
+
return search;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 修复 query,解决 query 在 hash 后面的情况
|
|
58
|
+
* @param url
|
|
59
|
+
* @returns
|
|
60
|
+
* 源数据:/#/login?a=1&b=2
|
|
61
|
+
* 结果:/?a=1&b=2#/login
|
|
62
|
+
*/
|
|
63
|
+
export function fixUrlQuery(url) {
|
|
64
|
+
if (!url || typeof url !== "string") {
|
|
65
|
+
console.warn("Warn handleUrlQuery: 请传入正确的 url 字符串");
|
|
66
|
+
return url;
|
|
67
|
+
}
|
|
68
|
+
const hashIdx = url.indexOf("#");
|
|
69
|
+
const queryIdx = url.indexOf("?");
|
|
70
|
+
|
|
71
|
+
// 双 query 情况
|
|
72
|
+
if (queryIdx < hashIdx) {
|
|
73
|
+
let hash = url.slice(hashIdx);
|
|
74
|
+
const hashQueryIdx = hash.indexOf("?");
|
|
75
|
+
if (hashQueryIdx >= 0) {
|
|
76
|
+
const _url = url.slice(0, queryIdx);
|
|
77
|
+
const urlQuery = url.slice(queryIdx, hashIdx)?.replace(/\/$/, "");
|
|
78
|
+
const hashQuery = hash.slice(hashQueryIdx)?.replace("?", "")?.replace(/\/$/, "");
|
|
79
|
+
hash = hash.slice(0, hashQueryIdx);
|
|
80
|
+
return `${_url}${urlQuery}&${hashQuery}${hash}`;
|
|
81
|
+
}
|
|
82
|
+
return url;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// query 在 hash # 之后
|
|
86
|
+
if (queryIdx >= 0 && hashIdx >= 0 && queryIdx > hashIdx) {
|
|
87
|
+
const hash = url.slice(hashIdx, queryIdx);
|
|
88
|
+
const search = url.slice(queryIdx);
|
|
89
|
+
return `${url.slice(0, hashIdx)}${search}${hash}`;
|
|
90
|
+
}
|
|
91
|
+
return url;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* 获取 url 中的 query 值的对象
|
|
96
|
+
* @param query
|
|
97
|
+
*/
|
|
98
|
+
export function searchToObj(url) {
|
|
99
|
+
if (!url || typeof url !== "string") {
|
|
100
|
+
console.warn("Warn getSearchParams: 请传入正确的 url 字符串.");
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
let _url = fixUrlQuery(url)
|
|
104
|
+
// 解决 query 在 /# 之前导致 query 参数多了 / 的问题
|
|
105
|
+
.replace("/#", "#");
|
|
106
|
+
const queryIdx = _url.indexOf("?");
|
|
107
|
+
const hashIdx = _url.indexOf("#");
|
|
108
|
+
if (queryIdx < 0) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
// 去除 hash 相关数据及 query 的 "?"
|
|
112
|
+
if (hashIdx >= 0 && queryIdx >= 0) {
|
|
113
|
+
_url = _url.slice(queryIdx + 1, hashIdx);
|
|
114
|
+
} else if (queryIdx >= 0) {
|
|
115
|
+
_url = _url.slice(queryIdx + 1);
|
|
116
|
+
}
|
|
117
|
+
const res = {};
|
|
118
|
+
_url.split("&")?.forEach((it) => {
|
|
119
|
+
const arr = it.split("=");
|
|
120
|
+
res[arr[0]] = arr[1];
|
|
121
|
+
});
|
|
122
|
+
return res;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* 添加 url 中的 query 的参数
|
|
127
|
+
* @param url
|
|
128
|
+
* @param params
|
|
129
|
+
* @returns url string
|
|
130
|
+
*/
|
|
131
|
+
export function addQuery(url, params) {
|
|
132
|
+
const newObj = { ...searchToObj(url), ...params };
|
|
133
|
+
return replaceSearch(url, objToSearch(newObj, !!params));
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* 添加 url 中的 query 参数,返回 search
|
|
138
|
+
* @param url
|
|
139
|
+
* @param params
|
|
140
|
+
* @returns search string
|
|
141
|
+
*/
|
|
142
|
+
export function addSearch(url: string, params: Record<string, unknown>) {
|
|
143
|
+
const newObj = { ...searchToObj(url), ...params };
|
|
144
|
+
return objToSearch(newObj, !!params);
|
|
145
|
+
}
|