@jhqn/utils-core 0.0.3-beta.8 → 0.1.0
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 +82 -106
- package/dist/index.d.cts +138 -48
- package/dist/index.d.mts +138 -48
- package/dist/index.d.ts +138 -48
- package/dist/index.mjs +74 -106
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -44,8 +44,12 @@ const jDebug = (...args) => console.log.bind(
|
|
|
44
44
|
`%c[${args.length === 1 ? "DEBUG" : "%s"}]`,
|
|
45
45
|
`${sharedStyle} background: linear-gradient(LightSalmon, Tomato); border-radius: 2px; font-weight: bold; border-top: 2px solid Crimson; border-bottom: 2px solid Crimson;`
|
|
46
46
|
)(...args);
|
|
47
|
-
const jWarn = console.
|
|
48
|
-
const jError = console.error.bind(
|
|
47
|
+
const jWarn = (...args) => console.warn.bind(console, `%c[${args.length === 1 ? "WARN" : "%s"}]`, `${sharedStyle} background: tomato;`)(...args);
|
|
48
|
+
const jError = (...args) => console.error.bind(
|
|
49
|
+
console,
|
|
50
|
+
`%c[${args.length === 1 ? "ERROR" : "%s"}]`,
|
|
51
|
+
`${sharedStyle} background: crimson;`
|
|
52
|
+
)(...args);
|
|
49
53
|
const jInfo = (...args) => console.info.bind(
|
|
50
54
|
console,
|
|
51
55
|
`%c[${args.length === 1 ? "INFO" : "%s"}]`,
|
|
@@ -60,18 +64,15 @@ const jClear = console.clear.bind(console);
|
|
|
60
64
|
|
|
61
65
|
let prevValue;
|
|
62
66
|
const jLogDedupe = (...args) => {
|
|
63
|
-
if (args.length === 0)
|
|
64
|
-
|
|
65
|
-
if (args.length > 0 && prevValue && JSON.stringify(args) === JSON.stringify(prevValue))
|
|
66
|
-
return;
|
|
67
|
+
if (args.length === 0) return;
|
|
68
|
+
if (args.length > 0 && prevValue && JSON.stringify(args) === JSON.stringify(prevValue)) return;
|
|
67
69
|
jLog(...args);
|
|
68
70
|
prevValue = args;
|
|
69
71
|
};
|
|
70
72
|
|
|
71
73
|
let logged = false;
|
|
72
74
|
const jLogOnce = (...args) => {
|
|
73
|
-
if (logged)
|
|
74
|
-
return;
|
|
75
|
+
if (logged) return;
|
|
75
76
|
jLog(...args);
|
|
76
77
|
logged = true;
|
|
77
78
|
};
|
|
@@ -109,7 +110,7 @@ const isJSONStr = (str) => {
|
|
|
109
110
|
JSON.parse(str);
|
|
110
111
|
return true;
|
|
111
112
|
} catch (e) {
|
|
112
|
-
|
|
113
|
+
jWarn("Failed to parse invalid string", e);
|
|
113
114
|
return false;
|
|
114
115
|
}
|
|
115
116
|
}
|
|
@@ -120,7 +121,7 @@ const isArrStr = (str) => {
|
|
|
120
121
|
try {
|
|
121
122
|
return Array.isArray(JSON.parse(str));
|
|
122
123
|
} catch (e) {
|
|
123
|
-
|
|
124
|
+
jWarn("Failed to parse invalid string", e);
|
|
124
125
|
return false;
|
|
125
126
|
}
|
|
126
127
|
}
|
|
@@ -132,39 +133,35 @@ const isEmpty = (val) => {
|
|
|
132
133
|
};
|
|
133
134
|
|
|
134
135
|
const cnPhoneRE = /^1[3-9]\d{9}$/;
|
|
135
|
-
const cnTelRE = /^0\d{
|
|
136
|
+
const cnTelRE = /^0\d{2,3}-?\d{7,8}$/;
|
|
137
|
+
const cnTelRE2 = /^\(0\d{2,3}\)\d{7,8}$/;
|
|
136
138
|
const cnUSCIRE = /^[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}$/;
|
|
139
|
+
const cnIdCard15Re = /^[1-9]\d{7}(?:0[1-9]|10|11|12)(?:[0-2][1-9]|10|20|30|31)\d{2}[\dX]$/i;
|
|
140
|
+
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;
|
|
141
|
+
const emailRE = /^[a-z\d](?:\w|-)+@[a-z\d]+\.[a-z]{2,5}$/i;
|
|
137
142
|
const isUSCI = (val) => {
|
|
138
|
-
if (!isDef(val))
|
|
139
|
-
return false;
|
|
143
|
+
if (!isDef(val)) return false;
|
|
140
144
|
return cnUSCIRE.test(val);
|
|
141
145
|
};
|
|
142
146
|
const isPhone = (val) => {
|
|
143
|
-
if (!isDef(val))
|
|
144
|
-
return false;
|
|
147
|
+
if (!isDef(val)) return false;
|
|
145
148
|
return cnPhoneRE.test(val.toString());
|
|
146
149
|
};
|
|
147
150
|
const isTel = (val) => {
|
|
148
|
-
if (!isDef(val))
|
|
149
|
-
|
|
150
|
-
return cnTelRE.test(
|
|
151
|
+
if (!isDef(val)) return false;
|
|
152
|
+
const valStr = val.toString().replaceAll(" ", "");
|
|
153
|
+
return cnTelRE.test(valStr) || cnTelRE2.test(valStr);
|
|
151
154
|
};
|
|
152
155
|
const isEmail = (val) => {
|
|
153
|
-
if (!isDef(val))
|
|
154
|
-
|
|
155
|
-
const reg = /^[a-z\d](?:\w|-)+@[a-z\d]+\.[a-z]{2,5}$/i;
|
|
156
|
-
return reg.test(val);
|
|
156
|
+
if (!isDef(val)) return false;
|
|
157
|
+
return emailRE.test(val);
|
|
157
158
|
};
|
|
158
159
|
const isIdCard = (val, compatible = true) => {
|
|
159
|
-
if (!isDef(val))
|
|
160
|
-
|
|
161
|
-
const card15 = /^[1-9]\d{7}(?:0[1-9]|10|11|12)(?:[0-2][1-9]|10|20|30|31)\d{2}[\dX]$/i;
|
|
162
|
-
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}[\dX]$/i;
|
|
163
|
-
return card18.test(val) || (compatible ? card15.test(val) : false);
|
|
160
|
+
if (!isDef(val)) return false;
|
|
161
|
+
return cnIdCard18Re.test(val) || (compatible ? cnIdCard15Re.test(val) : false);
|
|
164
162
|
};
|
|
165
163
|
const isNumeric = (val) => {
|
|
166
|
-
if (isDef(val))
|
|
167
|
-
return typeof val === "number" || /^\d+(?:\.\d+)?$/.test(val);
|
|
164
|
+
if (isDef(val)) return typeof val === "number" || /^\d+(?:\.\d+)?$/.test(val);
|
|
168
165
|
return false;
|
|
169
166
|
};
|
|
170
167
|
const checkImg = (ext) => {
|
|
@@ -172,8 +169,7 @@ const checkImg = (ext) => {
|
|
|
172
169
|
};
|
|
173
170
|
const nonASCII = (text) => /[^\x20-\x7E]/.test(text);
|
|
174
171
|
const isValidFileType = (file, type) => {
|
|
175
|
-
if (type === "*")
|
|
176
|
-
return true;
|
|
172
|
+
if (type === "*") return true;
|
|
177
173
|
const fileType = file.type;
|
|
178
174
|
const fileName = file.name;
|
|
179
175
|
function validate(item) {
|
|
@@ -201,51 +197,43 @@ const isValidArrRes = (res, code) => isValidResCode(res, code) && Array.isArray(
|
|
|
201
197
|
const isValidPageRes = (res, code) => isValidResCode(res, code) && isDef(res.data) && Array.isArray(res.data.list || res.data.records);
|
|
202
198
|
|
|
203
199
|
const hideMiddle = (str) => {
|
|
204
|
-
if (!isDef(str))
|
|
205
|
-
return "";
|
|
200
|
+
if (!isDef(str)) return "";
|
|
206
201
|
const len = str.length;
|
|
207
|
-
if (len
|
|
208
|
-
return "*";
|
|
209
|
-
if (len === 2)
|
|
210
|
-
return "**";
|
|
202
|
+
if (len <= 2) return "*".repeat(len);
|
|
211
203
|
return str[0] + "*".repeat(len - 2) + str[len - 1];
|
|
212
204
|
};
|
|
213
205
|
const hideThird = (str) => {
|
|
214
|
-
if (!isDef(str))
|
|
215
|
-
return "";
|
|
206
|
+
if (!isDef(str)) return "";
|
|
216
207
|
const len = str.length;
|
|
208
|
+
if (len <= 2) return "*".repeat(len);
|
|
217
209
|
const partLen = Math.round(len / 3);
|
|
218
|
-
if (len === 1)
|
|
219
|
-
return "*";
|
|
220
|
-
if (len === 2)
|
|
221
|
-
return "**";
|
|
222
210
|
return str.slice(0, partLen) + "*".repeat(len - 2 * partLen) + str.slice(len - partLen);
|
|
223
211
|
};
|
|
224
212
|
const hideHead = (str, count) => {
|
|
225
|
-
if (!isDef(str))
|
|
226
|
-
return "";
|
|
213
|
+
if (!isDef(str)) return "";
|
|
227
214
|
const len = str.length;
|
|
215
|
+
if (len <= 1) return "*".repeat(len);
|
|
228
216
|
const cnt = count !== void 0 ? Math.max(Math.min(len, count), 0) : len - 1;
|
|
229
|
-
if (len === 1)
|
|
230
|
-
return "*";
|
|
231
217
|
return "*".repeat(cnt) + str.substring(cnt);
|
|
232
218
|
};
|
|
233
219
|
const hideTail = (str, count) => {
|
|
234
|
-
if (!isDef(str))
|
|
235
|
-
return "";
|
|
220
|
+
if (!isDef(str)) return "";
|
|
236
221
|
const len = str.length;
|
|
222
|
+
if (len <= 1) return "*".repeat(len);
|
|
237
223
|
const cnt = count !== void 0 ? Math.max(Math.min(len, count), 0) : len - 1;
|
|
238
|
-
if (len === 1)
|
|
239
|
-
return "*";
|
|
240
224
|
return str.substring(0, len - cnt) + "*".repeat(cnt);
|
|
241
225
|
};
|
|
242
226
|
const hidePhone = (phone) => {
|
|
243
227
|
if (phone) {
|
|
244
|
-
|
|
245
|
-
|
|
228
|
+
const phoneStr = phone.replaceAll(" ", "");
|
|
229
|
+
if (isPhone(phoneStr)) {
|
|
230
|
+
return phoneStr.replace(/^(\d{3})\d{4}(\d{4})$/, "$1****$2");
|
|
231
|
+
}
|
|
232
|
+
if (cnTelRE.test(phoneStr)) {
|
|
233
|
+
return phoneStr.replace(/^(0\d{2,3})-?\d{3,4}(\d{4})$/, "$1****$2");
|
|
246
234
|
}
|
|
247
|
-
if (
|
|
248
|
-
return
|
|
235
|
+
if (cnTelRE2.test(phoneStr)) {
|
|
236
|
+
return phoneStr.replace(/^\((0\d{2,3})\)\d{3,4}(\d{4})$/, "$1****$2");
|
|
249
237
|
}
|
|
250
238
|
return phone;
|
|
251
239
|
}
|
|
@@ -260,20 +248,6 @@ const hideCardNo = (cardNo, strong = true) => {
|
|
|
260
248
|
}
|
|
261
249
|
return "";
|
|
262
250
|
};
|
|
263
|
-
const hideName = (name) => {
|
|
264
|
-
if (name) {
|
|
265
|
-
let res = "";
|
|
266
|
-
for (let i = 0; i < name.length; i += 1) {
|
|
267
|
-
if (i === 0) {
|
|
268
|
-
res += "*";
|
|
269
|
-
} else {
|
|
270
|
-
res += name[i];
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
return res;
|
|
274
|
-
}
|
|
275
|
-
return "";
|
|
276
|
-
};
|
|
277
251
|
const hideSurname = (name) => {
|
|
278
252
|
return hideHead(name, 1);
|
|
279
253
|
};
|
|
@@ -282,13 +256,11 @@ const hideFirstName = (name) => {
|
|
|
282
256
|
};
|
|
283
257
|
const hideEmail = (email) => {
|
|
284
258
|
if (email) {
|
|
285
|
-
if (!email.includes("@"))
|
|
286
|
-
return email;
|
|
259
|
+
if (!email.includes("@")) return email;
|
|
287
260
|
const arr = email.split("@");
|
|
288
261
|
const name = arr[0];
|
|
289
262
|
const domain = arr[1];
|
|
290
|
-
if (!name || !domain)
|
|
291
|
-
return email;
|
|
263
|
+
if (!name || !domain) return email;
|
|
292
264
|
const len = name.length;
|
|
293
265
|
let newName;
|
|
294
266
|
if (len <= 3) {
|
|
@@ -303,8 +275,7 @@ const hideEmail = (email) => {
|
|
|
303
275
|
const hideBankCard = (bankCard) => {
|
|
304
276
|
if (bankCard) {
|
|
305
277
|
const len = bankCard.length;
|
|
306
|
-
if (len <= 10)
|
|
307
|
-
return bankCard;
|
|
278
|
+
if (len <= 10) return bankCard;
|
|
308
279
|
return bankCard.substring(0, 6) + "*".repeat(len - 10) + bankCard.substring(len - 4, len);
|
|
309
280
|
}
|
|
310
281
|
return "";
|
|
@@ -318,8 +289,16 @@ function getBaseAttachUrl() {
|
|
|
318
289
|
return __BASE_ATTACH_URL__;
|
|
319
290
|
}
|
|
320
291
|
|
|
321
|
-
|
|
322
|
-
|
|
292
|
+
function parseToJSON(str, reviverFn) {
|
|
293
|
+
return isJSONStr(str) ? JSON.parse(str, reviverFn) : null;
|
|
294
|
+
}
|
|
295
|
+
function parseJSON(str, reviverFn = reviver) {
|
|
296
|
+
return isJSONStr(str) ? JSON.parse(str, reviverFn) : null;
|
|
297
|
+
}
|
|
298
|
+
function stringifyJSON(value, replacerFn = replacer) {
|
|
299
|
+
return JSON.stringify(value, replacerFn);
|
|
300
|
+
}
|
|
301
|
+
function replacer(_key, value) {
|
|
323
302
|
if (isMap(value)) {
|
|
324
303
|
return {
|
|
325
304
|
dataType: "Map",
|
|
@@ -339,8 +318,8 @@ const replacer = (_, value) => {
|
|
|
339
318
|
};
|
|
340
319
|
}
|
|
341
320
|
return value;
|
|
342
|
-
}
|
|
343
|
-
|
|
321
|
+
}
|
|
322
|
+
function reviver(_key, value) {
|
|
344
323
|
if (typeof value === "object" && value !== null) {
|
|
345
324
|
if (value.dataType === "Map") {
|
|
346
325
|
return new Map(value.value);
|
|
@@ -353,22 +332,19 @@ const reviver = (_, value) => {
|
|
|
353
332
|
}
|
|
354
333
|
}
|
|
355
334
|
return value;
|
|
356
|
-
}
|
|
335
|
+
}
|
|
357
336
|
const filterObj = (obj, keys, reverse = false) => Object.fromEntries(Object.entries(obj).filter(([key]) => reverse ? !keys.includes(key) : keys.includes(key)));
|
|
358
337
|
|
|
359
338
|
const toThousands = (num) => {
|
|
360
339
|
const arr = num.toString().split(".");
|
|
361
340
|
const integer = arr[0]?.replace(/(\d)(?=(?:\d{3})+$)/g, "$1,");
|
|
362
|
-
if (arr.length > 1)
|
|
363
|
-
return `${integer}.${arr[1]}`;
|
|
341
|
+
if (arr.length > 1) return `${integer}.${arr[1]}`;
|
|
364
342
|
return integer;
|
|
365
343
|
};
|
|
366
344
|
const currencyFmt = (currency, maximumFractionDigits = 2) => {
|
|
367
|
-
if (!isDef(currency))
|
|
368
|
-
return "";
|
|
345
|
+
if (!isDef(currency)) return "";
|
|
369
346
|
const num = Number(currency);
|
|
370
|
-
if (Number.isNaN(num))
|
|
371
|
-
return "";
|
|
347
|
+
if (Number.isNaN(num)) return "";
|
|
372
348
|
return new Intl.NumberFormat("zh-CN", { style: "currency", currency: "CNY", maximumFractionDigits }).format(num);
|
|
373
349
|
};
|
|
374
350
|
|
|
@@ -380,19 +356,15 @@ dayjs.extend(objectSupport);
|
|
|
380
356
|
const dateFmt = (date, format = "YYYY-MM-DD HH:mm:ss") => {
|
|
381
357
|
const dd = dayjs(date);
|
|
382
358
|
let fmt = format;
|
|
383
|
-
if (!isDef(date) || !dd.isValid())
|
|
384
|
-
|
|
385
|
-
if (format === "
|
|
386
|
-
fmt = "YYYY-MM-DD";
|
|
387
|
-
if (format === "datetime")
|
|
388
|
-
fmt = "YYYY-MM-DD HH:mm:ss";
|
|
359
|
+
if (!isDef(date) || !dd.isValid()) return "";
|
|
360
|
+
if (format === "date") fmt = "YYYY-MM-DD";
|
|
361
|
+
if (format === "datetime") fmt = "YYYY-MM-DD HH:mm:ss";
|
|
389
362
|
return dd.format(fmt);
|
|
390
363
|
};
|
|
391
364
|
const dateDuration = (start, end) => {
|
|
392
365
|
const startDay = dayjs(start);
|
|
393
366
|
const endDay = dayjs(end);
|
|
394
|
-
if (!isDef(start) || !startDay.isValid() || !isDef(end) || !endDay.isValid())
|
|
395
|
-
return "";
|
|
367
|
+
if (!isDef(start) || !startDay.isValid() || !isDef(end) || !endDay.isValid()) return "";
|
|
396
368
|
const dd = dayjs.duration(endDay.diff(startDay));
|
|
397
369
|
const year = dd.years();
|
|
398
370
|
const month = dd.months();
|
|
@@ -406,8 +378,7 @@ const dateDuration = (start, end) => {
|
|
|
406
378
|
};
|
|
407
379
|
|
|
408
380
|
const resUrl = (url) => {
|
|
409
|
-
if (!isDef(url))
|
|
410
|
-
return void 0;
|
|
381
|
+
if (!isDef(url)) return void 0;
|
|
411
382
|
if (/^https?:\/\//.test(url) || url.startsWith("blob:") || url.startsWith("data:")) {
|
|
412
383
|
return url;
|
|
413
384
|
}
|
|
@@ -438,14 +409,12 @@ const attachFmt = (data) => {
|
|
|
438
409
|
attachList = data.filter(Boolean);
|
|
439
410
|
} else if (typeof data === "string") {
|
|
440
411
|
if (isArrStr(data)) {
|
|
441
|
-
attachList = (
|
|
412
|
+
attachList = (parseJSON(data) || []).filter(Boolean);
|
|
442
413
|
} else {
|
|
443
|
-
const jsonData =
|
|
414
|
+
const jsonData = parseJSON(data);
|
|
444
415
|
const prototypeJson = Object.prototype.toString.call(jsonData);
|
|
445
|
-
if (prototypeJson === "[object Object]")
|
|
446
|
-
|
|
447
|
-
else
|
|
448
|
-
attachList = [{ id: data, name: "name", uri: data, group: "default" }];
|
|
416
|
+
if (prototypeJson === "[object Object]") attachList = [jsonData].filter(Boolean);
|
|
417
|
+
else attachList = [{ id: data, name: "name", uri: data, group: "default" }];
|
|
449
418
|
}
|
|
450
419
|
} else if (prototype === "[object Object]") {
|
|
451
420
|
attachList = [data];
|
|
@@ -456,8 +425,7 @@ const attachFmt = (data) => {
|
|
|
456
425
|
const isNumberArray = (arr) => arr.every((item) => typeof item === "number");
|
|
457
426
|
const isStringArray = (arr) => arr.every((item) => typeof item === "string");
|
|
458
427
|
const naturalSort = (arr) => {
|
|
459
|
-
if (isNumberArray(arr))
|
|
460
|
-
return [...arr].sort((a, b) => a - b);
|
|
428
|
+
if (isNumberArray(arr)) return [...arr].sort((a, b) => a - b);
|
|
461
429
|
if (isStringArray(arr)) {
|
|
462
430
|
return [...arr].sort((a, b) => a.localeCompare(b, "zh-CN")).sort((a, b) => a.length - b.length);
|
|
463
431
|
}
|
|
@@ -509,8 +477,8 @@ const getHostEnv = () => {
|
|
|
509
477
|
};
|
|
510
478
|
const XOR_VAL = 1111;
|
|
511
479
|
const getCodeOfTime = (time) => Math.abs(new Date(time).getTime() ^ XOR_VAL);
|
|
512
|
-
const getCodeOfHour = () => getCodeOfTime(`${dateFmt( new Date(), "YYYY-MM-DD HH")}:00:00`);
|
|
480
|
+
const getCodeOfHour = () => getCodeOfTime(`${dateFmt(/* @__PURE__ */ new Date(), "YYYY-MM-DD HH")}:00:00`);
|
|
513
481
|
const getCodeOfDate = () => getCodeOfTime(dateFmt(/* @__PURE__ */ new Date(), "date"));
|
|
514
482
|
const getCodeOfMonth = () => getCodeOfTime(dateFmt(dayjs().endOf("month"), "date"));
|
|
515
483
|
|
|
516
|
-
export { attachFmt, camel2Kebab, camel2Pascal, camel2Snake, camel2Title, checkImg, currencyFmt, dateDuration, dateFmt, filterObj, fixiOSInputAutoZoomIn, getBaseAttachUrl, getCodeOfDate, getCodeOfHour, getCodeOfMonth, getHostEnv, getVariableType, hideBankCard, hideCardNo, hideEmail, hideFirstName, hideHead, hideMiddle,
|
|
484
|
+
export { attachFmt, camel2Kebab, camel2Pascal, camel2Snake, camel2Title, checkImg, cnIdCard15Re, cnIdCard18Re, cnPhoneRE, cnTelRE, cnTelRE2, cnUSCIRE, currencyFmt, dateDuration, dateFmt, emailRE, filterObj, fixiOSInputAutoZoomIn, getBaseAttachUrl, getCodeOfDate, getCodeOfHour, getCodeOfMonth, getHostEnv, getVariableType, hideBankCard, hideCardNo, hideEmail, hideFirstName, hideHead, hideMiddle, hidePhone, hideSurname, hideTail, hideThird, inBrowser, isArrStr, isArray, isBigInt, isBlob, isBoolean, isDate, isDef, isEmail, isEmpty, isError, isFile, isFunction, isIdCard, isJSONStr, isMap, isNull, isNumber, isNumeric, isObject, isPhone, isPromise, isRegExp, isSet, isString, isSymbol, isTel, isURL, isUSCI, isUndefined, isValidArrRes, isValidFileType, isValidKey, isValidPageRes, isValidRes, isValidResCode, isWeakMap, isWeakSet, jClear, jDebug, jError, jGroup, jGroupC, jGroupE, jInfo, jLog, jLogDedupe, jLogOnce, jTime, jTimeEnd, jWarn, kebab2Camel, kebab2Pascal, kebab2Snake, kebab2Title, naturalSort, naturalSortBy, nonASCII, parseJSON, parseToJSON, pascal2Camel, pascal2Kebab, pascal2Snake, pascal2Title, recoverFile, replacer, resUrl, reviver, setBaseAttachUrl, snake2Camel, snake2Kebab, snake2Pascal, snake2Title, stringifyJSON, title2Camel, title2Kebab, title2Pascal, title2Snake, toThousands, userAgent, waitTime };
|
package/package.json
CHANGED