@hzab/utils 1.1.4 → 1.1.6

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,11 @@
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
+
1
9
  # @hzab/utils@1.1.4
2
10
 
3
11
  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.6",
4
4
  "description": "utils",
5
5
  "main": "src",
6
6
  "scripts": {
@@ -0,0 +1,108 @@
1
+ /**
2
+ * 获取视窗宽高
3
+ * @returns
4
+ */
5
+ export function getClientStyle() {
6
+ return {
7
+ width: document.documentElement.clientWidth || document.body.clientWidth,
8
+ height: document.documentElement.clientHeight || document.body.clientHeight,
9
+ };
10
+ }
11
+
12
+ /**
13
+ * 视窗宽高
14
+ */
15
+ export const clientStyle = getClientStyle();
16
+
17
+ window.addEventListener("resize", () => {
18
+ const { width, height } = getClientStyle();
19
+ clientStyle.width = width;
20
+ clientStyle.height = height;
21
+ });
22
+
23
+ /**
24
+ * 获取当前视窗 1vw、1vh 对应的 px
25
+ */
26
+ export function getViewportPX() {
27
+ const { width, height } = clientStyle;
28
+ return {
29
+ wPx: width / 100,
30
+ hPx: height / 100,
31
+ };
32
+ }
33
+
34
+ // 视觉稿默认的宽高 px
35
+ // TODO: 按需修改
36
+ export const DEFAULT_SIZE = {
37
+ width: 750,
38
+ height: 1334,
39
+ };
40
+
41
+ export function setDefaultSize(width, height) {
42
+ DEFAULT_SIZE.width = width || 750;
43
+ DEFAULT_SIZE.height = height || 1334;
44
+ }
45
+
46
+ /**
47
+ * 获取视窗与视觉稿的比例
48
+ * @param current
49
+ * @param defaultVal
50
+ * @returns
51
+ */
52
+ export function getClientRate(current, defaultVal) {
53
+ return parseFloat(current) / defaultVal;
54
+ }
55
+
56
+ /**
57
+ * 获取默认宽度对应的 vw 值
58
+ */
59
+ export function getVW(pxNum = 0) {
60
+ return `${getClientRate(pxNum, DEFAULT_SIZE.width) * 100}vw`;
61
+ }
62
+
63
+ /**
64
+ * 获取默认高度对应的 vh 值
65
+ */
66
+ export function getVH(pxNum = 0) {
67
+ return `${getClientRate(pxNum, DEFAULT_SIZE.height) * 100}vh`;
68
+ }
69
+
70
+ /**
71
+ * 获取默认宽度对应的 px 值
72
+ * 适用于必须传入 px 值的情况,如 ECharts
73
+ */
74
+ export function getWpx(pxNum = 0) {
75
+ return getClientRate(pxNum, DEFAULT_SIZE.width) * clientStyle.width;
76
+ }
77
+
78
+ /**
79
+ * 获取默认高度对应的 px 值
80
+ * 适用于必须传入 px 值的情况,如 ECharts
81
+ */
82
+ export function getHpx(pxNum = 0) {
83
+ return getClientRate(pxNum, DEFAULT_SIZE.height) * clientStyle.height;
84
+ }
85
+
86
+ /**
87
+ * 通过 vw 获取对应的 px
88
+ * @param {number} vw
89
+ */
90
+ export function getHPxByVW(vw = 0) {
91
+ let _vw = vw;
92
+ if (typeof _vw === "string") {
93
+ _vw = parseFloat(_vw);
94
+ }
95
+ return clientStyle.width * (_vw / 100);
96
+ }
97
+
98
+ /**
99
+ * 通过 vh 获取对应的 px
100
+ * @param {number} vh
101
+ */
102
+ export function getWPxByVH(vh = 0) {
103
+ let _vh = vh;
104
+ if (typeof _vh === "string") {
105
+ _vh = parseFloat(_vh);
106
+ }
107
+ return clientStyle.height * (_vh / 100);
108
+ }
@@ -14,7 +14,7 @@ class OssUpload extends OssUploadUtil {
14
14
 
15
15
  upload(file, opt: IUploadOpt = {}): Promise<{ fileUrl: string }> {
16
16
  const fileName = getFileNameByFileObj(file, opt);
17
- return this._upload(file, { ...opt, fileName });
17
+ return this._upload(file, { fileName, ...opt });
18
18
  }
19
19
  }
20
20
 
@@ -117,15 +117,20 @@ export const getFileNameByFileObj = (file: IFileNameSource, opt?: IGetFileNameBy
117
117
  } = opt || {};
118
118
 
119
119
  const _fullFileName = file.name || fileName || "";
120
+
120
121
  const { mainPart, ext } = getFileNameExt(_fullFileName);
121
122
 
122
123
  console.log("getFileTypeExt(file.type)", file.type, getFileTypeExt(file.type));
123
124
 
124
125
  const _fileName = mainPart || _fullFileName || getFileName(file.url) || "file";
126
+
125
127
  const _ext = ext || getFileTypeExt(file.type);
126
128
 
127
129
  const hashStr = useHashName ? nanoidNumALetters() : "";
128
- const nm = mergeFileName(_fileName + "." + _ext, (_fileName.indexOf("~kid-") < 0 ? `~kid-${hashStr}-${file.createTime || dayjs().valueOf()}-${ext}` : ""));
130
+ const nm = mergeFileName(
131
+ _fileName + "." + _ext,
132
+ _fileName.indexOf("~kid-") < 0 ? `~kid-${hashStr}-${file.createTime || dayjs().valueOf()}-${ext}` : "",
133
+ );
129
134
 
130
135
  return nm;
131
136
  };
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
  }