@hzab/utils 1.1.6 → 1.1.7

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/src/url.ts CHANGED
@@ -1,155 +1,181 @@
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, opt) {
40
- if (!query || typeof query !== "object") {
41
- console.warn("Warn objToSearch: 请传入正确的对象");
42
- return;
43
- }
44
- // @deprecated 兼容老的入参写法
45
- let hasQuery = opt?.hasQuery;
46
- if (typeof opt === "boolean") {
47
- hasQuery = opt;
48
- }
49
- const { isEncode } = opt || {};
50
- let search = hasQuery ? "?" : "";
51
- Object.keys(query).forEach((key) => {
52
- if (search && search != "?") {
53
- search += "&";
54
- }
55
- const _key = isEncode ? encodeURIComponent(key) : key;
56
- search += _key;
57
- search += "=";
58
- const val = isEncode ? encodeURIComponent(query[key]) : query[key];
59
- search += val;
60
- });
61
- return search;
62
- }
63
-
64
- /**
65
- * 修复 query,解决 query 在 hash 后面的情况
66
- * @param url
67
- * @returns
68
- * 源数据:/#/login?a=1&b=2
69
- * 结果:/?a=1&b=2#/login
70
- */
71
- export function fixUrlQuery(url) {
72
- if (!url || typeof url !== "string") {
73
- console.warn("Warn handleUrlQuery: 请传入正确的 url 字符串");
74
- return url;
75
- }
76
- const hashIdx = url.indexOf("#");
77
- const queryIdx = url.indexOf("?");
78
-
79
- // 双 query 情况
80
- if (queryIdx < hashIdx) {
81
- let hash = url.slice(hashIdx);
82
- const hashQueryIdx = hash.indexOf("?");
83
- if (hashQueryIdx >= 0) {
84
- const _url = url.slice(0, queryIdx);
85
- const urlQuery = url.slice(queryIdx, hashIdx)?.replace(/\/$/, "");
86
- const hashQuery = hash.slice(hashQueryIdx)?.replace("?", "")?.replace(/\/$/, "");
87
- hash = hash.slice(0, hashQueryIdx);
88
- return `${_url}${urlQuery}&${hashQuery}${hash}`;
89
- }
90
- return url;
91
- }
92
-
93
- // query 在 hash # 之后
94
- if (queryIdx >= 0 && hashIdx >= 0 && queryIdx > hashIdx) {
95
- const hash = url.slice(hashIdx, queryIdx);
96
- const search = url.slice(queryIdx);
97
- return `${url.slice(0, hashIdx)}${search}${hash}`;
98
- }
99
- return url;
100
- }
101
-
102
- /**
103
- * 获取 url 中的 query 值的对象
104
- * @param query
105
- */
106
- export function searchToObj(url) {
107
- if (!url || typeof url !== "string") {
108
- console.warn("Warn getSearchParams: 请传入正确的 url 字符串.");
109
- return;
110
- }
111
- let _url = fixUrlQuery(url)
112
- // 解决 query 在 /# 之前导致 query 参数多了 / 的问题
113
- .replace("/#", "#");
114
- const queryIdx = _url.indexOf("?");
115
- const hashIdx = _url.indexOf("#");
116
- if (queryIdx < 0) {
117
- return;
118
- }
119
- // 去除 hash 相关数据及 query 的 "?"
120
- if (hashIdx >= 0 && queryIdx >= 0) {
121
- _url = _url.slice(queryIdx + 1, hashIdx);
122
- } else if (queryIdx >= 0) {
123
- _url = _url.slice(queryIdx + 1);
124
- }
125
- const res = {};
126
- _url.split("&")?.forEach((it) => {
127
- const arr = it.split("=");
128
- const key = decodeURIComponent(arr[0]);
129
- const val = decodeURIComponent(arr[1] ?? "");
130
- res[key] = val;
131
- });
132
- return res;
133
- }
134
-
135
- /**
136
- * 添加 url 中的 query 的参数
137
- * @param url
138
- * @param params
139
- * @returns url string
140
- */
141
- export function addQuery(url, params) {
142
- const newObj = { ...searchToObj(url), ...params };
143
- return replaceSearch(url, objToSearch(newObj, !!params));
144
- }
145
-
146
- /**
147
- * 添加 url 中的 query 参数,返回 search
148
- * @param url
149
- * @param params
150
- * @returns search string
151
- */
152
- export function addSearch(url: string, params: Record<string, unknown>) {
153
- const newObj = { ...searchToObj(url), ...params };
154
- return objToSearch(newObj, !!params);
155
- }
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, opt) {
40
+ if (!query || typeof query !== "object") {
41
+ console.warn("Warn objToSearch: 请传入正确的对象");
42
+ return;
43
+ }
44
+ // @deprecated 兼容老的入参写法
45
+ let hasQuery = opt?.hasQuery;
46
+ if (typeof opt === "boolean") {
47
+ hasQuery = opt;
48
+ }
49
+ const { isEncode } = opt || {};
50
+ let search = hasQuery ? "?" : "";
51
+ Object.keys(query).forEach((key) => {
52
+ if (search && search != "?") {
53
+ search += "&";
54
+ }
55
+ const _key = isEncode ? encodeURIComponent(key) : key;
56
+ search += _key;
57
+ search += "=";
58
+ const val = isEncode ? encodeURIComponent(query[key]) : query[key];
59
+ search += val;
60
+ });
61
+ return search;
62
+ }
63
+
64
+ /**
65
+ * 修复 query,解决 query 在 hash 后面的情况
66
+ * @param url
67
+ * @returns
68
+ * 源数据:/#/login?a=1&b=2
69
+ * 结果:/?a=1&b=2#/login
70
+ */
71
+ export function fixUrlQuery(url) {
72
+ if (!url || typeof url !== "string") {
73
+ console.warn("Warn handleUrlQuery: 请传入正确的 url 字符串");
74
+ return url;
75
+ }
76
+ const hashIdx = url.indexOf("#");
77
+ const queryIdx = url.indexOf("?");
78
+
79
+ // 双 query 情况
80
+ if (queryIdx < hashIdx) {
81
+ let hash = url.slice(hashIdx);
82
+ const hashQueryIdx = hash.indexOf("?");
83
+ if (hashQueryIdx >= 0) {
84
+ const _url = url.slice(0, queryIdx);
85
+ const urlQuery = url.slice(queryIdx, hashIdx)?.replace(/\/$/, "");
86
+ const hashQuery = hash.slice(hashQueryIdx)?.replace("?", "")?.replace(/\/$/, "");
87
+ hash = hash.slice(0, hashQueryIdx);
88
+ return `${_url}${urlQuery}&${hashQuery}${hash}`;
89
+ }
90
+ return url;
91
+ }
92
+
93
+ // query 在 hash # 之后
94
+ if (queryIdx >= 0 && hashIdx >= 0 && queryIdx > hashIdx) {
95
+ const hash = url.slice(hashIdx, queryIdx);
96
+ const search = url.slice(queryIdx);
97
+ return `${url.slice(0, hashIdx)}${search}${hash}`;
98
+ }
99
+ return url;
100
+ }
101
+
102
+ /**
103
+ * 获取 url 中的 query 值的对象
104
+ * 支持:完整 URL、`?a=1`、`&a=1`、纯 `a=1&b=2`
105
+ * @param query
106
+ */
107
+ export function searchToObj(url) {
108
+ if (!url || typeof url !== "string") {
109
+ console.warn("Warn getSearchParams: 请传入正确的 url 字符串.");
110
+ return;
111
+ }
112
+
113
+ let queryBody;
114
+ if (url.includes("?")) {
115
+ const _url = fixUrlQuery(url)
116
+ // 解决 query /# 之前导致 query 参数多了 / 的问题
117
+ .replace("/#", "#");
118
+ const queryIdx = _url.indexOf("?");
119
+ const hashIdx = _url.indexOf("#");
120
+ if (queryIdx < 0) {
121
+ return;
122
+ }
123
+ // 去除 hash 相关数据及 query 的 "?"
124
+ queryBody = hashIdx >= 0 ? _url.slice(queryIdx + 1, hashIdx) : _url.slice(queryIdx + 1);
125
+ } else {
126
+ // 纯 query:`a=1&b=2` / `&a=1`(无 ? 且非完整 URL)
127
+ const bare = url.replace(/^[?&]+/, "").split("#")[0];
128
+ if (!bare || !bare.includes("=") || url.includes("://")) {
129
+ return;
130
+ }
131
+ queryBody = bare;
132
+ }
133
+ if (!queryBody) {
134
+ return;
135
+ }
136
+
137
+ const res = {};
138
+ queryBody.split("&")?.forEach((it) => {
139
+ if (!it) {
140
+ return;
141
+ }
142
+ const eqIdx = it.indexOf("=");
143
+ const rawKey = eqIdx >= 0 ? it.slice(0, eqIdx) : it;
144
+ const rawVal = eqIdx >= 0 ? it.slice(eqIdx + 1) : "";
145
+ try {
146
+ res[decodeURIComponent(rawKey)] = decodeURIComponent(rawVal);
147
+ } catch {
148
+ // 非法转义序列:保留原值,避免整段解析抛错
149
+ res[rawKey] = rawVal;
150
+ }
151
+ });
152
+ return res;
153
+ }
154
+
155
+ /**
156
+ * 添加 url 中的 query 的参数
157
+ * @param url
158
+ * @param params
159
+ * @param opt.isEncode 是否对 key/value 做 encodeURIComponent(默认 false,保持 1.x 既有输出)
160
+ * @returns url string
161
+ */
162
+ export function addQuery(url, params, opt: { isEncode?: boolean } = {}) {
163
+ const newObj = { ...searchToObj(url), ...params };
164
+ return replaceSearch(url, objToSearch(newObj, { hasQuery: !!params, isEncode: opt?.isEncode }));
165
+ }
166
+
167
+ /**
168
+ * 添加 url 中的 query 参数,返回 search
169
+ * @param url
170
+ * @param params
171
+ * @param opt.isEncode 是否对 key/value 做 encodeURIComponent(默认 false,保持 1.x 既有输出)
172
+ * @returns search string
173
+ */
174
+ export function addSearch(
175
+ url: string,
176
+ params: Record<string, unknown>,
177
+ opt: { isEncode?: boolean } = {},
178
+ ) {
179
+ const newObj = { ...searchToObj(url), ...params };
180
+ return objToSearch(newObj, { hasQuery: !!params, isEncode: opt?.isEncode });
181
+ }
package/README.md DELETED
@@ -1,137 +0,0 @@
1
- # @hzab/utils
2
-
3
- 组件模板
4
-
5
- - node@16.16.0
6
-
7
- - 注意:首次克隆先执行 npm run prepare 命令
8
-
9
- # 组件
10
-
11
- ## ossUpload 示例
12
-
13
- ```jsx
14
- import OssUpload from "@hzab/utils/src/ossUpload";
15
-
16
- /**
17
- * 批量文件上传
18
- * @param {Array} files
19
- * @param {Object} opt
20
- * @returns
21
- */
22
- export async function handleOssUpload(files, opt) {
23
- const _files = files;
24
- const { ossUrl, signatureParams, ossParams, axiosConf, useHashName } = opt || {};
25
- const ossUpload = new OssUpload({
26
- axios: opt.axios,
27
- axiosConf: axiosConf,
28
- serverUrl: ossUrl || "/api/v1/common/oss/getWebOssConfig",
29
- });
30
-
31
- const promise = [];
32
- _files?.forEach((file) => {
33
- if (!file) {
34
- return;
35
- }
36
- // 数据已经是 url 的情况
37
- if (typeof file === "string" || file.ossUrl || file.url) {
38
- promise.push(Promise.resolve(file));
39
- } else {
40
- promise.push(
41
- ossUpload
42
- .upload(file, {
43
- signatureParams: {
44
- isPublic: 1,
45
- ...(signatureParams || {}),
46
- },
47
- ossParams,
48
- axiosConf,
49
- useHashName,
50
- })
51
- .then((res) => {
52
- return Promise.resolve(res?.data?.data?.fileUrl);
53
- }),
54
- );
55
- }
56
- });
57
-
58
- return Promise.all(promise).then((filePromises) => {
59
- filePromises?.forEach((fileUrl, idx) => {
60
- if (typeof fileUrl === "string") {
61
- _files[idx].ossUrl = fileUrl;
62
- _files[idx].url = fileUrl;
63
- } else if (_files[idx].url) {
64
- _files[idx].ossUrl = _files[idx].url;
65
- }
66
- });
67
- return Promise.resolve(_files);
68
- });
69
- }
70
- ```
71
-
72
- ## API
73
-
74
- ### InfoPanel Attributes
75
-
76
- | 参数 | 类型 | 必填 | 默认值 | 说明 |
77
- | ------ | ------ | ---- | ------ | ----------------- |
78
- | schema | Object | 是 | - | 数据信息的 schema |
79
-
80
- # 组件开发流程
81
-
82
- - 在 config/webpack.config.js 中按需修改 library 配置的文件名
83
- - 在 config/webpack.config.js 中按需修改 alias 配置的包名,便于本地调试
84
- - 在 tsconfig.json 中按需修改 paths 配置的包名,解决 ts 报错问题
85
- - npm run dev
86
-
87
- ## 文件目录
88
-
89
- - example 本地开发测试代码
90
- - src 组件源码
91
-
92
- ## 命令
93
-
94
- - Mac 执行该命令,设置 pre-commit 为可执行文件
95
-
96
- - npm run mac-chmod
97
- - chmod +x .husky && chmod +x .husky/pre-commit
98
-
99
- - 生成文档:npm run docs
100
- - 本地运行:npm run dev
101
-
102
- ## 发布
103
-
104
- - npm 源和云效源都需要发布
105
-
106
- - 命令:npm publish --access public
107
- - 发布目录:
108
- - src
109
-
110
- ### 迭代发布命令-版本自增
111
-
112
- - beta: 需要手动修改 package.json 中的 version,添加 -betaX 版本号。使用 npm publish --beta 发布
113
- - 0.0.x: npm run publish-patch
114
- - 0.x.0: npm run publish-minor
115
- - x.0.0: npm run publish-major
116
-
117
- ### nrm
118
-
119
- - 安装
120
- npm install -g nrm
121
- - 增加源
122
- nrm add aliyun https://packages.aliyun.com/62046985b3ead41b374a17f7/npm/npm-registry/
123
- - 切换源
124
- nrm use aliyun
125
- nrm use npm
126
- - 登录(账号密码在 https://packages.aliyun.com/npm/npm-registry/guide 查看)
127
- npm login --registry=https://packages.aliyun.com/62046985b3ead41b374a17f7/npm/npm-registry/
128
-
129
- ## 配置
130
-
131
- ### 配置文件
132
-
133
- - 本地配置文件:config/config.js
134
-
135
- ### webpack 配置文件
136
-
137
- - config/webpack.config.js
package/changelog.md DELETED
@@ -1,56 +0,0 @@
1
- # @hzab/utils@1.1.6
2
-
3
- feat: 添加sizeAdapter.js适配文件
4
-
5
- # @hzab/utils@1.1.5
6
-
7
- fix: objToSearch encode; searchToObj decode
8
-
9
- # @hzab/utils@1.1.4
10
-
11
- fix: url replace hash 重复拼接问题
12
- feat: url addSearch 往 url search 添加参数,返回结果 search
13
-
14
- # @hzab/utils@1.1.3
15
-
16
- feat: 增强 mergePreviewConfig——仅在有有效前缀时拼接;支持 data/blob/协议相对地址;兼容 baseUrl 别名
17
-
18
- # @hzab/utils@1.1.2
19
-
20
- fix: opt?.params?.hasPrePath 支持关闭内部默认地址前缀
21
-
22
- # @hzab/utils@1.1.1
23
-
24
- fix: fileName 添加 hash 判断
25
-
26
- # @hzab/utils@1.1.0
27
-
28
- refactor: lodash 改为 lodash-es
29
-
30
- # @hzab/utils@1.0.19
31
-
32
- fix: 修复图片后缀名-判断优化
33
-
34
- # @hzab/utils@1.0.18
35
-
36
- fix: 修复图片后缀名
37
-
38
- # @hzab/utils@1.0.17
39
-
40
- fix: scan-qr 引用路径修复
41
-
42
- # @hzab/utils@1.0.16
43
-
44
- feat: 图片链接添加 query
45
-
46
- # @hzab/utils@1.0.15
47
-
48
- feat: 二维码,创建路径,区分前缀
49
-
50
- # @hzab/utils@1.0.14
51
-
52
- feat: 添加压缩图片函数
53
-
54
- # @hzab/utils@1.0.13
55
-
56
- feat: upload axios ts