@jhqn/utils-core 0.0.1-beta.23 → 0.0.1-beta.25

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 CHANGED
@@ -1,13 +1,13 @@
1
1
  'use strict';
2
2
 
3
3
  const detectBrowser = require('detect-browser');
4
- const dayjs = require('dayjs/esm');
5
- const localeData = require('dayjs/esm/plugin/localeData');
6
- const duration = require('dayjs/esm/plugin/duration');
7
- const relativeTime = require('dayjs/esm/plugin/relativeTime');
8
- const customParseFormat = require('dayjs/esm/plugin/customParseFormat');
9
- const objectSupport = require('dayjs/esm/plugin/objectSupport');
10
- const zhCN = require('dayjs/esm/locale/zh-cn');
4
+ const dayjs = require('dayjs');
5
+ const localeData = require('dayjs/plugin/localeData');
6
+ const duration = require('dayjs/plugin/duration');
7
+ const relativeTime = require('dayjs/plugin/relativeTime');
8
+ const customParseFormat = require('dayjs/plugin/customParseFormat');
9
+ const objectSupport = require('dayjs/plugin/objectSupport');
10
+ const zhCN = require('dayjs/locale/zh-cn');
11
11
 
12
12
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
13
13
 
@@ -76,6 +76,9 @@ const isNumeric = (val) => typeof val === "number" || /^\d+(?:\.\d+)?$/.test(val
76
76
  const checkImg = (ext) => {
77
77
  return /\.(?:jpg|jpeg|png|gif)$/.test((ext || "").toLowerCase());
78
78
  };
79
+ const isEmpty = (val) => {
80
+ return !isDef(val) || val === "" || Array.isArray(val) && val.length === 0 || Object.prototype.toString.call(val) === "[object Object]" && Object.keys(val).length === 0 || val instanceof Map && val.size === 0 || val instanceof Set && val.size === 0;
81
+ };
79
82
  const isJSONStr = (str) => {
80
83
  try {
81
84
  JSON.parse(str);
@@ -108,17 +111,18 @@ const isEmail = (val) => {
108
111
  const reg = /^[a-zA-Z\d](?:\w|-)+@[a-zA-Z\d]+\.[a-zA-Z]{2,5}$/;
109
112
  return reg.test(val);
110
113
  };
111
- const isIdCard = (val) => {
114
+ const isIdCard = (val, compatible = true) => {
112
115
  if (!isDef(val))
113
116
  return false;
114
117
  const card15 = /^[1-9]\d{7}(?:0[1-9]|10|11|12)(?:[0-2][1-9]|10|20|30|31)\d{2}[\dXx]$/;
115
118
  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]$/;
116
- return card15.test(val) || card18.test(val);
119
+ return card18.test(val) || (compatible ? card15.test(val) : false);
117
120
  };
118
121
  const nonASCII = (text) => /[^\x20-\x7E]/.test(text);
119
- const isValidRes = (res) => res.code === 200 && isDef(res.data);
120
- const isValidArrRes = (res) => res.code === 200 && Array.isArray(res.data);
121
- const isValidPageRes = (res) => res.code === 200 && isDef(res.data) && Array.isArray(res.data.list || res.data.records);
122
+ const isValidResCode = (res, code = 200) => res.code === code;
123
+ const isValidRes = (res, code) => isValidResCode(res, code) && isDef(res.data);
124
+ const isValidArrRes = (res, code) => isValidResCode(res, code) && Array.isArray(res.data);
125
+ const isValidPageRes = (res, code) => isValidResCode(res, code) && isDef(res.data) && Array.isArray(res.data.list || res.data.records);
122
126
  const isValidFileType = (file, type) => {
123
127
  if (type === "*")
124
128
  return true;
@@ -164,21 +168,23 @@ const hideThird = (str) => {
164
168
  return "**";
165
169
  return str.slice(0, partLen) + "*".repeat(len - 2 * partLen) + str.slice(len - partLen);
166
170
  };
167
- const hideHead = (str) => {
171
+ const hideHead = (str, count) => {
168
172
  if (!isDef(str))
169
173
  return "";
170
174
  const len = str.length;
175
+ const cnt = count !== void 0 ? Math.max(Math.min(len, count), 0) : len - 1;
171
176
  if (len === 1)
172
177
  return "*";
173
- return "*".repeat(len - 1) + str[len - 1];
178
+ return "*".repeat(cnt) + str.substring(cnt);
174
179
  };
175
- const hideTail = (str) => {
180
+ const hideTail = (str, count) => {
176
181
  if (!isDef(str))
177
182
  return "";
178
183
  const len = str.length;
184
+ const cnt = count !== void 0 ? Math.max(Math.min(len, count), 0) : len - 1;
179
185
  if (len === 1)
180
186
  return "*";
181
- return str[0] + "*".repeat(len - 1);
187
+ return str.substring(0, len - cnt) + "*".repeat(cnt);
182
188
  };
183
189
  const hidePhone = (phone) => {
184
190
  if (phone) {
@@ -192,9 +198,12 @@ const hidePhone = (phone) => {
192
198
  }
193
199
  return "";
194
200
  };
195
- const hideCardNo = (cardNo) => {
201
+ const hideCardNo = (cardNo, strong = true) => {
196
202
  if (cardNo) {
197
- return cardNo.replace(/^(.{4})\d+(.{2})$/, "$1*******$2");
203
+ if (isIdCard(cardNo, false)) {
204
+ return strong ? hideMiddle(cardNo) : cardNo.replace(/^(.)\d{3}(.{8})\d{5}(.)$/, "$1***$2*****$3");
205
+ }
206
+ return cardNo;
198
207
  }
199
208
  return "";
200
209
  };
@@ -213,7 +222,7 @@ const hideName = (name) => {
213
222
  return "";
214
223
  };
215
224
  const hideSurname = (name) => {
216
- return hideHead(name);
225
+ return hideHead(name, 1);
217
226
  };
218
227
  const hideFirstName = (name) => {
219
228
  return hideTail(name);
@@ -221,14 +230,29 @@ const hideFirstName = (name) => {
221
230
  const hideEmail = (email) => {
222
231
  if (email) {
223
232
  if (!email.includes("@"))
224
- return "";
233
+ return email;
225
234
  const arr = email.split("@");
226
235
  const name = arr[0];
227
236
  const domain = arr[1];
228
- const len = name?.length;
229
- if (!len)
237
+ if (!name || !domain)
230
238
  return email;
231
- return `${hideTail(name)}@${domain}`;
239
+ const len = name.length;
240
+ let newName = "";
241
+ if (len <= 3) {
242
+ newName = `${name}***`;
243
+ } else {
244
+ newName = `${name.substring(0, len - 3)}***`;
245
+ }
246
+ return `${newName}@${domain}`;
247
+ }
248
+ return "";
249
+ };
250
+ const hideBankCard = (bankCard) => {
251
+ if (bankCard) {
252
+ const len = bankCard.length;
253
+ if (len <= 10)
254
+ return bankCard;
255
+ return bankCard.substring(0, 6) + "*".repeat(len - 10) + bankCard.substring(len - 4, len);
232
256
  }
233
257
  return "";
234
258
  };
@@ -341,17 +365,94 @@ const dateDuration = (start, end) => {
341
365
  return dd.format(format);
342
366
  };
343
367
 
368
+ let BASE_ATTACH_URL;
369
+ function setBaseAttachUrl(baseUrl) {
370
+ BASE_ATTACH_URL = baseUrl;
371
+ }
372
+ function getBaseAttachUrl() {
373
+ return BASE_ATTACH_URL;
374
+ }
375
+ if (undefined?.VITE_BASE_ATTACH_URL) {
376
+ setBaseAttachUrl(undefined.VITE_BASE_ATTACH_URL);
377
+ }
378
+
379
+ const toThousands = (num) => {
380
+ const arr = num.toString().split(".");
381
+ const integer = arr[0]?.replace(/(\d)(?=(?:\d{3})+$)/g, "$1,");
382
+ if (arr.length > 1)
383
+ return `${integer}.${arr[1]}`;
384
+ return integer;
385
+ };
386
+ const currencyFmt = (currency, fraction = 0) => {
387
+ if (!isDef(currency))
388
+ return "";
389
+ return new Intl.NumberFormat("zh-CN", { style: "currency", currency: "CNY", maximumFractionDigits: fraction }).format(
390
+ currency
391
+ );
392
+ };
393
+
394
+ const resUrl = (url) => {
395
+ if (!isDef(url))
396
+ return void 0;
397
+ if (/^https?:\/\//.test(url) || /^blob:/.test(url) || /^data:/.test(url)) {
398
+ return url;
399
+ }
400
+ const baseUrl = getBaseAttachUrl();
401
+ if (!isDef(baseUrl)) {
402
+ throw new Error("\u8BF7\u5148\u8BBE\u7F6E import.meta\u200B.env.VITE_BASE_ATTACH_URL");
403
+ }
404
+ return `${baseUrl}/${url}`.replace(/([^:]\/)\/+/g, "$1");
405
+ };
406
+ const recoverFile = (resource) => {
407
+ const { uri, url, ...item } = resource;
408
+ const validUrl = uri || url;
409
+ return {
410
+ ...item,
411
+ url: resUrl(validUrl),
412
+ content: resUrl(validUrl),
413
+ uri: validUrl || "",
414
+ status: "done",
415
+ percent: 100
416
+ };
417
+ };
418
+ const attachFmt = (data) => {
419
+ let attachList = [];
420
+ const prototype = Object.prototype.toString.call(data);
421
+ if (!data)
422
+ attachList = [];
423
+ else if (Array.isArray(data))
424
+ attachList = data.filter(Boolean);
425
+ else if (typeof data === "string") {
426
+ if (isArrStr(data)) {
427
+ attachList = (parseToJSON(data) || []).filter(Boolean);
428
+ } else {
429
+ const jsonData = parseToJSON(data);
430
+ const prototypeJson = Object.prototype.toString.call(jsonData);
431
+ if (prototypeJson === "[object Object]")
432
+ attachList = [jsonData].filter(Boolean);
433
+ else
434
+ attachList = [{ id: data, name: "name", uri: data, group: "default" }];
435
+ }
436
+ } else if (prototype === "[object Object]")
437
+ attachList = [data];
438
+ return attachList.map(recoverFile);
439
+ };
440
+
344
441
  exports.dayjs = dayjs__default;
442
+ exports.attachFmt = attachFmt;
345
443
  exports.camel2Kebab = camel2Kebab;
346
444
  exports.camel2Pascal = camel2Pascal;
347
445
  exports.camel2Snake = camel2Snake;
348
446
  exports.camel2Title = camel2Title;
349
447
  exports.checkImg = checkImg;
448
+ exports.currencyFmt = currencyFmt;
350
449
  exports.dateDuration = dateDuration;
351
450
  exports.dateFmt = dateFmt;
352
451
  exports.filterObj = filterObj;
353
452
  exports.fixiOSInputAutoZoomIn = fixiOSInputAutoZoomIn;
453
+ exports.getBaseAttachUrl = getBaseAttachUrl;
354
454
  exports.getHostEnv = getHostEnv;
455
+ exports.hideBankCard = hideBankCard;
355
456
  exports.hideCardNo = hideCardNo;
356
457
  exports.hideEmail = hideEmail;
357
458
  exports.hideFirstName = hideFirstName;
@@ -366,6 +467,7 @@ exports.inBrowser = inBrowser;
366
467
  exports.isArrStr = isArrStr;
367
468
  exports.isDef = isDef;
368
469
  exports.isEmail = isEmail;
470
+ exports.isEmpty = isEmpty;
369
471
  exports.isIdCard = isIdCard;
370
472
  exports.isJSONStr = isJSONStr;
371
473
  exports.isNumeric = isNumeric;
@@ -376,6 +478,7 @@ exports.isValidFileType = isValidFileType;
376
478
  exports.isValidKey = isValidKey;
377
479
  exports.isValidPageRes = isValidPageRes;
378
480
  exports.isValidRes = isValidRes;
481
+ exports.isValidResCode = isValidResCode;
379
482
  exports.jClear = jClear;
380
483
  exports.jDebug = jDebug;
381
484
  exports.jError = jError;
@@ -397,8 +500,11 @@ exports.pascal2Camel = pascal2Camel;
397
500
  exports.pascal2Kebab = pascal2Kebab;
398
501
  exports.pascal2Snake = pascal2Snake;
399
502
  exports.pascal2Title = pascal2Title;
503
+ exports.recoverFile = recoverFile;
400
504
  exports.replacer = replacer;
505
+ exports.resUrl = resUrl;
401
506
  exports.reviver = reviver;
507
+ exports.setBaseAttachUrl = setBaseAttachUrl;
402
508
  exports.snake2Camel = snake2Camel;
403
509
  exports.snake2Kebab = snake2Kebab;
404
510
  exports.snake2Pascal = snake2Pascal;
@@ -407,5 +513,6 @@ exports.title2Camel = title2Camel;
407
513
  exports.title2Kebab = title2Kebab;
408
514
  exports.title2Pascal = title2Pascal;
409
515
  exports.title2Snake = title2Snake;
516
+ exports.toThousands = toThousands;
410
517
  exports.userAgent = userAgent;
411
518
  exports.waitTime = waitTime;