@hzab/utils 1.1.5 → 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.
@@ -0,0 +1,142 @@
1
+ import _ from "lodash";
2
+ import dayjs from "dayjs";
3
+ import advancedFormat from "dayjs/plugin/advancedFormat";
4
+ import weekOfYear from "dayjs/plugin/weekOfYear";
5
+
6
+ dayjs.extend(advancedFormat);
7
+ dayjs.extend(weekOfYear);
8
+
9
+ const dateFormatEnum: Record<string, string> = {
10
+ time: "YYYY-MM-DD HH:mm:ss",
11
+ date: "YYYY-MM-DD",
12
+ week: "YYYY年 w周",
13
+ quarter: "YYYY年 Q季度",
14
+ month: "YYYY-MM",
15
+ year: "YYYY",
16
+ };
17
+
18
+ /**
19
+ * 获取 field 对应的值
20
+ * @param {Object} field schema 项
21
+ * @param {Object} data 数据
22
+ * @param {Object} opt 配置项
23
+ * @returns
24
+ */
25
+ export function getVal(field: any = {}, data: any = {}, opt: any = {}) {
26
+ let val = _.get(data, field.name);
27
+ const xComponent = field["x-component"];
28
+ const xComponentProps = field["x-component-props"] || {};
29
+
30
+ if (val && (xComponent === "DatePicker" || xComponent === "DatePicker.RangePicker")) {
31
+ const { picker, showTime } = xComponentProps || {};
32
+ const mode = showTime === true ? "time" : picker || "date";
33
+ const _formatEnum = opt.dateFormatEnum || dateFormatEnum;
34
+ const format = xComponentProps?.format || _formatEnum[mode];
35
+
36
+ if (Array.isArray(val)) {
37
+ return val.map((it) => getDateVal(it, format)).join(" ~ ");
38
+ }
39
+ return getDateVal(val, format);
40
+ }
41
+
42
+ if (val && (xComponent === "TimePicker" || xComponent === "TimePicker.RangePicker")) {
43
+ const format = xComponentProps?.format || "HH:mm:ss";
44
+ if (Array.isArray(val)) {
45
+ return val.map((it) => dayjs(it).format(format)).join(" ~ ");
46
+ }
47
+ return dayjs(val).format(format);
48
+ }
49
+
50
+ if (xComponent === "Switch") {
51
+ if (val === undefined || val === false || val === field.inactiveValue) {
52
+ return field.inactiveText || "否";
53
+ }
54
+ if (val === true || val === field.activeValue) {
55
+ return field.activeText || "是";
56
+ }
57
+ return val;
58
+ }
59
+ if (typeof field.relation === "object") {
60
+ const { key, name, label } = field.relation;
61
+ return data[key]?.[name];
62
+ }
63
+
64
+ const dataSource = field?.dataSource || field.enum;
65
+ if (
66
+ (xComponent === "Select" || xComponent === "Radio.Group" || xComponent === "Checkbox.Group") &&
67
+ Array.isArray(dataSource)
68
+ ) {
69
+ if (!Array.isArray(val)) {
70
+ return getFieldOptItByVal(val, field, opt)?.label || val;
71
+ } else if (Array.isArray(val)) {
72
+ const _val: any[] = [];
73
+ val.forEach((valIt) => {
74
+ _val.push(getFieldOptItByVal(val, field, opt)?.label || valIt);
75
+ });
76
+ val = _val;
77
+ }
78
+ }
79
+
80
+ if (Array.isArray(val)) {
81
+ return val.join("、");
82
+ }
83
+
84
+ return val;
85
+ }
86
+
87
+ export function getDateVal(val: any, format: any) {
88
+ return dayjs(val).format(format);
89
+ }
90
+
91
+ /**
92
+ * 根据 val 获取对应的 options 项数据
93
+ * @param {string|number|boolean} val
94
+ * @param {Object} field
95
+ */
96
+ export const getFieldOptItByVal = function (val: any, field: any, opt: any) {
97
+ if (!field) {
98
+ return {};
99
+ }
100
+ const options = Array.isArray(field.enum)
101
+ ? field.enum
102
+ : Array.isArray(field.options)
103
+ ? field.options
104
+ : field.dataSource || [];
105
+ return getOptItByVal(val, options, {
106
+ fieldNames: field.fieldNames || field["x-component-props"]?.fieldNames,
107
+ ...opt,
108
+ });
109
+ };
110
+
111
+ /**
112
+ * 根据 val 获取对应的 options 项数据,并进行数据归一化处理
113
+ * value, label, children
114
+ * @param {string|number|boolean} val
115
+ * @param {Object} field
116
+ */
117
+ export const getOptItByVal = function (val: any, options: any, opt: any) {
118
+ const { fieldNames } = opt || {};
119
+ const { value = "value", label = "label", children = "children" } = fieldNames || {};
120
+ for (let i = 0; i < options.length; i++) {
121
+ const it = options[i];
122
+ if (it?.[value] === val) {
123
+ return {
124
+ ...it,
125
+ value: it?.[value],
126
+ label: it?.[label],
127
+ children: it?.[children],
128
+ };
129
+ }
130
+ // 递归处理
131
+ const _children = it?.[children];
132
+ if (Array.isArray(_children) && _children.length > 0) {
133
+ const child = getOptItByVal(val, _children, opt);
134
+ return {
135
+ ...child,
136
+ value: child?.[value],
137
+ label: child?.[label],
138
+ children: child?.[children],
139
+ };
140
+ }
141
+ }
142
+ };
@@ -0,0 +1,9 @@
1
+ export {};
2
+ declare global {
3
+ interface Window {
4
+ utilsGlobalServeConfig?: Record<string, any>;
5
+ _$offlineSignatureRes?: any;
6
+ _$ossSignatureRes?: any;
7
+ formilyGlobalComponents?: Record<string, any>;
8
+ }
9
+ }
@@ -149,7 +149,7 @@ export function convert15to18(id15) {
149
149
  }
150
150
 
151
151
  // 年份前加完整的四位年份
152
- let id17 = id15.substring(0, 6) + fullYear + id15.substring(8);
152
+ const id17 = id15.substring(0, 6) + fullYear + id15.substring(8);
153
153
  // 计算校验码
154
154
  const checkCode = calculateCheckCode(id17);
155
155
  return id17 + checkCode;
package/src/img-utils.ts CHANGED
@@ -108,7 +108,7 @@ export function setDomWH(dom: HTMLElement, w: number, h: number) {
108
108
  * @returns {Promise<Blob>}
109
109
  */
110
110
  export function getCanvasPic(canvas: HTMLCanvasElement): Blob {
111
- var gl = canvas.getContext("2d", { preserveDrawingBuffer: true });
111
+ const gl = canvas.getContext("2d", { preserveDrawingBuffer: true });
112
112
 
113
113
  return dataURItoBlob(canvas.toDataURL(IMAGE_TYPES.PNG, 1));
114
114
  }
@@ -119,11 +119,11 @@ export function getCanvasPic(canvas: HTMLCanvasElement): Blob {
119
119
  * @returns
120
120
  */
121
121
  export function dataURItoBlob(base64: string): Blob {
122
- let arr = base64.split(",");
123
- let mime = arr[0]?.match(/:(.*?);/)?.[1];
124
- let bstr = atob(arr[1]);
122
+ const arr = base64.split(",");
123
+ const mime = arr[0]?.match(/:(.*?);/)?.[1];
124
+ const bstr = atob(arr[1]);
125
125
  let n = bstr.length;
126
- let u8arr = new Uint8Array(n);
126
+ const u8arr = new Uint8Array(n);
127
127
  while (n--) {
128
128
  u8arr[n] = bstr.charCodeAt(n);
129
129
  }
@@ -153,7 +153,7 @@ export function getImgEle(imgSrc: string): Promise<HTMLImageElement> {
153
153
  export function getImgBase64(imgSrc: string): Promise<string> {
154
154
  return getImgEle(imgSrc).then((img) => {
155
155
  const canvas = document.createElement("canvas");
156
- let { width, height } = getDomWH(img);
156
+ const { width, height } = getDomWH(img);
157
157
  setDomWH(canvas, width, height);
158
158
  const ctx = canvas.getContext("2d");
159
159
  ctx && ctx.drawImage(img, 0, 0, width, height, 0, 0, width, height);
@@ -169,7 +169,7 @@ export function getImgBase64(imgSrc: string): Promise<string> {
169
169
  export function getCanvas(imgSrc: string): Promise<HTMLCanvasElement> {
170
170
  return getImgEle(imgSrc).then((img) => {
171
171
  const canvas = document.createElement("canvas");
172
- let { width, height } = getDomWH(img);
172
+ const { width, height } = getDomWH(img);
173
173
  setDomWH(canvas, width, height);
174
174
  const ctx = canvas.getContext("2d");
175
175
  ctx && ctx.drawImage(img, 0, 0, width, height, 0, 0, width, height);
@@ -184,7 +184,7 @@ export function getCanvas(imgSrc: string): Promise<HTMLCanvasElement> {
184
184
  */
185
185
  export function getCanvasByImg(img: HTMLImageElement): HTMLCanvasElement {
186
186
  const canvas = document.createElement("canvas");
187
- let { width, height } = getDomWH(img);
187
+ const { width, height } = getDomWH(img);
188
188
  setDomWH(canvas, width, height);
189
189
  const ctx = canvas.getContext("2d");
190
190
  ctx && ctx.drawImage(img, 0, 0, width, height, 0, 0, width, height);
@@ -294,7 +294,7 @@ export async function imgToBase64(img: string | HTMLImageElement | HTMLCanvasEle
294
294
  } else if (img instanceof HTMLVideoElement) {
295
295
  // 从视频中获取图片
296
296
  const canvas = document.createElement("canvas");
297
- let { width, height } = getDomWH(img);
297
+ const { width, height } = getDomWH(img);
298
298
  setDomWH(canvas, width, height);
299
299
  const ctx = canvas.getContext("2d");
300
300
  ctx && ctx.drawImage(img, 0, 0, width, height, 0, 0, width, height);
package/src/index.ts ADDED
@@ -0,0 +1,43 @@
1
+ // @hzab/utils 根入口(新增公共入口,兼容 `@hzab/utils` 裸导入)。
2
+ // 注意:此处只聚合"无命名冲突"的叶子工具模块;formily/upload 等含重复导出名
3
+ // (如 getSignature/getPreviewUrl)的模块仍走深路径 `@hzab/utils/src/<sub>`。
4
+ export * from "./array";
5
+ export * from "./base64Str";
6
+ export * from "./blob";
7
+ export * from "./color";
8
+ export * from "./empty-val";
9
+ export * from "./globalConfig";
10
+ export * from "./idCardVerify";
11
+ export * from "./img-utils";
12
+ export * from "./locationUtils";
13
+ export * from "./nanoid";
14
+ export * from "./options";
15
+ export * from "./scan-qr";
16
+ export * from "./sizeAdapter";
17
+ export * from "./string";
18
+ export * from "./url";
19
+
20
+ // file 族:含 `IFile` 等与 `upload/*` 重名的导出,故不用 `export *` 聚合,
21
+ // 改为显式具名再导出,既避免命名冲突又让 2.0 线包可从根入口取用。
22
+ export {
23
+ getFileExt,
24
+ getFileName,
25
+ getFullFileName,
26
+ getUFileName,
27
+ getFileNameExt,
28
+ mergeFileName,
29
+ } from "./file/fileName";
30
+ export {
31
+ getFileType,
32
+ getFileTypeExt,
33
+ getFileTypeStr,
34
+ checkUrlSuffix,
35
+ checkImageUrl,
36
+ checkVideoUrl,
37
+ checkAudioUrl,
38
+ isValidMediaType,
39
+ TYPE_VIDEO,
40
+ TYPE_IMG,
41
+ TYPE_AUDIO,
42
+ } from "./file/fileType";
43
+ export { getFileURL, type IFile } from "./file/file";
package/src/nanoid.ts CHANGED
@@ -1,19 +1,19 @@
1
- import { customAlphabet } from "nanoid";
2
-
3
- /**
4
- * 指定 nanoid 字符集合
5
- * 数字和大小写字母
6
- */
7
- export const numALetters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
8
-
9
- /** nanoid 数字和大小写字母 */
10
- export const nanoidNumALetters = customAlphabet(numALetters, 8);
11
-
12
- /**
13
- * 指定 nanoid 字符集合
14
- * 数字和大小写字母
15
- */
16
- export const numALettersAUrl = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_~";
17
-
18
- /** nanoid url 参数规范 */
19
- export const nanoidUrl = customAlphabet(numALettersAUrl, 20);
1
+ import { customAlphabet } from "nanoid";
2
+
3
+ /**
4
+ * 指定 nanoid 字符集合
5
+ * 数字和大小写字母
6
+ */
7
+ export const numALetters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
8
+
9
+ /** nanoid 数字和大小写字母 */
10
+ export const nanoidNumALetters = customAlphabet(numALetters, 8);
11
+
12
+ /**
13
+ * 指定 nanoid 字符集合
14
+ * 数字和大小写字母
15
+ */
16
+ export const numALettersAUrl = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_~";
17
+
18
+ /** nanoid url 参数规范 */
19
+ export const nanoidUrl = customAlphabet(numALettersAUrl, 20);