@jhqn/utils-core 0.0.3-beta.1 → 0.0.3-beta.11
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/dist/index.cjs +127 -57
- package/dist/index.d.cts +199 -138
- package/dist/index.d.mts +199 -138
- package/dist/index.d.ts +199 -138
- package/dist/index.mjs +114 -54
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -2,22 +2,22 @@
|
|
|
2
2
|
|
|
3
3
|
const detectBrowser = require('detect-browser');
|
|
4
4
|
const dayjs = require('dayjs');
|
|
5
|
-
const
|
|
6
|
-
const duration = require('dayjs/plugin/duration');
|
|
7
|
-
const relativeTime = require('dayjs/plugin/relativeTime');
|
|
5
|
+
const zhCN = require('dayjs/locale/zh-cn');
|
|
8
6
|
const customParseFormat = require('dayjs/plugin/customParseFormat');
|
|
7
|
+
const duration = require('dayjs/plugin/duration');
|
|
8
|
+
const localeData = require('dayjs/plugin/localeData');
|
|
9
9
|
const objectSupport = require('dayjs/plugin/objectSupport');
|
|
10
|
-
const
|
|
10
|
+
const relativeTime = require('dayjs/plugin/relativeTime');
|
|
11
11
|
|
|
12
12
|
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
13
13
|
|
|
14
14
|
const dayjs__default = /*#__PURE__*/_interopDefaultCompat(dayjs);
|
|
15
|
-
const
|
|
16
|
-
const duration__default = /*#__PURE__*/_interopDefaultCompat(duration);
|
|
17
|
-
const relativeTime__default = /*#__PURE__*/_interopDefaultCompat(relativeTime);
|
|
15
|
+
const zhCN__default = /*#__PURE__*/_interopDefaultCompat(zhCN);
|
|
18
16
|
const customParseFormat__default = /*#__PURE__*/_interopDefaultCompat(customParseFormat);
|
|
17
|
+
const duration__default = /*#__PURE__*/_interopDefaultCompat(duration);
|
|
18
|
+
const localeData__default = /*#__PURE__*/_interopDefaultCompat(localeData);
|
|
19
19
|
const objectSupport__default = /*#__PURE__*/_interopDefaultCompat(objectSupport);
|
|
20
|
-
const
|
|
20
|
+
const relativeTime__default = /*#__PURE__*/_interopDefaultCompat(relativeTime);
|
|
21
21
|
|
|
22
22
|
const snake2Kebab = (str) => str.replace(/_([a-z])/g, (_, letter) => `-${letter.toLowerCase()}`);
|
|
23
23
|
const snake2Pascal = (str) => str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase()).replace(/^[a-z]/, (letter) => letter.toUpperCase());
|
|
@@ -115,22 +115,26 @@ const isURL = (val) => getVariableType(val) === "url";
|
|
|
115
115
|
const isError = (val) => getVariableType(val) === "error";
|
|
116
116
|
const isDef = (val) => !isNull(val) && !isUndefined(val);
|
|
117
117
|
const isJSONStr = (str) => {
|
|
118
|
-
if (isDef(str))
|
|
118
|
+
if (isDef(str)) {
|
|
119
119
|
try {
|
|
120
120
|
JSON.parse(str);
|
|
121
121
|
return true;
|
|
122
122
|
} catch (e) {
|
|
123
|
+
jError("isJSONStr", e);
|
|
123
124
|
return false;
|
|
124
125
|
}
|
|
126
|
+
}
|
|
125
127
|
return false;
|
|
126
128
|
};
|
|
127
129
|
const isArrStr = (str) => {
|
|
128
|
-
if (isDef(str))
|
|
130
|
+
if (isDef(str)) {
|
|
129
131
|
try {
|
|
130
132
|
return Array.isArray(JSON.parse(str));
|
|
131
133
|
} catch (e) {
|
|
134
|
+
jError("isArrStr", e);
|
|
132
135
|
return false;
|
|
133
136
|
}
|
|
137
|
+
}
|
|
134
138
|
return false;
|
|
135
139
|
};
|
|
136
140
|
const isValidKey = (key, object) => key in object;
|
|
@@ -138,13 +142,18 @@ const isEmpty = (val) => {
|
|
|
138
142
|
return !isDef(val) || val === "" || isArray(val) && val.length === 0 || isObject(val) && Object.keys(val).length === 0 || isMap(val) && val.size === 0 || isSet(val) && val.size === 0;
|
|
139
143
|
};
|
|
140
144
|
|
|
141
|
-
const isValidResCode = (res, code = 200) => res.code === code;
|
|
142
|
-
const isValidRes = (res, code) => isValidResCode(res, code) && isDef(res.data);
|
|
143
|
-
const isValidArrRes = (res, code) => isValidResCode(res, code) && Array.isArray(res.data);
|
|
144
|
-
const isValidPageRes = (res, code) => isValidResCode(res, code) && isDef(res.data) && Array.isArray(res.data.list || res.data.records);
|
|
145
|
-
|
|
146
145
|
const cnPhoneRE = /^1[3-9]\d{9}$/;
|
|
147
|
-
const cnTelRE = /^0\d{
|
|
146
|
+
const cnTelRE = /^0\d{2,3}-?\d{7,8}$/;
|
|
147
|
+
const cnTelRE2 = /^\(0\d{2,3}\)\d{7,8}$/;
|
|
148
|
+
const cnUSCIRE = /^[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}$/;
|
|
149
|
+
const cnIdCard15Re = /^[1-9]\d{7}(?:0[1-9]|10|11|12)(?:[0-2][1-9]|10|20|30|31)\d{2}[\dX]$/i;
|
|
150
|
+
const cnIdCard18Re = /^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|1[0-2])(?:[0-2][1-9]|10|20|30|31)\d{3}[\dX]$/i;
|
|
151
|
+
const emailRE = /^[a-z\d](?:\w|-)+@[a-z\d]+\.[a-z]{2,5}$/i;
|
|
152
|
+
const isUSCI = (val) => {
|
|
153
|
+
if (!isDef(val))
|
|
154
|
+
return false;
|
|
155
|
+
return cnUSCIRE.test(val);
|
|
156
|
+
};
|
|
148
157
|
const isPhone = (val) => {
|
|
149
158
|
if (!isDef(val))
|
|
150
159
|
return false;
|
|
@@ -153,20 +162,18 @@ const isPhone = (val) => {
|
|
|
153
162
|
const isTel = (val) => {
|
|
154
163
|
if (!isDef(val))
|
|
155
164
|
return false;
|
|
156
|
-
|
|
165
|
+
const valStr = val.toString().replaceAll(" ", "");
|
|
166
|
+
return cnTelRE.test(valStr) || cnTelRE2.test(valStr);
|
|
157
167
|
};
|
|
158
168
|
const isEmail = (val) => {
|
|
159
169
|
if (!isDef(val))
|
|
160
170
|
return false;
|
|
161
|
-
|
|
162
|
-
return reg.test(val);
|
|
171
|
+
return emailRE.test(val);
|
|
163
172
|
};
|
|
164
173
|
const isIdCard = (val, compatible = true) => {
|
|
165
174
|
if (!isDef(val))
|
|
166
175
|
return false;
|
|
167
|
-
|
|
168
|
-
const card18 = /^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|1[0-2])(?:[0-2][1-9]|10|20|30|31)\d{3}[\dXx]$/;
|
|
169
|
-
return card18.test(val) || (compatible ? card15.test(val) : false);
|
|
176
|
+
return cnIdCard18Re.test(val) || (compatible ? cnIdCard15Re.test(val) : false);
|
|
170
177
|
};
|
|
171
178
|
const isNumeric = (val) => {
|
|
172
179
|
if (isDef(val))
|
|
@@ -201,10 +208,17 @@ const isValidFileType = (file, type) => {
|
|
|
201
208
|
return validate(type);
|
|
202
209
|
};
|
|
203
210
|
|
|
211
|
+
const isValidResCode = (res, code = 200) => res.code === code;
|
|
212
|
+
const isValidRes = (res, code) => isValidResCode(res, code) && isDef(res.data);
|
|
213
|
+
const isValidArrRes = (res, code) => isValidResCode(res, code) && Array.isArray(res.data);
|
|
214
|
+
const isValidPageRes = (res, code) => isValidResCode(res, code) && isDef(res.data) && Array.isArray(res.data.list || res.data.records);
|
|
215
|
+
|
|
204
216
|
const hideMiddle = (str) => {
|
|
205
217
|
if (!isDef(str))
|
|
206
218
|
return "";
|
|
207
219
|
const len = str.length;
|
|
220
|
+
if (len === 0)
|
|
221
|
+
return "";
|
|
208
222
|
if (len === 1)
|
|
209
223
|
return "*";
|
|
210
224
|
if (len === 2)
|
|
@@ -227,6 +241,8 @@ const hideHead = (str, count) => {
|
|
|
227
241
|
return "";
|
|
228
242
|
const len = str.length;
|
|
229
243
|
const cnt = count !== void 0 ? Math.max(Math.min(len, count), 0) : len - 1;
|
|
244
|
+
if (len === 0)
|
|
245
|
+
return "";
|
|
230
246
|
if (len === 1)
|
|
231
247
|
return "*";
|
|
232
248
|
return "*".repeat(cnt) + str.substring(cnt);
|
|
@@ -236,17 +252,23 @@ const hideTail = (str, count) => {
|
|
|
236
252
|
return "";
|
|
237
253
|
const len = str.length;
|
|
238
254
|
const cnt = count !== void 0 ? Math.max(Math.min(len, count), 0) : len - 1;
|
|
255
|
+
if (len === 0)
|
|
256
|
+
return "";
|
|
239
257
|
if (len === 1)
|
|
240
258
|
return "*";
|
|
241
259
|
return str.substring(0, len - cnt) + "*".repeat(cnt);
|
|
242
260
|
};
|
|
243
261
|
const hidePhone = (phone) => {
|
|
244
262
|
if (phone) {
|
|
245
|
-
|
|
246
|
-
|
|
263
|
+
const phoneStr = phone.replaceAll(" ", "");
|
|
264
|
+
if (isPhone(phoneStr)) {
|
|
265
|
+
return phoneStr.replace(/^(\d{3})\d{4}(\d{4})$/, "$1****$2");
|
|
247
266
|
}
|
|
248
|
-
if (
|
|
249
|
-
return
|
|
267
|
+
if (cnTelRE.test(phoneStr)) {
|
|
268
|
+
return phoneStr.replace(/^(0\d{2,3})-?\d{3,4}(\d{4})$/, "$1****$2");
|
|
269
|
+
}
|
|
270
|
+
if (cnTelRE2.test(phoneStr)) {
|
|
271
|
+
return phoneStr.replace(/^\((0\d{2,3})\)\d{3,4}(\d{4})$/, "$1****$2");
|
|
250
272
|
}
|
|
251
273
|
return phone;
|
|
252
274
|
}
|
|
@@ -311,14 +333,34 @@ const hideBankCard = (bankCard) => {
|
|
|
311
333
|
return "";
|
|
312
334
|
};
|
|
313
335
|
|
|
336
|
+
let __BASE_ATTACH_URL__;
|
|
337
|
+
function setBaseAttachUrl(baseUrl) {
|
|
338
|
+
__BASE_ATTACH_URL__ = baseUrl;
|
|
339
|
+
}
|
|
340
|
+
function getBaseAttachUrl() {
|
|
341
|
+
return __BASE_ATTACH_URL__;
|
|
342
|
+
}
|
|
343
|
+
|
|
314
344
|
const parseToJSON = (str, reviver2) => isJSONStr(str) ? JSON.parse(str, reviver2) : null;
|
|
315
345
|
const replacer = (_, value) => {
|
|
316
|
-
if (value
|
|
346
|
+
if (isMap(value)) {
|
|
317
347
|
return {
|
|
318
348
|
dataType: "Map",
|
|
319
349
|
value: [...value]
|
|
320
350
|
};
|
|
321
351
|
}
|
|
352
|
+
if (isSet(value)) {
|
|
353
|
+
return {
|
|
354
|
+
dataType: "Set",
|
|
355
|
+
value: [...value]
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
if (isDate(value)) {
|
|
359
|
+
return {
|
|
360
|
+
dataType: "Date",
|
|
361
|
+
value: value.toISOString()
|
|
362
|
+
};
|
|
363
|
+
}
|
|
322
364
|
return value;
|
|
323
365
|
};
|
|
324
366
|
const reviver = (_, value) => {
|
|
@@ -326,11 +368,33 @@ const reviver = (_, value) => {
|
|
|
326
368
|
if (value.dataType === "Map") {
|
|
327
369
|
return new Map(value.value);
|
|
328
370
|
}
|
|
371
|
+
if (value.dataType === "Set") {
|
|
372
|
+
return new Set(value.value);
|
|
373
|
+
}
|
|
374
|
+
if (value.dataType === "Date") {
|
|
375
|
+
return new Date(value.value);
|
|
376
|
+
}
|
|
329
377
|
}
|
|
330
378
|
return value;
|
|
331
379
|
};
|
|
332
380
|
const filterObj = (obj, keys, reverse = false) => Object.fromEntries(Object.entries(obj).filter(([key]) => reverse ? !keys.includes(key) : keys.includes(key)));
|
|
333
381
|
|
|
382
|
+
const toThousands = (num) => {
|
|
383
|
+
const arr = num.toString().split(".");
|
|
384
|
+
const integer = arr[0]?.replace(/(\d)(?=(?:\d{3})+$)/g, "$1,");
|
|
385
|
+
if (arr.length > 1)
|
|
386
|
+
return `${integer}.${arr[1]}`;
|
|
387
|
+
return integer;
|
|
388
|
+
};
|
|
389
|
+
const currencyFmt = (currency, maximumFractionDigits = 2) => {
|
|
390
|
+
if (!isDef(currency))
|
|
391
|
+
return "";
|
|
392
|
+
const num = Number(currency);
|
|
393
|
+
if (Number.isNaN(num))
|
|
394
|
+
return "";
|
|
395
|
+
return new Intl.NumberFormat("zh-CN", { style: "currency", currency: "CNY", maximumFractionDigits }).format(num);
|
|
396
|
+
};
|
|
397
|
+
|
|
334
398
|
dayjs__default.extend(localeData__default).locale("zh-cn", zhCN__default);
|
|
335
399
|
dayjs__default.extend(duration__default);
|
|
336
400
|
dayjs__default.extend(relativeTime__default);
|
|
@@ -364,34 +428,10 @@ const dateDuration = (start, end) => {
|
|
|
364
428
|
return dd.format(format);
|
|
365
429
|
};
|
|
366
430
|
|
|
367
|
-
let __BASE_ATTACH_URL__;
|
|
368
|
-
function setBaseAttachUrl(baseUrl) {
|
|
369
|
-
__BASE_ATTACH_URL__ = baseUrl;
|
|
370
|
-
}
|
|
371
|
-
function getBaseAttachUrl() {
|
|
372
|
-
return __BASE_ATTACH_URL__;
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
const toThousands = (num) => {
|
|
376
|
-
const arr = num.toString().split(".");
|
|
377
|
-
const integer = arr[0]?.replace(/(\d)(?=(?:\d{3})+$)/g, "$1,");
|
|
378
|
-
if (arr.length > 1)
|
|
379
|
-
return `${integer}.${arr[1]}`;
|
|
380
|
-
return integer;
|
|
381
|
-
};
|
|
382
|
-
const currencyFmt = (currency, maximumFractionDigits = 2) => {
|
|
383
|
-
if (!isDef(currency))
|
|
384
|
-
return "";
|
|
385
|
-
const num = Number(currency);
|
|
386
|
-
if (Number.isNaN(num))
|
|
387
|
-
return "";
|
|
388
|
-
return new Intl.NumberFormat("zh-CN", { style: "currency", currency: "CNY", maximumFractionDigits }).format(num);
|
|
389
|
-
};
|
|
390
|
-
|
|
391
431
|
const resUrl = (url) => {
|
|
392
432
|
if (!isDef(url))
|
|
393
433
|
return void 0;
|
|
394
|
-
if (/^https?:\/\//.test(url) ||
|
|
434
|
+
if (/^https?:\/\//.test(url) || url.startsWith("blob:") || url.startsWith("data:")) {
|
|
395
435
|
return url;
|
|
396
436
|
}
|
|
397
437
|
const baseUrl = getBaseAttachUrl();
|
|
@@ -415,11 +455,11 @@ const recoverFile = (resource) => {
|
|
|
415
455
|
const attachFmt = (data) => {
|
|
416
456
|
let attachList = [];
|
|
417
457
|
const prototype = Object.prototype.toString.call(data);
|
|
418
|
-
if (!data)
|
|
458
|
+
if (!data) {
|
|
419
459
|
attachList = [];
|
|
420
|
-
else if (Array.isArray(data))
|
|
460
|
+
} else if (Array.isArray(data)) {
|
|
421
461
|
attachList = data.filter(Boolean);
|
|
422
|
-
else if (typeof data === "string") {
|
|
462
|
+
} else if (typeof data === "string") {
|
|
423
463
|
if (isArrStr(data)) {
|
|
424
464
|
attachList = (parseToJSON(data) || []).filter(Boolean);
|
|
425
465
|
} else {
|
|
@@ -430,11 +470,29 @@ const attachFmt = (data) => {
|
|
|
430
470
|
else
|
|
431
471
|
attachList = [{ id: data, name: "name", uri: data, group: "default" }];
|
|
432
472
|
}
|
|
433
|
-
} else if (prototype === "[object Object]")
|
|
473
|
+
} else if (prototype === "[object Object]") {
|
|
434
474
|
attachList = [data];
|
|
475
|
+
}
|
|
435
476
|
return attachList.map(recoverFile);
|
|
436
477
|
};
|
|
437
478
|
|
|
479
|
+
const isNumberArray = (arr) => arr.every((item) => typeof item === "number");
|
|
480
|
+
const isStringArray = (arr) => arr.every((item) => typeof item === "string");
|
|
481
|
+
const naturalSort = (arr) => {
|
|
482
|
+
if (isNumberArray(arr))
|
|
483
|
+
return [...arr].sort((a, b) => a - b);
|
|
484
|
+
if (isStringArray(arr)) {
|
|
485
|
+
return [...arr].sort((a, b) => a.localeCompare(b, "zh-CN")).sort((a, b) => a.length - b.length);
|
|
486
|
+
}
|
|
487
|
+
const newArr = [...arr].sort();
|
|
488
|
+
const validArr = newArr.filter((a) => isDef(a));
|
|
489
|
+
const invalidArr = newArr.filter((a) => !isDef(a));
|
|
490
|
+
return naturalSort(validArr).concat(invalidArr);
|
|
491
|
+
};
|
|
492
|
+
const naturalSortBy = (arr, key) => {
|
|
493
|
+
return [...arr].sort((a, b) => naturalSort([a[key], b[key]])[0] === a[key] ? -1 : 1);
|
|
494
|
+
};
|
|
495
|
+
|
|
438
496
|
const inBrowser = typeof window !== "undefined";
|
|
439
497
|
const waitTime = (time = 100) => {
|
|
440
498
|
return new Promise((resolve) => {
|
|
@@ -456,7 +514,8 @@ const getHostEnv = () => {
|
|
|
456
514
|
const ua = userAgent.toLowerCase();
|
|
457
515
|
const zlb = ua.includes("@zlb");
|
|
458
516
|
const zyd = ua.includes("saas");
|
|
459
|
-
const zzd = ua.includes("zhejiang") || ua.includes("tauruszjd");
|
|
517
|
+
const zzd = ua.includes("zhejiang") || ua.includes("tauruszjd") || ua.includes("dingtalk-win");
|
|
518
|
+
const dd = ua.includes("taurusapp");
|
|
460
519
|
const wx = ua.includes("micromessenger");
|
|
461
520
|
const zfb = ua.includes("alipay");
|
|
462
521
|
const mini = ua.includes("miniprogram");
|
|
@@ -464,6 +523,7 @@ const getHostEnv = () => {
|
|
|
464
523
|
zlb,
|
|
465
524
|
zyd,
|
|
466
525
|
zzd,
|
|
526
|
+
dd,
|
|
467
527
|
wx,
|
|
468
528
|
zfb,
|
|
469
529
|
mini
|
|
@@ -484,9 +544,16 @@ exports.camel2Pascal = camel2Pascal;
|
|
|
484
544
|
exports.camel2Snake = camel2Snake;
|
|
485
545
|
exports.camel2Title = camel2Title;
|
|
486
546
|
exports.checkImg = checkImg;
|
|
547
|
+
exports.cnIdCard15Re = cnIdCard15Re;
|
|
548
|
+
exports.cnIdCard18Re = cnIdCard18Re;
|
|
549
|
+
exports.cnPhoneRE = cnPhoneRE;
|
|
550
|
+
exports.cnTelRE = cnTelRE;
|
|
551
|
+
exports.cnTelRE2 = cnTelRE2;
|
|
552
|
+
exports.cnUSCIRE = cnUSCIRE;
|
|
487
553
|
exports.currencyFmt = currencyFmt;
|
|
488
554
|
exports.dateDuration = dateDuration;
|
|
489
555
|
exports.dateFmt = dateFmt;
|
|
556
|
+
exports.emailRE = emailRE;
|
|
490
557
|
exports.filterObj = filterObj;
|
|
491
558
|
exports.fixiOSInputAutoZoomIn = fixiOSInputAutoZoomIn;
|
|
492
559
|
exports.getBaseAttachUrl = getBaseAttachUrl;
|
|
@@ -534,6 +601,7 @@ exports.isString = isString;
|
|
|
534
601
|
exports.isSymbol = isSymbol;
|
|
535
602
|
exports.isTel = isTel;
|
|
536
603
|
exports.isURL = isURL;
|
|
604
|
+
exports.isUSCI = isUSCI;
|
|
537
605
|
exports.isUndefined = isUndefined;
|
|
538
606
|
exports.isValidArrRes = isValidArrRes;
|
|
539
607
|
exports.isValidFileType = isValidFileType;
|
|
@@ -560,6 +628,8 @@ exports.kebab2Camel = kebab2Camel;
|
|
|
560
628
|
exports.kebab2Pascal = kebab2Pascal;
|
|
561
629
|
exports.kebab2Snake = kebab2Snake;
|
|
562
630
|
exports.kebab2Title = kebab2Title;
|
|
631
|
+
exports.naturalSort = naturalSort;
|
|
632
|
+
exports.naturalSortBy = naturalSortBy;
|
|
563
633
|
exports.nonASCII = nonASCII;
|
|
564
634
|
exports.parseToJSON = parseToJSON;
|
|
565
635
|
exports.pascal2Camel = pascal2Camel;
|