@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 CHANGED
@@ -1,3 +1,7 @@
1
+ # @hzab/utils@1.1.5
2
+
3
+ fix: objToSearch encode; searchToObj decode
4
+
1
5
  # @hzab/utils@1.1.4
2
6
 
3
7
  fix: url replace hash 重复拼接问题
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hzab/utils",
3
- "version": "1.1.4",
3
+ "version": "1.1.5",
4
4
  "description": "utils",
5
5
  "main": "src",
6
6
  "scripts": {
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, hasQuery) {
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
- search += key;
55
+ const _key = isEncode ? encodeURIComponent(key) : key;
56
+ search += _key;
50
57
  search += "=";
51
- search += query[key];
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
- res[arr[0]] = arr[1];
128
+ const key = decodeURIComponent(arr[0]);
129
+ const val = decodeURIComponent(arr[1] ?? "");
130
+ res[key] = val;
121
131
  });
122
132
  return res;
123
133
  }