@jhqn/utils-core 0.0.3-beta.6 → 0.0.3-beta.8

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.mjs CHANGED
@@ -1,12 +1,12 @@
1
1
  export { detect } from 'detect-browser';
2
2
  import dayjs from 'dayjs';
3
3
  export { default as dayjs } from 'dayjs';
4
- import localeData from 'dayjs/plugin/localeData';
5
- import duration from 'dayjs/plugin/duration';
6
- import relativeTime from 'dayjs/plugin/relativeTime';
4
+ import zhCN from 'dayjs/locale/zh-cn';
7
5
  import customParseFormat from 'dayjs/plugin/customParseFormat';
6
+ import duration from 'dayjs/plugin/duration';
7
+ import localeData from 'dayjs/plugin/localeData';
8
8
  import objectSupport from 'dayjs/plugin/objectSupport';
9
- import zhCN from 'dayjs/locale/zh-cn';
9
+ import relativeTime from 'dayjs/plugin/relativeTime';
10
10
 
11
11
  const snake2Kebab = (str) => str.replace(/_([a-z])/g, (_, letter) => `-${letter.toLowerCase()}`);
12
12
  const snake2Pascal = (str) => str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase()).replace(/^[a-z]/, (letter) => letter.toUpperCase());
@@ -109,6 +109,7 @@ const isJSONStr = (str) => {
109
109
  JSON.parse(str);
110
110
  return true;
111
111
  } catch (e) {
112
+ jError("isJSONStr", e);
112
113
  return false;
113
114
  }
114
115
  }
@@ -119,6 +120,7 @@ const isArrStr = (str) => {
119
120
  try {
120
121
  return Array.isArray(JSON.parse(str));
121
122
  } catch (e) {
123
+ jError("isArrStr", e);
122
124
  return false;
123
125
  }
124
126
  }
@@ -129,13 +131,14 @@ const isEmpty = (val) => {
129
131
  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;
130
132
  };
131
133
 
132
- const isValidResCode = (res, code = 200) => res.code === code;
133
- const isValidRes = (res, code) => isValidResCode(res, code) && isDef(res.data);
134
- const isValidArrRes = (res, code) => isValidResCode(res, code) && Array.isArray(res.data);
135
- const isValidPageRes = (res, code) => isValidResCode(res, code) && isDef(res.data) && Array.isArray(res.data.list || res.data.records);
136
-
137
134
  const cnPhoneRE = /^1[3-9]\d{9}$/;
138
135
  const cnTelRE = /^0\d{9,11}$/;
136
+ const cnUSCIRE = /^[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}$/;
137
+ const isUSCI = (val) => {
138
+ if (!isDef(val))
139
+ return false;
140
+ return cnUSCIRE.test(val);
141
+ };
139
142
  const isPhone = (val) => {
140
143
  if (!isDef(val))
141
144
  return false;
@@ -192,6 +195,11 @@ const isValidFileType = (file, type) => {
192
195
  return validate(type);
193
196
  };
194
197
 
198
+ const isValidResCode = (res, code = 200) => res.code === code;
199
+ const isValidRes = (res, code) => isValidResCode(res, code) && isDef(res.data);
200
+ const isValidArrRes = (res, code) => isValidResCode(res, code) && Array.isArray(res.data);
201
+ const isValidPageRes = (res, code) => isValidResCode(res, code) && isDef(res.data) && Array.isArray(res.data.list || res.data.records);
202
+
195
203
  const hideMiddle = (str) => {
196
204
  if (!isDef(str))
197
205
  return "";
@@ -302,14 +310,34 @@ const hideBankCard = (bankCard) => {
302
310
  return "";
303
311
  };
304
312
 
313
+ let __BASE_ATTACH_URL__;
314
+ function setBaseAttachUrl(baseUrl) {
315
+ __BASE_ATTACH_URL__ = baseUrl;
316
+ }
317
+ function getBaseAttachUrl() {
318
+ return __BASE_ATTACH_URL__;
319
+ }
320
+
305
321
  const parseToJSON = (str, reviver2) => isJSONStr(str) ? JSON.parse(str, reviver2) : null;
306
322
  const replacer = (_, value) => {
307
- if (value instanceof Map) {
323
+ if (isMap(value)) {
308
324
  return {
309
325
  dataType: "Map",
310
326
  value: [...value]
311
327
  };
312
328
  }
329
+ if (isSet(value)) {
330
+ return {
331
+ dataType: "Set",
332
+ value: [...value]
333
+ };
334
+ }
335
+ if (isDate(value)) {
336
+ return {
337
+ dataType: "Date",
338
+ value: value.toISOString()
339
+ };
340
+ }
313
341
  return value;
314
342
  };
315
343
  const reviver = (_, value) => {
@@ -317,11 +345,33 @@ const reviver = (_, value) => {
317
345
  if (value.dataType === "Map") {
318
346
  return new Map(value.value);
319
347
  }
348
+ if (value.dataType === "Set") {
349
+ return new Set(value.value);
350
+ }
351
+ if (value.dataType === "Date") {
352
+ return new Date(value.value);
353
+ }
320
354
  }
321
355
  return value;
322
356
  };
323
357
  const filterObj = (obj, keys, reverse = false) => Object.fromEntries(Object.entries(obj).filter(([key]) => reverse ? !keys.includes(key) : keys.includes(key)));
324
358
 
359
+ const toThousands = (num) => {
360
+ const arr = num.toString().split(".");
361
+ const integer = arr[0]?.replace(/(\d)(?=(?:\d{3})+$)/g, "$1,");
362
+ if (arr.length > 1)
363
+ return `${integer}.${arr[1]}`;
364
+ return integer;
365
+ };
366
+ const currencyFmt = (currency, maximumFractionDigits = 2) => {
367
+ if (!isDef(currency))
368
+ return "";
369
+ const num = Number(currency);
370
+ if (Number.isNaN(num))
371
+ return "";
372
+ return new Intl.NumberFormat("zh-CN", { style: "currency", currency: "CNY", maximumFractionDigits }).format(num);
373
+ };
374
+
325
375
  dayjs.extend(localeData).locale("zh-cn", zhCN);
326
376
  dayjs.extend(duration);
327
377
  dayjs.extend(relativeTime);
@@ -355,34 +405,10 @@ const dateDuration = (start, end) => {
355
405
  return dd.format(format);
356
406
  };
357
407
 
358
- let __BASE_ATTACH_URL__;
359
- function setBaseAttachUrl(baseUrl) {
360
- __BASE_ATTACH_URL__ = baseUrl;
361
- }
362
- function getBaseAttachUrl() {
363
- return __BASE_ATTACH_URL__;
364
- }
365
-
366
- const toThousands = (num) => {
367
- const arr = num.toString().split(".");
368
- const integer = arr[0]?.replace(/(\d)(?=(?:\d{3})+$)/g, "$1,");
369
- if (arr.length > 1)
370
- return `${integer}.${arr[1]}`;
371
- return integer;
372
- };
373
- const currencyFmt = (currency, maximumFractionDigits = 2) => {
374
- if (!isDef(currency))
375
- return "";
376
- const num = Number(currency);
377
- if (Number.isNaN(num))
378
- return "";
379
- return new Intl.NumberFormat("zh-CN", { style: "currency", currency: "CNY", maximumFractionDigits }).format(num);
380
- };
381
-
382
408
  const resUrl = (url) => {
383
409
  if (!isDef(url))
384
410
  return void 0;
385
- if (/^https?:\/\//.test(url) || /^blob:/.test(url) || /^data:/.test(url)) {
411
+ if (/^https?:\/\//.test(url) || url.startsWith("blob:") || url.startsWith("data:")) {
386
412
  return url;
387
413
  }
388
414
  const baseUrl = getBaseAttachUrl();
@@ -427,6 +453,23 @@ const attachFmt = (data) => {
427
453
  return attachList.map(recoverFile);
428
454
  };
429
455
 
456
+ const isNumberArray = (arr) => arr.every((item) => typeof item === "number");
457
+ const isStringArray = (arr) => arr.every((item) => typeof item === "string");
458
+ const naturalSort = (arr) => {
459
+ if (isNumberArray(arr))
460
+ return [...arr].sort((a, b) => a - b);
461
+ if (isStringArray(arr)) {
462
+ return [...arr].sort((a, b) => a.localeCompare(b, "zh-CN")).sort((a, b) => a.length - b.length);
463
+ }
464
+ const newArr = [...arr].sort();
465
+ const validArr = newArr.filter((a) => isDef(a));
466
+ const invalidArr = newArr.filter((a) => !isDef(a));
467
+ return naturalSort(validArr).concat(invalidArr);
468
+ };
469
+ const naturalSortBy = (arr, key) => {
470
+ return [...arr].sort((a, b) => naturalSort([a[key], b[key]])[0] === a[key] ? -1 : 1);
471
+ };
472
+
430
473
  const inBrowser = typeof window !== "undefined";
431
474
  const waitTime = (time = 100) => {
432
475
  return new Promise((resolve) => {
@@ -470,4 +513,4 @@ const getCodeOfHour = () => getCodeOfTime(`${dateFmt( new Date(), "YYYY-MM-DD HH
470
513
  const getCodeOfDate = () => getCodeOfTime(dateFmt(/* @__PURE__ */ new Date(), "date"));
471
514
  const getCodeOfMonth = () => getCodeOfTime(dateFmt(dayjs().endOf("month"), "date"));
472
515
 
473
- export { attachFmt, camel2Kebab, camel2Pascal, camel2Snake, camel2Title, checkImg, currencyFmt, dateDuration, dateFmt, filterObj, fixiOSInputAutoZoomIn, getBaseAttachUrl, getCodeOfDate, getCodeOfHour, getCodeOfMonth, getHostEnv, getVariableType, hideBankCard, hideCardNo, hideEmail, hideFirstName, hideHead, hideMiddle, hideName, 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, 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, nonASCII, parseToJSON, pascal2Camel, pascal2Kebab, pascal2Snake, pascal2Title, recoverFile, replacer, resUrl, reviver, setBaseAttachUrl, snake2Camel, snake2Kebab, snake2Pascal, snake2Title, title2Camel, title2Kebab, title2Pascal, title2Snake, toThousands, userAgent, waitTime };
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, hideName, 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, parseToJSON, pascal2Camel, pascal2Kebab, pascal2Snake, pascal2Title, recoverFile, replacer, resUrl, reviver, setBaseAttachUrl, snake2Camel, snake2Kebab, snake2Pascal, snake2Title, title2Camel, title2Kebab, title2Pascal, title2Snake, toThousands, userAgent, waitTime };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jhqn/utils-core",
3
3
  "type": "module",
4
- "version": "0.0.3-beta.6",
4
+ "version": "0.0.3-beta.8",
5
5
  "author": "jade-gjz <jiangang.zhang@jaderd.com>",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/jaderd-jh/utils/tree/main/packages/core#readme",
@@ -37,7 +37,7 @@
37
37
  "dist"
38
38
  ],
39
39
  "dependencies": {
40
- "dayjs": "^1.11.11",
40
+ "dayjs": "^1.11.13",
41
41
  "detect-browser": "^5.3.0"
42
42
  },
43
43
  "scripts": {