@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/options.ts CHANGED
@@ -1,226 +1,248 @@
1
- import { cloneDeep } from "lodash-es";
2
-
3
- import { checkEmpty, handleEmptyVal, IHandleEmptyValOpt } from "./empty-val";
4
-
5
- export interface IFieldNames {
6
- value?: string;
7
- label?: string;
8
- children?: string;
9
- }
10
-
11
- export interface IFindOptionOpt extends IHandleEmptyValOpt {
12
- fieldNames?: IFieldNames;
13
- // 是否返回完整的路径数组
14
- isPathList?: boolean;
15
- pathList?: Array<any>;
16
- labelList?: Array<string>;
17
- parent?: any;
18
- }
19
-
20
- export interface IGetArrayLabels extends IFindOptionOpt {
21
- split?: string;
22
- }
23
-
24
- export interface IGetValLabelOpt extends IHandleEmptyValOpt {
25
- showEmpty?: boolean;
26
- }
27
-
28
- export type formatOptionsOptT = {
29
- fieldNames?: IFieldNames;
30
- sourceFieldNames?: IFieldNames;
31
- /**
32
- * 是否清除其他参数
33
- */
34
- isClear?: boolean;
35
- };
36
-
37
- export const DEF_FIELD_NAMES = {
38
- value: "value",
39
- label: "label",
40
- children: "children",
41
- };
42
-
43
- /**
44
- * 获取无限级数据的选中项
45
- * @param val
46
- * @param options
47
- * @param opt
48
- * @returns
49
- */
50
- export const findOptionByVal = function (
51
- val,
52
- options,
53
- opt: IFindOptionOpt = {
54
- // 是否返回完整的路径数组
55
- isPathList: false,
56
- pathList: [],
57
- labelList: [],
58
- parent: undefined,
59
- fieldNames: {
60
- label: "label",
61
- value: "value",
62
- children: "children",
63
- },
64
- },
65
- ) {
66
- if (!options) {
67
- console.warn("getDeepData 请传入 options");
68
- return;
69
- }
70
- const { fieldNames, isPathList, pathList = [], labelList = [] } = opt || {};
71
- const { label = "label", value = "value", children = "children" } = fieldNames || {};
72
- const res = options.find((it) => it[value] === val);
73
- if (res) {
74
- if (isPathList) {
75
- pathList.unshift(res);
76
- labelList.unshift(res[label]);
77
- return {
78
- last: res,
79
- pathList,
80
- labelList,
81
- };
82
- }
83
- return res;
84
- }
85
- const len = options.length;
86
- for (let i = 0; i < len; i++) {
87
- const item = options[i];
88
- const childList = item[children];
89
- if (Array.isArray(childList) && childList.length > 0) {
90
- const res = findOptionByVal(val, childList, {
91
- ...opt,
92
- parent: item,
93
- pathList,
94
- labelList,
95
- });
96
- if (res) {
97
- if (isPathList) {
98
- pathList.unshift(item);
99
- labelList.unshift(item[label]);
100
- return {
101
- last: res.last,
102
- labelList,
103
- pathList,
104
- };
105
- }
106
- return res;
107
- }
108
- }
109
- }
110
- };
111
-
112
- /**
113
- * 获取 options 中 val 对应的 label
114
- * @param {*} val
115
- * @param {Array<Object>} options
116
- * @param {Object} opt
117
- * @returns
118
- */
119
- export const getOptionLabel = function (val, options = [], opt: IFindOptionOpt = {}) {
120
- const { fieldNames = DEF_FIELD_NAMES } = opt || {};
121
- return findOptionByVal(val, options, opt)?.[fieldNames?.label || DEF_FIELD_NAMES.label];
122
- };
123
-
124
- /**
125
- * 获取 options 中的指定项 的 label
126
- * @param {*} val
127
- * @param {*} options
128
- * @param {*} opt
129
- * @returns
130
- */
131
- export function getValLabel(val, options = [], opt: IGetValLabelOpt = {}) {
132
- const { showEmpty } = opt;
133
- if (showEmpty && checkEmpty(val, opt)) {
134
- return handleEmptyVal(val, opt);
135
- }
136
- return getOptionLabel(val, options, opt);
137
- }
138
-
139
- /**
140
- * 获取 options 中的指定项 label 数组
141
- * @param {*} values
142
- * @param {*} options
143
- * @param {*} opt
144
- * @returns
145
- */
146
- export function getArrayLabels(values, options, opt: IGetArrayLabels = {}) {
147
- if (checkEmpty(values, opt)) {
148
- return handleEmptyVal(values, opt);
149
- }
150
- const { split = "、" } = opt || {};
151
- let _values = values;
152
- if (!Array.isArray(_values)) {
153
- _values = [_values];
154
- }
155
- return _values
156
- ?.map((val) => getOptionLabel(val, options, opt) ?? val)
157
- ?.filter((it) => (it ?? it) || false)
158
- ?.join(split);
159
- }
160
-
161
- /**
162
- * 格式化 options 数组
163
- */
164
- export const formatOptions = function (options, opt: formatOptionsOptT = {}) {
165
- if (!Array.isArray(options)) {
166
- console.warn("formatOptions Error: options not Array");
167
- return options;
168
- }
169
-
170
- const {
171
- fieldNames = DEF_FIELD_NAMES,
172
- sourceFieldNames = DEF_FIELD_NAMES,
173
- // 是否清除其他参数
174
- isClear,
175
- } = opt || {};
176
-
177
- const { label, value, children } = fieldNames;
178
- const { label: sLabel, value: sValue, children: sChildren } = sourceFieldNames;
179
-
180
- const _options = cloneDeep(options);
181
- const resOptions = [];
182
- _options.forEach((it) => {
183
- const _it = isClear ? {} : { ...it };
184
- _it[value] = it[sValue];
185
- _it[label] = it[sLabel];
186
- resOptions.push(_it);
187
- const childList = it[sChildren];
188
- if (childList) {
189
- _it[children] = formatOptions(childList, opt);
190
- }
191
- });
192
- return resOptions;
193
- };
194
-
195
- /**
196
- * 根据 val 获取对应的 options 项数据,并进行数据归一化处理
197
- * value, label, children
198
- * @param {string|number|boolean} val
199
- * @param {Object} field
200
- */
201
- export const getOptItByVal = function (val, options, opt) {
202
- const { fieldNames } = opt || {};
203
- const { value = "value", label = "label", children = "children" } = fieldNames || {};
204
- for (let i = 0; i < options.length; i++) {
205
- const it = options[i];
206
- if (it?.[value] === val) {
207
- return {
208
- ...it,
209
- value: it?.[value],
210
- label: it?.[label],
211
- children: it?.[children],
212
- };
213
- }
214
- // 递归处理
215
- const _children = it?.[children];
216
- if (Array.isArray(_children) && _children.length > 0) {
217
- const child = getOptItByVal(val, _children, opt);
218
- return {
219
- ...child,
220
- value: child?.[value],
221
- label: child?.[label],
222
- children: child?.[children],
223
- };
224
- }
225
- }
226
- };
1
+ import { cloneDeep } from "lodash-es";
2
+
3
+ import type { IHandleEmptyValOpt } from "./empty-val";
4
+ import { checkEmpty, handleEmptyVal } from "./empty-val";
5
+
6
+ export interface IFieldNames {
7
+ value?: string;
8
+ label?: string;
9
+ children?: string;
10
+ }
11
+
12
+ export interface IFindOptionOpt extends IHandleEmptyValOpt {
13
+ fieldNames?: IFieldNames;
14
+ // 是否返回完整的路径数组
15
+ isPathList?: boolean;
16
+ pathList?: Array<any>;
17
+ labelList?: Array<string>;
18
+ parent?: any;
19
+ }
20
+
21
+ export interface IGetArrayLabels extends IFindOptionOpt {
22
+ split?: string;
23
+ }
24
+
25
+ export interface IGetValLabelOpt extends IHandleEmptyValOpt {
26
+ showEmpty?: boolean;
27
+ }
28
+
29
+ export type formatOptionsOptT = {
30
+ fieldNames?: IFieldNames;
31
+ sourceFieldNames?: IFieldNames;
32
+ /**
33
+ * 是否清除其他参数
34
+ */
35
+ isClear?: boolean;
36
+ };
37
+
38
+ export const DEF_FIELD_NAMES = {
39
+ value: "value",
40
+ label: "label",
41
+ children: "children",
42
+ };
43
+
44
+ /**
45
+ * 获取无限级数据的选中项
46
+ * @param val
47
+ * @param options
48
+ * @param opt
49
+ * @returns
50
+ */
51
+ export const findOptionByVal = function (
52
+ val,
53
+ options,
54
+ opt: IFindOptionOpt = {
55
+ // 是否返回完整的路径数组
56
+ isPathList: false,
57
+ pathList: [],
58
+ labelList: [],
59
+ parent: undefined,
60
+ fieldNames: {
61
+ label: "label",
62
+ value: "value",
63
+ children: "children",
64
+ },
65
+ },
66
+ ) {
67
+ if (!options) {
68
+ console.warn("getDeepData 请传入 options");
69
+ return;
70
+ }
71
+ const { fieldNames, isPathList, pathList = [], labelList = [] } = opt || {};
72
+ const { label = "label", value = "value", children = "children" } = fieldNames || {};
73
+ const res = options.find((it) => it[value] === val);
74
+ if (res) {
75
+ if (isPathList) {
76
+ pathList.unshift(res);
77
+ labelList.unshift(res[label]);
78
+ return {
79
+ last: res,
80
+ pathList,
81
+ labelList,
82
+ };
83
+ }
84
+ return res;
85
+ }
86
+ const len = options.length;
87
+ for (let i = 0; i < len; i++) {
88
+ const item = options[i];
89
+ const childList = item[children];
90
+ if (Array.isArray(childList) && childList.length > 0) {
91
+ const res = findOptionByVal(val, childList, {
92
+ ...opt,
93
+ parent: item,
94
+ pathList,
95
+ labelList,
96
+ });
97
+ if (res) {
98
+ if (isPathList) {
99
+ pathList.unshift(item);
100
+ labelList.unshift(item[label]);
101
+ return {
102
+ last: res.last,
103
+ labelList,
104
+ pathList,
105
+ };
106
+ }
107
+ return res;
108
+ }
109
+ }
110
+ }
111
+ };
112
+
113
+ /**
114
+ * 在命中项上写出标准 value / label / children(保留原字段)。
115
+ * `getOptItByVal` 的归一化语义一致;供 `normalizeOption(findOptionByVal(...))` 组合使用。
116
+ * @param item 命中的 option 项
117
+ * @param fieldNames 自定义字段名映射
118
+ */
119
+ export function normalizeOption<T extends Record<string, any>>(
120
+ item: T,
121
+ fieldNames?: IFieldNames,
122
+ ): T & { value: unknown; label: unknown; children: unknown } {
123
+ const { value = "value", label = "label", children = "children" } = fieldNames || {};
124
+ return {
125
+ ...item,
126
+ value: item?.[value],
127
+ label: item?.[label],
128
+ children: item?.[children],
129
+ };
130
+ }
131
+
132
+ /**
133
+ * 获取 options 中 val 对应的 label
134
+ * @param {*} val
135
+ * @param {Array<Object>} options
136
+ * @param {Object} opt
137
+ * @returns
138
+ */
139
+ export const getOptionLabel = function (val, options = [], opt: IFindOptionOpt = {}) {
140
+ const { fieldNames = DEF_FIELD_NAMES } = opt || {};
141
+ return findOptionByVal(val, options, opt)?.[fieldNames?.label || DEF_FIELD_NAMES.label];
142
+ };
143
+
144
+ /**
145
+ * 获取 options 中的指定项 的 label
146
+ * @param {*} val
147
+ * @param {*} options
148
+ * @param {*} opt
149
+ * @returns
150
+ */
151
+ export function getValLabel(val, options = [], opt: IGetValLabelOpt = {}) {
152
+ const { showEmpty } = opt;
153
+ if (showEmpty && checkEmpty(val, opt)) {
154
+ return handleEmptyVal(val, opt);
155
+ }
156
+ return getOptionLabel(val, options, opt);
157
+ }
158
+
159
+ /**
160
+ * 获取 options 中的指定项 的 label 数组
161
+ * @param {*} values
162
+ * @param {*} options
163
+ * @param {*} opt
164
+ * @returns
165
+ */
166
+ export function getArrayLabels(values, options, opt: IGetArrayLabels = {}) {
167
+ if (checkEmpty(values, opt)) {
168
+ return handleEmptyVal(values, opt);
169
+ }
170
+ const { split = "、" } = opt || {};
171
+ let _values = values;
172
+ if (!Array.isArray(_values)) {
173
+ _values = [_values];
174
+ }
175
+ return _values
176
+ ?.map((val) => getOptionLabel(val, options, opt) ?? val)
177
+ ?.filter((it) => (it ?? it) || false)
178
+ ?.join(split);
179
+ }
180
+
181
+ /**
182
+ * 格式化 options 数组
183
+ */
184
+ export const formatOptions = function (options, opt: formatOptionsOptT = {}) {
185
+ if (!Array.isArray(options)) {
186
+ console.warn("formatOptions Error: options not Array");
187
+ return options;
188
+ }
189
+
190
+ const {
191
+ fieldNames = DEF_FIELD_NAMES,
192
+ sourceFieldNames = DEF_FIELD_NAMES,
193
+ // 是否清除其他参数
194
+ isClear,
195
+ } = opt || {};
196
+
197
+ const { label, value, children } = fieldNames;
198
+ const { label: sLabel, value: sValue, children: sChildren } = sourceFieldNames;
199
+
200
+ const _options = cloneDeep(options);
201
+ const resOptions = [];
202
+ _options.forEach((it) => {
203
+ const _it = isClear ? {} : { ...it };
204
+ _it[value] = it[sValue];
205
+ _it[label] = it[sLabel];
206
+ resOptions.push(_it);
207
+ const childList = it[sChildren];
208
+ if (childList) {
209
+ _it[children] = formatOptions(childList, opt);
210
+ }
211
+ });
212
+ return resOptions;
213
+ };
214
+
215
+ /**
216
+ * 根据 val 获取对应的 options 项数据,并进行数据归一化处理
217
+ * value, label, children
218
+ * @param {string|number|boolean} val
219
+ * @param {Object} field
220
+ */
221
+ export const getOptItByVal = function (val, options, opt) {
222
+ const { fieldNames } = opt || {};
223
+ const { value = "value", label = "label", children = "children" } = fieldNames || {};
224
+ for (let i = 0; i < options.length; i++) {
225
+ const it = options[i];
226
+ if (it?.[value] === val) {
227
+ return {
228
+ ...it,
229
+ value: it?.[value],
230
+ label: it?.[label],
231
+ children: it?.[children],
232
+ };
233
+ }
234
+ // 递归处理
235
+ const _children = it?.[children];
236
+ if (Array.isArray(_children) && _children.length > 0) {
237
+ const child = getOptItByVal(val, _children, opt);
238
+ if (child) {
239
+ return {
240
+ ...child,
241
+ value: child?.[value],
242
+ label: child?.[label],
243
+ children: child?.[children],
244
+ };
245
+ }
246
+ }
247
+ }
248
+ };
@@ -1,5 +1,5 @@
1
1
  import dayjs from "dayjs";
2
- import { Axios, AxiosRequestConfig } from "axios";
2
+ import type { Axios, AxiosRequestConfig } from "axios";
3
3
 
4
4
  import { axios } from "@hzab/data-model";
5
5
 
@@ -141,7 +141,7 @@ export function getSignature(opt: IGetSignatureOpt = {}) {
141
141
 
142
142
  export interface IGetPreviewUrlsOpt {
143
143
  previewUrl?: string;
144
- params?: Object;
144
+ params?: object;
145
145
  axios?: Axios;
146
146
  axiosConf?: AxiosRequestConfig;
147
147
  }
@@ -1,5 +1,5 @@
1
1
  import dayjs from "dayjs";
2
- import { Axios, AxiosResponse, AxiosRequestConfig } from "axios";
2
+ import type { Axios, AxiosResponse, AxiosRequestConfig } from "axios";
3
3
 
4
4
  import { axios } from "@hzab/data-model";
5
5
  import { formatDirStr } from "./uploadUtils";
@@ -66,11 +66,11 @@ export interface INormalizationOpt {
66
66
  /** 子项数据模式: 格式化对象 | file 文件格式 | url 地址 | JSON 序列化字符串 */
67
67
  itemMode: "object" | "file" | "url" | "jsonStr";
68
68
  /** 预览配置参数 */
69
- previewConfig?: Object;
69
+ previewConfig?: object;
70
70
  /** 设置预览 URL 的回调 */
71
71
  getPreviewUrl?: (file, opt) => string;
72
72
  /** 缩略图配置参数 */
73
- thumbnailUrlConf?: Object;
73
+ thumbnailUrlConf?: object;
74
74
  /** 设置缩略图 URL 的回调 */
75
75
  getThumbnailUrl?: (file, opt) => string;
76
76
  }
@@ -105,7 +105,7 @@ export const normalizationFileObj = (data: IFile, opt: INormalizationOpt) => {
105
105
  console.warn("Upload handleInputData 请传入正确的数据");
106
106
  return data;
107
107
  }
108
- let file = {
108
+ const file = {
109
109
  ...fileTpl,
110
110
  };
111
111
  // 对象字符串 转为 对象
@@ -1,5 +1,6 @@
1
1
  import { mergeDirStr, getFileNameByFileObj } from "./uploadUtils";
2
- import OssUploadUtil, { axios, IUploadOpt } from "./OssUploadUtil";
2
+ import type { IUploadOpt } from "./OssUploadUtil";
3
+ import OssUploadUtil, { axios } from "./OssUploadUtil";
3
4
 
4
5
  /**
5
6
  * oss 上传工具类