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