@hzab/utils 1.1.1 → 1.1.3

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.
@@ -1,26 +1,26 @@
1
- export const DEFAULT_POSITION = {
2
- lon: 120.168893,
3
- lat: 30.225404,
4
- addr: "浙江省杭州市上城区南星街道杭州西湖风景名胜区",
5
- };
6
-
7
- /**
8
- * 获取当前定位
9
- * @param config
10
- * @returns
11
- */
12
- export function getCurrentPosition(config?: any): Promise<any> {
13
- return new Promise((resolve, reject) => {
14
- navigator?.geolocation?.getCurrentPosition(
15
- (position) => {
16
- resolve(position);
17
- },
18
- reject,
19
- {
20
- enableHighAccuracy: true,
21
- timeout: 5000,
22
- ...config,
23
- },
24
- );
25
- });
26
- }
1
+ export const DEFAULT_POSITION = {
2
+ lon: 120.168893,
3
+ lat: 30.225404,
4
+ addr: "浙江省杭州市上城区南星街道杭州西湖风景名胜区",
5
+ };
6
+
7
+ /**
8
+ * 获取当前定位
9
+ * @param config
10
+ * @returns
11
+ */
12
+ export function getCurrentPosition(config?: any): Promise<any> {
13
+ return new Promise((resolve, reject) => {
14
+ navigator?.geolocation?.getCurrentPosition(
15
+ (position) => {
16
+ resolve(position);
17
+ },
18
+ reject,
19
+ {
20
+ enableHighAccuracy: true,
21
+ timeout: 5000,
22
+ ...config,
23
+ },
24
+ );
25
+ });
26
+ }
package/src/options.ts CHANGED
@@ -1,226 +1,226 @@
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 { 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
+ };
package/src/string.ts CHANGED
@@ -1,31 +1,31 @@
1
- /**
2
- * 字符串中划线转为小驼峰
3
- * @param s
4
- * @returns
5
- */
6
- export const convertToCamelCase = function (s) {
7
- return s.replace(/-(\w)/g, function (all, letter) {
8
- return letter.toUpperCase();
9
- });
10
- };
11
-
12
- /**
13
- * 对象字符串转为对象
14
- * @param objStr
15
- */
16
- export const objStrToObj = function (objStr) {
17
- let _objStr = objStr;
18
- if (typeof _objStr === "object") {
19
- return _objStr;
20
- }
21
- // 对象字符串 转为 对象
22
- if (typeof _objStr === "string") {
23
- _objStr = _objStr.trim();
24
- if (/^\[.*\]$|^\{.*\}$/.test(_objStr)) {
25
- try {
26
- _objStr = JSON.parse(_objStr);
27
- } catch (error) {}
28
- }
29
- }
30
- return _objStr;
31
- };
1
+ /**
2
+ * 字符串中划线转为小驼峰
3
+ * @param s
4
+ * @returns
5
+ */
6
+ export const convertToCamelCase = function (s) {
7
+ return s.replace(/-(\w)/g, function (all, letter) {
8
+ return letter.toUpperCase();
9
+ });
10
+ };
11
+
12
+ /**
13
+ * 对象字符串转为对象
14
+ * @param objStr
15
+ */
16
+ export const objStrToObj = function (objStr) {
17
+ let _objStr = objStr;
18
+ if (typeof _objStr === "object") {
19
+ return _objStr;
20
+ }
21
+ // 对象字符串 转为 对象
22
+ if (typeof _objStr === "string") {
23
+ _objStr = _objStr.trim();
24
+ if (/^\[.*\]$|^\{.*\}$/.test(_objStr)) {
25
+ try {
26
+ _objStr = JSON.parse(_objStr);
27
+ } catch (error) {}
28
+ }
29
+ }
30
+ return _objStr;
31
+ };