@zcrkey/js-utils 0.0.5 → 0.0.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/README.md +9 -0
- package/{src/eventCenter.ts → dist/cjs/eventCenter.js} +120 -112
- package/dist/cjs/index.js +51 -0
- package/dist/cjs/objUtil.js +37 -0
- package/{src/storage.ts → dist/cjs/storage.js} +118 -101
- package/dist/cjs/treeUtil.js +145 -0
- package/{src/util.ts → dist/cjs/util.js} +170 -254
- package/dist/esm/eventCenter.d.ts +59 -0
- package/{src/index.ts → dist/esm/index.d.ts} +8 -8
- package/dist/esm/objUtil.d.ts +12 -0
- package/dist/esm/storage.d.ts +44 -0
- package/dist/esm/treeUtil.d.ts +48 -0
- package/dist/esm/util.d.ts +209 -0
- package/package.json +15 -4
- package/.dumi/global.less +0 -1396
- package/.dumirc.ts +0 -36
- package/.fatherrc.ts +0 -14
- package/.husky/commit-msg +0 -4
- package/.husky/pre-commit +0 -4
- package/.prettierignore +0 -2
- package/.stylelintrc +0 -10
- package/docs/api/eventCenter/index.md +0 -34
- package/docs/api/index.md +0 -5
- package/docs/api/storage/index.md +0 -9
- package/docs/api/storage/local.tsx +0 -91
- package/docs/api/storage/session.tsx +0 -85
- package/docs/api/treeUtil/index.md +0 -5
- package/docs/api/treeUtil/index.tsx +0 -266
- package/docs/api/util/index.md +0 -6
- package/docs/api/util/index.tsx +0 -405
- package/docs/api/util/is.tsx +0 -196
- package/docs/guide.md +0 -24
- package/src/objUtil.ts +0 -20
- package/src/treeUtil.ts +0 -164
- package/tsconfig.json +0 -18
- /package/dist/{eventCenter.d.ts → cjs/eventCenter.d.ts} +0 -0
- /package/dist/{index.d.ts → cjs/index.d.ts} +0 -0
- /package/dist/{objUtil.d.ts → cjs/objUtil.d.ts} +0 -0
- /package/dist/{storage.d.ts → cjs/storage.d.ts} +0 -0
- /package/dist/{treeUtil.d.ts → cjs/treeUtil.d.ts} +0 -0
- /package/dist/{util.d.ts → cjs/util.d.ts} +0 -0
- /package/dist/{eventCenter.js → esm/eventCenter.js} +0 -0
- /package/dist/{index.js → esm/index.js} +0 -0
- /package/dist/{objUtil.js → esm/objUtil.js} +0 -0
- /package/dist/{storage.js → esm/storage.js} +0 -0
- /package/dist/{treeUtil.js → esm/treeUtil.js} +0 -0
- /package/dist/{util.js → esm/util.js} +0 -0
- /package/dist/{index.umd.js → umd/index.umd.js} +0 -0
|
@@ -1,157 +1,162 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
|
|
29
|
+
// src/util.ts
|
|
30
|
+
var util_exports = {};
|
|
31
|
+
__export(util_exports, {
|
|
32
|
+
default: () => CrUtil
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(util_exports);
|
|
35
|
+
var import_lodash = require("lodash");
|
|
36
|
+
var import_qs = __toESM(require("qs"));
|
|
37
|
+
var CrUtil = class {
|
|
5
38
|
/**
|
|
6
39
|
* 判断是否为数组
|
|
7
40
|
* @param value
|
|
8
41
|
* @returns boolean
|
|
9
42
|
*/
|
|
10
|
-
static isArray
|
|
43
|
+
static isArray(value) {
|
|
11
44
|
return Array.isArray(value);
|
|
12
45
|
}
|
|
13
|
-
|
|
14
46
|
/**
|
|
15
47
|
* 判断是否为对象
|
|
16
48
|
* @param value
|
|
17
49
|
* @returns boolean
|
|
18
50
|
*/
|
|
19
|
-
static isObject
|
|
20
|
-
value
|
|
21
|
-
): value is T extends object ? (T extends any[] ? never : T) : never {
|
|
22
|
-
return (
|
|
23
|
-
typeof value === 'object' && value !== null && !CrUtil.isArray(value)
|
|
24
|
-
);
|
|
51
|
+
static isObject(value) {
|
|
52
|
+
return typeof value === "object" && value !== null && !CrUtil.isArray(value);
|
|
25
53
|
}
|
|
26
|
-
|
|
27
54
|
/**
|
|
28
55
|
* 判断是否为空对象
|
|
29
56
|
* @param value
|
|
30
57
|
* @returns boolean
|
|
31
58
|
*/
|
|
32
|
-
static isEmptyObject(value
|
|
59
|
+
static isEmptyObject(value) {
|
|
33
60
|
return Object.keys(value).length === 0 && value.constructor === Object;
|
|
34
61
|
}
|
|
35
|
-
|
|
36
62
|
/**
|
|
37
63
|
* 判断是否对象的属性是否全部为空
|
|
38
64
|
* @param value
|
|
39
65
|
* @returns boolean
|
|
40
66
|
*/
|
|
41
|
-
static isObjectPropertiesAllEmpty(
|
|
42
|
-
value: Record<string | number, any>,
|
|
43
|
-
): boolean {
|
|
67
|
+
static isObjectPropertiesAllEmpty(value) {
|
|
44
68
|
return Object.keys(value).every((key) => {
|
|
45
69
|
const _value = value[key];
|
|
46
|
-
return (
|
|
47
|
-
_value === undefined ||
|
|
48
|
-
_value === null ||
|
|
49
|
-
_value === '' ||
|
|
50
|
-
(_value === 0 && typeof _value === 'number') ||
|
|
51
|
-
isNaN(_value)
|
|
52
|
-
);
|
|
70
|
+
return _value === void 0 || _value === null || _value === "" || _value === 0 && typeof _value === "number" || isNaN(_value);
|
|
53
71
|
});
|
|
54
72
|
}
|
|
55
|
-
|
|
56
73
|
/**
|
|
57
74
|
* 判断是否为日期
|
|
58
75
|
* @param value
|
|
59
76
|
* @returns boolean
|
|
60
77
|
*/
|
|
61
|
-
static isDate(value
|
|
62
|
-
return Object.prototype.toString.call(value) ===
|
|
78
|
+
static isDate(value) {
|
|
79
|
+
return Object.prototype.toString.call(value) === "[object Date]";
|
|
63
80
|
}
|
|
64
|
-
|
|
65
81
|
/**
|
|
66
82
|
* 判断是否为字符串
|
|
67
83
|
* @param value
|
|
68
84
|
* @returns boolean
|
|
69
85
|
*/
|
|
70
|
-
static isString(value
|
|
71
|
-
return Object.prototype.toString.call(value) ===
|
|
86
|
+
static isString(value) {
|
|
87
|
+
return Object.prototype.toString.call(value) === "[object String]";
|
|
72
88
|
}
|
|
73
|
-
|
|
74
89
|
/**
|
|
75
90
|
* 判断是否为数字
|
|
76
91
|
* @param value
|
|
77
92
|
* @returns boolean
|
|
78
93
|
*/
|
|
79
|
-
static isNumber(value
|
|
80
|
-
return typeof value ===
|
|
94
|
+
static isNumber(value) {
|
|
95
|
+
return typeof value === "number" && !isNaN(value);
|
|
81
96
|
}
|
|
82
|
-
|
|
83
97
|
/**
|
|
84
98
|
* 判断是否为文件 File
|
|
85
99
|
* @param value
|
|
86
100
|
* @returns boolean
|
|
87
101
|
*/
|
|
88
|
-
static isFile(value
|
|
89
|
-
return Object.prototype.toString.call(value) ===
|
|
102
|
+
static isFile(value) {
|
|
103
|
+
return Object.prototype.toString.call(value) === "[object File]";
|
|
90
104
|
}
|
|
91
|
-
|
|
92
105
|
/**
|
|
93
106
|
* 判断是否为 Boolean
|
|
94
107
|
* @param value
|
|
95
108
|
* @returns boolean
|
|
96
109
|
*/
|
|
97
|
-
static isBoolean(value
|
|
98
|
-
return Object.prototype.toString.call(value) ===
|
|
110
|
+
static isBoolean(value) {
|
|
111
|
+
return Object.prototype.toString.call(value) === "[object Boolean]";
|
|
99
112
|
}
|
|
100
|
-
|
|
101
113
|
/**
|
|
102
114
|
* 判断是否为 Function
|
|
103
115
|
* @param value
|
|
104
116
|
* @returns boolean
|
|
105
117
|
*/
|
|
106
|
-
static isFunction(value
|
|
107
|
-
return typeof value ===
|
|
118
|
+
static isFunction(value) {
|
|
119
|
+
return typeof value === "function";
|
|
108
120
|
}
|
|
109
|
-
|
|
110
121
|
/**
|
|
111
122
|
* 去掉字符串前后所有空格
|
|
112
123
|
* @param str
|
|
113
124
|
* @returns string
|
|
114
125
|
*/
|
|
115
|
-
static trim(str
|
|
116
|
-
return (str +
|
|
126
|
+
static trim(str) {
|
|
127
|
+
return (str + "").replace(/(^[\s\n\t]+|[\s\n\t]+$)/g, "");
|
|
117
128
|
}
|
|
118
|
-
|
|
119
129
|
/**
|
|
120
130
|
* 获取数组为几维数组
|
|
121
131
|
* @param arr
|
|
122
132
|
* @returns number
|
|
123
133
|
*/
|
|
124
|
-
static getArrayDimension(arr
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
// 找到数组中的最大维度
|
|
134
|
+
static getArrayDimension(arr) {
|
|
135
|
+
if (!CrUtil.isArray(arr))
|
|
136
|
+
return 0;
|
|
129
137
|
let maxDimension = 0;
|
|
130
138
|
for (let i = 0; i < arr.length; i++) {
|
|
131
139
|
const dimension = CrUtil.getArrayDimension(arr[i]);
|
|
132
140
|
maxDimension = Math.max(maxDimension, dimension);
|
|
133
141
|
}
|
|
134
|
-
// 返回最大维度加1,代表当前层数
|
|
135
142
|
return maxDimension + 1;
|
|
136
143
|
}
|
|
137
|
-
|
|
138
144
|
/**
|
|
139
145
|
* 深拷贝
|
|
140
146
|
* @param value
|
|
141
147
|
* @returns
|
|
142
148
|
*/
|
|
143
|
-
static cloneDeep
|
|
144
|
-
return
|
|
149
|
+
static cloneDeep(value) {
|
|
150
|
+
return (0, import_lodash.cloneDeep)(value);
|
|
145
151
|
}
|
|
146
|
-
|
|
147
152
|
/**
|
|
148
153
|
* 深拷贝
|
|
149
154
|
* @param target
|
|
150
155
|
* @deprecated 即将移除,使用 {@link CrUtil.cloneDeep} 替代
|
|
151
156
|
* @returns
|
|
152
157
|
*/
|
|
153
|
-
static deepCopy
|
|
154
|
-
let _target
|
|
158
|
+
static deepCopy(target) {
|
|
159
|
+
let _target;
|
|
155
160
|
if (CrUtil.isArray(target)) {
|
|
156
161
|
_target = [];
|
|
157
162
|
for (let i = 0, len = target.length; i < len; i++) {
|
|
@@ -175,7 +180,6 @@ export default class CrUtil {
|
|
|
175
180
|
}
|
|
176
181
|
return _target;
|
|
177
182
|
}
|
|
178
|
-
|
|
179
183
|
/**
|
|
180
184
|
* 列表数据转树型数据
|
|
181
185
|
* @param listData
|
|
@@ -186,55 +190,40 @@ export default class CrUtil {
|
|
|
186
190
|
* @deprecated 即将移除,使用 {@link CrObjUtil.listToTree} 替代
|
|
187
191
|
* @returns
|
|
188
192
|
*/
|
|
189
|
-
static listToTreeData(
|
|
190
|
-
listData: any[],
|
|
191
|
-
settings?: {
|
|
192
|
-
idField?: string;
|
|
193
|
-
pidField?: string;
|
|
194
|
-
childrenField?: string;
|
|
195
|
-
isDeepCopy?: boolean;
|
|
196
|
-
getData?: (item: any) => any;
|
|
197
|
-
},
|
|
198
|
-
) {
|
|
193
|
+
static listToTreeData(listData, settings) {
|
|
199
194
|
const options = Object.assign(
|
|
200
195
|
{
|
|
201
|
-
idField:
|
|
202
|
-
pidField:
|
|
203
|
-
childrenField:
|
|
196
|
+
idField: "id",
|
|
197
|
+
pidField: "parentId",
|
|
198
|
+
childrenField: "children",
|
|
204
199
|
isDeepCopy: false,
|
|
205
|
-
getData: (item
|
|
200
|
+
getData: (item) => {
|
|
206
201
|
return item;
|
|
207
|
-
}
|
|
202
|
+
}
|
|
208
203
|
},
|
|
209
|
-
settings
|
|
204
|
+
settings
|
|
210
205
|
);
|
|
211
|
-
|
|
212
206
|
const { idField, pidField, childrenField, isDeepCopy, getData } = options;
|
|
213
|
-
|
|
214
207
|
if (isDeepCopy) {
|
|
215
208
|
listData = CrUtil.deepCopy(listData);
|
|
216
209
|
}
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
const pidMap = new Map<any, any[]>();
|
|
220
|
-
const idMap = new Map<any, any>();
|
|
210
|
+
const pidMap = /* @__PURE__ */ new Map();
|
|
211
|
+
const idMap = /* @__PURE__ */ new Map();
|
|
221
212
|
for (const item of listData) {
|
|
222
213
|
const pid = item[pidField];
|
|
223
214
|
const id = item[idField];
|
|
224
215
|
if (!pidMap.has(pid)) {
|
|
225
216
|
pidMap.set(pid, []);
|
|
226
217
|
}
|
|
227
|
-
pidMap.get(pid)
|
|
218
|
+
pidMap.get(pid).push(item);
|
|
228
219
|
idMap.set(id, item);
|
|
229
220
|
}
|
|
230
|
-
|
|
231
|
-
// 递归构建树 + 字段过滤
|
|
232
|
-
const buildTree = (pid: any): any[] => {
|
|
221
|
+
const buildTree = (pid) => {
|
|
233
222
|
const children = pidMap.get(pid) || [];
|
|
234
223
|
return children.map((item) => {
|
|
235
224
|
const rawChildren = buildTree(item[idField]);
|
|
236
225
|
const node = {
|
|
237
|
-
...getData(item)
|
|
226
|
+
...getData(item)
|
|
238
227
|
};
|
|
239
228
|
if (rawChildren.length > 0) {
|
|
240
229
|
node[childrenField] = rawChildren;
|
|
@@ -242,34 +231,18 @@ export default class CrUtil {
|
|
|
242
231
|
return node;
|
|
243
232
|
});
|
|
244
233
|
};
|
|
245
|
-
|
|
246
|
-
// 收集所有根节点 pid,去重
|
|
247
|
-
const rootPids = new Set<any>();
|
|
234
|
+
const rootPids = /* @__PURE__ */ new Set();
|
|
248
235
|
for (const pid of pidMap.keys()) {
|
|
249
|
-
if (pid ===
|
|
236
|
+
if (pid === void 0 || pid === null || pid === 0 || !idMap.has(pid)) {
|
|
250
237
|
rootPids.add(pid);
|
|
251
238
|
}
|
|
252
239
|
}
|
|
253
|
-
|
|
254
|
-
// 构建最终树
|
|
255
|
-
const tree: any[] = [];
|
|
240
|
+
const tree = [];
|
|
256
241
|
for (const pid of rootPids) {
|
|
257
242
|
tree.push(...buildTree(pid));
|
|
258
243
|
}
|
|
259
|
-
|
|
260
|
-
// 只构建根节点(pid 为 null / 0 / undefined / 不存在)
|
|
261
|
-
// const tree = buildTree(undefined)
|
|
262
|
-
// .concat(buildTree(null))
|
|
263
|
-
// .concat(buildTree(0))
|
|
264
|
-
// .concat(
|
|
265
|
-
// Array.from(pidMap.keys())
|
|
266
|
-
// .filter((pid) => !idMap.has(pid))
|
|
267
|
-
// .flatMap((pid) => buildTree(pid)),
|
|
268
|
-
// );
|
|
269
|
-
|
|
270
244
|
return tree;
|
|
271
245
|
}
|
|
272
|
-
|
|
273
246
|
/**
|
|
274
247
|
* 树型数据转列表数据
|
|
275
248
|
* @param treeData
|
|
@@ -280,29 +253,20 @@ export default class CrUtil {
|
|
|
280
253
|
* @deprecated 即将移除,使用 {@link CrObjUtil.treeToList} 替代
|
|
281
254
|
* @returns
|
|
282
255
|
*/
|
|
283
|
-
static treeDataToListData(
|
|
284
|
-
treeData: any[],
|
|
285
|
-
pid: string | number = 0,
|
|
286
|
-
settings?: {
|
|
287
|
-
childrenField?: string;
|
|
288
|
-
idField?: string;
|
|
289
|
-
pidField?: string;
|
|
290
|
-
isDeepCopy?: boolean;
|
|
291
|
-
},
|
|
292
|
-
) {
|
|
256
|
+
static treeDataToListData(treeData, pid = 0, settings) {
|
|
293
257
|
let options = Object.assign(
|
|
294
258
|
{
|
|
295
|
-
idField:
|
|
296
|
-
pidField:
|
|
297
|
-
childrenField:
|
|
298
|
-
isDeepCopy: false
|
|
259
|
+
idField: "id",
|
|
260
|
+
pidField: "parentId",
|
|
261
|
+
childrenField: "children",
|
|
262
|
+
isDeepCopy: false
|
|
299
263
|
},
|
|
300
|
-
settings
|
|
264
|
+
settings
|
|
301
265
|
);
|
|
302
|
-
let listData
|
|
266
|
+
let listData = [];
|
|
303
267
|
for (let i = 0; i < treeData.length; i++) {
|
|
304
268
|
const node = treeData[i];
|
|
305
|
-
const item
|
|
269
|
+
const item = { ...node };
|
|
306
270
|
item[options.pidField] = pid;
|
|
307
271
|
delete item[options.childrenField];
|
|
308
272
|
listData.push(item);
|
|
@@ -310,7 +274,7 @@ export default class CrUtil {
|
|
|
310
274
|
const childrenList = this.treeDataToListData(
|
|
311
275
|
node[options.childrenField],
|
|
312
276
|
item[options.idField],
|
|
313
|
-
settings
|
|
277
|
+
settings
|
|
314
278
|
);
|
|
315
279
|
listData.push(...childrenList);
|
|
316
280
|
}
|
|
@@ -320,7 +284,6 @@ export default class CrUtil {
|
|
|
320
284
|
}
|
|
321
285
|
return listData;
|
|
322
286
|
}
|
|
323
|
-
|
|
324
287
|
/**
|
|
325
288
|
* 获取所有父级数据(包含自身)
|
|
326
289
|
* @param listData
|
|
@@ -332,44 +295,34 @@ export default class CrUtil {
|
|
|
332
295
|
* @deprecated 即将移除,使用 {@link CrObjUtil.getFlatParentDatas} 替代
|
|
333
296
|
* @returns
|
|
334
297
|
*/
|
|
335
|
-
static getParentNodes
|
|
336
|
-
listData: R[],
|
|
337
|
-
value: number | string,
|
|
338
|
-
settings?: {
|
|
339
|
-
valueField?: string;
|
|
340
|
-
idField?: string;
|
|
341
|
-
pidField?: string;
|
|
342
|
-
getData?: (item: R) => T;
|
|
343
|
-
},
|
|
344
|
-
): T[] {
|
|
298
|
+
static getParentNodes(listData, value, settings) {
|
|
345
299
|
const options = Object.assign(
|
|
346
300
|
{
|
|
347
|
-
valueField:
|
|
348
|
-
idField:
|
|
349
|
-
pidField:
|
|
350
|
-
getData: (item
|
|
351
|
-
return item
|
|
352
|
-
}
|
|
301
|
+
valueField: "value",
|
|
302
|
+
idField: "id",
|
|
303
|
+
pidField: "parentId",
|
|
304
|
+
getData: (item) => {
|
|
305
|
+
return item;
|
|
306
|
+
}
|
|
353
307
|
},
|
|
354
|
-
settings
|
|
308
|
+
settings
|
|
355
309
|
);
|
|
356
|
-
|
|
357
310
|
if (!(listData && listData.length > 0) || !value) {
|
|
358
311
|
return [];
|
|
359
312
|
}
|
|
360
313
|
const idField = options.idField;
|
|
361
314
|
const pidField = options.pidField;
|
|
362
315
|
const valueField = options.valueField;
|
|
363
|
-
const nodes
|
|
364
|
-
let node
|
|
365
|
-
(item
|
|
316
|
+
const nodes = [];
|
|
317
|
+
let node = listData.find(
|
|
318
|
+
(item) => item[valueField] == value
|
|
366
319
|
);
|
|
367
320
|
if (node) {
|
|
368
321
|
nodes.push(options.getData(node));
|
|
369
322
|
while (node && node[pidField]) {
|
|
370
|
-
const parentOption
|
|
323
|
+
const parentOption = listData.find(
|
|
371
324
|
// eslint-disable-next-line @typescript-eslint/no-loop-func
|
|
372
|
-
(item
|
|
325
|
+
(item) => item[idField] == node[pidField]
|
|
373
326
|
);
|
|
374
327
|
if (parentOption) {
|
|
375
328
|
nodes.push(options.getData(parentOption));
|
|
@@ -380,56 +333,46 @@ export default class CrUtil {
|
|
|
380
333
|
nodes.reverse();
|
|
381
334
|
return nodes;
|
|
382
335
|
}
|
|
383
|
-
|
|
384
336
|
/**
|
|
385
337
|
* 参数序列化
|
|
386
338
|
* @param params {a:'1',b:{},c:[]}
|
|
387
339
|
* @returns
|
|
388
340
|
*/
|
|
389
|
-
static paramsSerializer(
|
|
390
|
-
params: Record<string, any>,
|
|
391
|
-
settings?: { isFilterNonNull: boolean },
|
|
392
|
-
) {
|
|
341
|
+
static paramsSerializer(params, settings) {
|
|
393
342
|
const options = Object.assign(
|
|
394
343
|
{
|
|
395
|
-
isFilterNonNull: false
|
|
344
|
+
isFilterNonNull: false
|
|
396
345
|
},
|
|
397
|
-
settings
|
|
346
|
+
settings
|
|
398
347
|
);
|
|
399
348
|
if (options.isFilterNonNull) {
|
|
400
349
|
Object.keys(params).forEach((key) => {
|
|
401
|
-
if (
|
|
402
|
-
params[key] === null ||
|
|
403
|
-
params[key] === undefined ||
|
|
404
|
-
params[key] === ''
|
|
405
|
-
) {
|
|
350
|
+
if (params[key] === null || params[key] === void 0 || params[key] === "") {
|
|
406
351
|
delete params[key];
|
|
407
352
|
}
|
|
408
353
|
});
|
|
409
354
|
}
|
|
410
|
-
return
|
|
355
|
+
return import_qs.default.stringify(params);
|
|
411
356
|
}
|
|
412
|
-
|
|
413
357
|
/**
|
|
414
358
|
* 参数解析
|
|
415
359
|
* @param {*} str
|
|
416
360
|
* @param {*} settings
|
|
417
361
|
* @returns
|
|
418
362
|
*/
|
|
419
|
-
static paramsParse(str
|
|
363
|
+
static paramsParse(str, settings) {
|
|
420
364
|
let obj = {};
|
|
421
365
|
let option = Object.assign(
|
|
422
366
|
{
|
|
423
|
-
ignoreQueryPrefix: true
|
|
367
|
+
ignoreQueryPrefix: true
|
|
424
368
|
},
|
|
425
|
-
settings
|
|
369
|
+
settings
|
|
426
370
|
);
|
|
427
371
|
if (str) {
|
|
428
|
-
obj =
|
|
372
|
+
obj = import_qs.default.parse(str, option);
|
|
429
373
|
}
|
|
430
374
|
return obj;
|
|
431
375
|
}
|
|
432
|
-
|
|
433
376
|
/**
|
|
434
377
|
* 获取 URL 参数
|
|
435
378
|
* @param {*} url
|
|
@@ -438,48 +381,45 @@ export default class CrUtil {
|
|
|
438
381
|
* "https://example.com?foo=bar&baz=qux" => {"query":{"foo":"bar","baz":"qux"},"hash":{},"all":{"foo":"bar","baz":"qux"}}
|
|
439
382
|
* "https://example.com/page?foo=1#/?a=1&b=2" => {"query":{"foo":"1"},"hash":{"/":"","a":"1","b":"2"},"all":{"foo":"1","/":"","a":"1","b":"2"}},
|
|
440
383
|
*/
|
|
441
|
-
static getQueryParams(url
|
|
442
|
-
query
|
|
443
|
-
hash
|
|
444
|
-
all
|
|
445
|
-
|
|
446
|
-
const query: Record<string, any> = {};
|
|
447
|
-
const hash: Record<string, any> = {};
|
|
448
|
-
const all: Record<string, any> = {};
|
|
449
|
-
const parse = (queryString: string) => {
|
|
384
|
+
static getQueryParams(url) {
|
|
385
|
+
const query = {};
|
|
386
|
+
const hash = {};
|
|
387
|
+
const all = {};
|
|
388
|
+
const parse = (queryString) => {
|
|
450
389
|
if (!queryString) {
|
|
451
390
|
return {};
|
|
452
391
|
}
|
|
453
392
|
try {
|
|
454
|
-
return
|
|
393
|
+
return import_qs.default.parse(queryString);
|
|
455
394
|
} catch {
|
|
456
|
-
const result
|
|
457
|
-
if (!queryString)
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
395
|
+
const result = {};
|
|
396
|
+
if (!queryString)
|
|
397
|
+
return result;
|
|
398
|
+
queryString.split("&").forEach((pair) => {
|
|
399
|
+
if (!pair)
|
|
400
|
+
return;
|
|
401
|
+
const [key, val] = pair.split("=");
|
|
461
402
|
if (key)
|
|
462
|
-
result[decodeURIComponent(key)] = decodeURIComponent(val ||
|
|
403
|
+
result[decodeURIComponent(key)] = decodeURIComponent(val || "");
|
|
463
404
|
});
|
|
464
405
|
return result;
|
|
465
406
|
}
|
|
466
407
|
};
|
|
467
408
|
try {
|
|
468
|
-
const [base, hashPart] = url.split(
|
|
469
|
-
const queryPart = base.split(
|
|
409
|
+
const [base, hashPart] = url.split("#");
|
|
410
|
+
const queryPart = base.split("?")[1];
|
|
470
411
|
Object.assign(query, parse(queryPart));
|
|
471
412
|
if (hashPart) {
|
|
472
|
-
for (const part of hashPart.split(
|
|
413
|
+
for (const part of hashPart.split("?")) {
|
|
473
414
|
Object.assign(hash, parse(part));
|
|
474
415
|
}
|
|
475
416
|
}
|
|
476
417
|
Object.assign(all, query, hash);
|
|
477
418
|
} catch (error) {
|
|
478
|
-
console.error(
|
|
419
|
+
console.error("getQueryParams:", error);
|
|
479
420
|
}
|
|
480
421
|
return { query, hash, all };
|
|
481
422
|
}
|
|
482
|
-
|
|
483
423
|
/**
|
|
484
424
|
* 版本号比较
|
|
485
425
|
* @param v1
|
|
@@ -487,15 +427,15 @@ export default class CrUtil {
|
|
|
487
427
|
* @returns number
|
|
488
428
|
* @description v1大于v2(1)、v1小于v2(-1)、v1等于v2(0)
|
|
489
429
|
*/
|
|
490
|
-
static compareVersion(v1
|
|
491
|
-
let v1Arr
|
|
492
|
-
let v2Arr
|
|
430
|
+
static compareVersion(v1, v2) {
|
|
431
|
+
let v1Arr = CrUtil.trim(v1).split(".");
|
|
432
|
+
let v2Arr = CrUtil.trim(v2).split(".");
|
|
493
433
|
const len = Math.max(v1.length, v2.length);
|
|
494
434
|
while (v1Arr.length < len) {
|
|
495
|
-
v1Arr.push(
|
|
435
|
+
v1Arr.push("0");
|
|
496
436
|
}
|
|
497
437
|
while (v2Arr.length < len) {
|
|
498
|
-
v2Arr.push(
|
|
438
|
+
v2Arr.push("0");
|
|
499
439
|
}
|
|
500
440
|
for (let i = 0; i < len; i++) {
|
|
501
441
|
const num1 = parseInt(v1Arr[i]);
|
|
@@ -508,7 +448,6 @@ export default class CrUtil {
|
|
|
508
448
|
}
|
|
509
449
|
return 0;
|
|
510
450
|
}
|
|
511
|
-
|
|
512
451
|
/**
|
|
513
452
|
* 根据 ids 获取相对应的数据名称
|
|
514
453
|
* @param data
|
|
@@ -516,35 +455,28 @@ export default class CrUtil {
|
|
|
516
455
|
* @param settings
|
|
517
456
|
* @returns string
|
|
518
457
|
*/
|
|
519
|
-
static getDataNameByIds
|
|
520
|
-
data: T[],
|
|
521
|
-
ids: (string | number)[],
|
|
522
|
-
settings?: { sep?: string; nameField?: string; idField?: string },
|
|
523
|
-
): string {
|
|
458
|
+
static getDataNameByIds(data, ids, settings) {
|
|
524
459
|
const options = Object.assign(
|
|
525
460
|
{
|
|
526
|
-
sep:
|
|
527
|
-
idField:
|
|
528
|
-
nameField:
|
|
461
|
+
sep: "、",
|
|
462
|
+
idField: "id",
|
|
463
|
+
nameField: "name"
|
|
529
464
|
},
|
|
530
|
-
settings
|
|
465
|
+
settings
|
|
531
466
|
);
|
|
532
|
-
let str =
|
|
467
|
+
let str = "";
|
|
533
468
|
if (ids && ids.length > 0) {
|
|
534
|
-
str = ids
|
|
535
|
-
.
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
})
|
|
543
|
-
.join(options.sep);
|
|
469
|
+
str = ids.map((id) => {
|
|
470
|
+
let item = data.find((item2) => item2[options.idField] == id);
|
|
471
|
+
if (item) {
|
|
472
|
+
return item[options.nameField];
|
|
473
|
+
} else {
|
|
474
|
+
return "";
|
|
475
|
+
}
|
|
476
|
+
}).join(options.sep);
|
|
544
477
|
}
|
|
545
478
|
return str;
|
|
546
479
|
}
|
|
547
|
-
|
|
548
480
|
/**
|
|
549
481
|
* 追加html标签属性值
|
|
550
482
|
* @param htmlStr
|
|
@@ -553,21 +485,16 @@ export default class CrUtil {
|
|
|
553
485
|
* @param newAttrValue
|
|
554
486
|
* @returns string
|
|
555
487
|
*/
|
|
556
|
-
static appendHtmlTagAttr(
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
newAttrValue: string,
|
|
561
|
-
) {
|
|
562
|
-
let regex = new RegExp(`(<${tagName}\\s+)([^>]*?)([^>]*>)`, 'g');
|
|
563
|
-
let replacedHtml = htmlStr.replace(regex, function (match, p1, p2, p3) {
|
|
564
|
-
let _regex = new RegExp(`${attrName}="(.*?)"`, 'g');
|
|
488
|
+
static appendHtmlTagAttr(htmlStr, tagName, attrName, newAttrValue) {
|
|
489
|
+
let regex = new RegExp(`(<${tagName}\\s+)([^>]*?)([^>]*>)`, "g");
|
|
490
|
+
let replacedHtml = htmlStr.replace(regex, function(match, p1, p2, p3) {
|
|
491
|
+
let _regex = new RegExp(`${attrName}="(.*?)"`, "g");
|
|
565
492
|
if (_regex.exec(match)) {
|
|
566
493
|
let __regex = new RegExp(
|
|
567
494
|
`(<${tagName}\\s+)([^>]*?)${attrName}="(.*)"([^>]*>)`,
|
|
568
|
-
|
|
495
|
+
"g"
|
|
569
496
|
);
|
|
570
|
-
return match.replace(__regex, function
|
|
497
|
+
return match.replace(__regex, function(_match, _p1, _p2, _p3, _p4) {
|
|
571
498
|
return _p1 + _p2 + `${attrName}="${_p3} ${newAttrValue}"` + _p4;
|
|
572
499
|
});
|
|
573
500
|
} else {
|
|
@@ -575,36 +502,31 @@ export default class CrUtil {
|
|
|
575
502
|
}
|
|
576
503
|
});
|
|
577
504
|
replacedHtml = replacedHtml.replace(
|
|
578
|
-
new RegExp(`<${tagName}>`,
|
|
579
|
-
`<${tagName} ${attrName}="${newAttrValue}"
|
|
505
|
+
new RegExp(`<${tagName}>`, "g"),
|
|
506
|
+
`<${tagName} ${attrName}="${newAttrValue}">`
|
|
580
507
|
);
|
|
581
508
|
return replacedHtml;
|
|
582
509
|
}
|
|
583
|
-
|
|
584
510
|
/**
|
|
585
511
|
* 比较数据差异
|
|
586
512
|
* @param data1
|
|
587
513
|
* @param data2
|
|
588
514
|
* @returns
|
|
589
515
|
*/
|
|
590
|
-
static compareDataDiff(data1
|
|
591
|
-
const diffFieldSet
|
|
592
|
-
const compare = (path
|
|
516
|
+
static compareDataDiff(data1, data2) {
|
|
517
|
+
const diffFieldSet = {};
|
|
518
|
+
const compare = (path, o1, o2) => {
|
|
593
519
|
if (CrUtil.isArray(o1) && CrUtil.isArray(o2)) {
|
|
594
|
-
// 如果两者都是数组,比较它们的长度和内容
|
|
595
520
|
const lengthDiff = o1.length - o2.length;
|
|
596
521
|
if (lengthDiff !== 0) {
|
|
597
|
-
|
|
598
|
-
diffFieldSet[path] = 'modified';
|
|
522
|
+
diffFieldSet[path] = "modified";
|
|
599
523
|
if (lengthDiff > 0) {
|
|
600
|
-
// o1比o2长,所以o1中有多余的元素
|
|
601
524
|
for (let i = o2.length; i < o1.length; i++) {
|
|
602
|
-
diffFieldSet[`${path}[${i}]`] =
|
|
525
|
+
diffFieldSet[`${path}[${i}]`] = "removed";
|
|
603
526
|
}
|
|
604
527
|
} else {
|
|
605
|
-
// o2比o1长,所以o2中有新增的元素
|
|
606
528
|
for (let i = o1.length; i < o2.length; i++) {
|
|
607
|
-
diffFieldSet[`${path}[${i}]`] =
|
|
529
|
+
diffFieldSet[`${path}[${i}]`] = "added";
|
|
608
530
|
}
|
|
609
531
|
}
|
|
610
532
|
} else {
|
|
@@ -613,44 +535,38 @@ export default class CrUtil {
|
|
|
613
535
|
}
|
|
614
536
|
}
|
|
615
537
|
} else if (CrUtil.isObject(o1) && CrUtil.isObject(o2)) {
|
|
616
|
-
// 如果两者都是对象,遍历它们的属性
|
|
617
|
-
// eslint-disable-next-line guard-for-in
|
|
618
538
|
for (let key in o1) {
|
|
619
539
|
const _path = path ? `${path}.${key}` : key;
|
|
620
540
|
if (Object.prototype.hasOwnProperty.call(o1, key) && !(key in o2)) {
|
|
621
|
-
diffFieldSet[_path] =
|
|
541
|
+
diffFieldSet[_path] = "removed";
|
|
622
542
|
} else if (Object.prototype.hasOwnProperty.call(o2, key)) {
|
|
623
543
|
compare(_path, o1[key], o2[key]);
|
|
624
544
|
}
|
|
625
545
|
}
|
|
626
|
-
// 检查o2中是否存在o1中没有的属性
|
|
627
|
-
// eslint-disable-next-line guard-for-in
|
|
628
546
|
for (let key in o2) {
|
|
629
547
|
const _path = path ? `${path}.${key}` : key;
|
|
630
548
|
if (Object.prototype.hasOwnProperty.call(o2, key) && !(key in o1)) {
|
|
631
|
-
diffFieldSet[_path] =
|
|
549
|
+
diffFieldSet[_path] = "added";
|
|
632
550
|
}
|
|
633
551
|
}
|
|
634
552
|
} else if (o1 !== o2) {
|
|
635
|
-
|
|
636
|
-
diffFieldSet[path] = 'modified';
|
|
553
|
+
diffFieldSet[path] = "modified";
|
|
637
554
|
}
|
|
638
555
|
};
|
|
639
|
-
compare(
|
|
556
|
+
compare("", data1, data2);
|
|
640
557
|
return diffFieldSet;
|
|
641
558
|
}
|
|
642
|
-
|
|
643
559
|
/**
|
|
644
560
|
* 格式化为千分位
|
|
645
561
|
* @param value
|
|
646
562
|
* @returns 12345.6789 => 12,345.6789
|
|
647
563
|
*/
|
|
648
|
-
static fmtThousands(value
|
|
649
|
-
if (value ==
|
|
564
|
+
static fmtThousands(value) {
|
|
565
|
+
if (value == void 0 || value == null || isNaN(Number(value))) {
|
|
650
566
|
return String(value);
|
|
651
567
|
}
|
|
652
|
-
const [intPart, decimalPart] = String(value).split(
|
|
653
|
-
const formattedInt = intPart.replace(/\B(?=(\d{3})+(?!\d))/g,
|
|
568
|
+
const [intPart, decimalPart] = String(value).split(".");
|
|
569
|
+
const formattedInt = intPart.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
654
570
|
return decimalPart ? `${formattedInt}.${decimalPart}` : formattedInt;
|
|
655
571
|
}
|
|
656
|
-
}
|
|
572
|
+
};
|