@jhqn/utils-core 0.4.4 → 0.4.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/dist/index.d.ts CHANGED
@@ -3,9 +3,7 @@
3
3
  /// <reference types="dayjs/plugin/relativeTime" />
4
4
  /// <reference types="dayjs/plugin/customParseFormat" />
5
5
  /// <reference types="dayjs/plugin/objectSupport" />
6
-
7
6
  import dayjs, { ConfigType, Dayjs } from "dayjs";
8
-
9
7
  //#region types/index.d.ts
10
8
  /**
11
9
  * 数字或字符串
@@ -538,16 +536,25 @@ declare const filterObj: (obj: Record<string, any>, keys: string[], reverse?: bo
538
536
  declare const toThousands: (num: number) => string;
539
537
  /**
540
538
  * 货币格式化
539
+ * @example
540
+ * ```ts
541
+ * currencyFmt(123456.789) // "¥123,456.79"
542
+ * ```
541
543
  * @param {number | string | bigint} currency - 金额
542
- * @param {number} [maximumFractionDigits] - 小数位数
544
+ * @param {number} [maximumFractionDigits] - 最多小数位数
543
545
  */
544
546
  declare const currencyFmt: (currency: UnDef<Numeric | bigint>, maximumFractionDigits?: number) => string;
545
547
  //#endregion
546
548
  //#region src/format/dayjs.d.ts
547
549
  /**
548
550
  * 格式化时间
551
+ * @example
552
+ * ```ts
553
+ * dateFmt(new Date(), 'date') // 2024-06-01
554
+ * dateFmt(dayjs(), 'datetime') // 2024-06-01 12:34:56
555
+ * ```
549
556
  * @param {import('dayjs').ConfigType} date 时间
550
- * @param {string} format 格式
557
+ * @param {string} [format = "datetime"] 格式化pattern
551
558
  * @returns {string} 格式化后的时间
552
559
  */
553
560
  declare const dateFmt: (date: ConfigType, format?: string | "date" | "datetime") => string;
@@ -718,6 +725,11 @@ declare const isArray: (val: any) => val is Array<any>;
718
725
  * @param {any} val 检测的目标
719
726
  */
720
727
  declare const isFunction: (val: any) => val is Function;
728
+ /**
729
+ * 是否是纯函数(不包含 Class,含原生构造函数如 Date、Array 等)
730
+ * @param {any} val 检测的目标
731
+ */
732
+ declare const isPureFunction: (val: any) => val is Function;
721
733
  /**
722
734
  * 是否是日期
723
735
  * @param {any} val 检测的目标
@@ -895,4 +907,4 @@ declare const isValidArrRes: (res: Res<any[]>, code?: number) => boolean;
895
907
  */
896
908
  declare const isValidPageRes: (res: PageRes, code?: number) => boolean;
897
909
  //#endregion
898
- export { type Dayjs, type Dict, type HostEnv, type MaybeArray, type NullOrUndefinable, type Nullable, type Numeric, type PageReq, type PageRes, type PageResData, type PartialBy, type Res, type UnDef, type Undefinable, type VK, attachFmt, camel2Kebab, camel2Pascal, camel2Snake, camel2Title, checkImg, cnIdCard15Re, cnIdCard18Re, cnPhoneRE, cnTelRE, cnTelRE2, cnUSCIRE, currencyFmt, dateDuration, dateFmt, dayjs, emailRE, filterObj, fixiOSInputAutoZoomIn, getBaseAttachUrl, getCodeOfDate, getCodeOfHour, getCodeOfMonth, getHostEnv, getMockData, getMockDataList, 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 };
910
+ export { type Dayjs, type Dict, type HostEnv, type MaybeArray, type NullOrUndefinable, type Nullable, type Numeric, type PageReq, type PageRes, type PageResData, type PartialBy, type Res, type UnDef, type Undefinable, type VK, attachFmt, camel2Kebab, camel2Pascal, camel2Snake, camel2Title, checkImg, cnIdCard15Re, cnIdCard18Re, cnPhoneRE, cnTelRE, cnTelRE2, cnUSCIRE, currencyFmt, dateDuration, dateFmt, dayjs, emailRE, filterObj, fixiOSInputAutoZoomIn, getBaseAttachUrl, getCodeOfDate, getCodeOfHour, getCodeOfMonth, getHostEnv, getMockData, getMockDataList, 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, isPureFunction, 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/dist/index.js CHANGED
@@ -5,7 +5,6 @@ import duration from "dayjs/plugin/duration.js";
5
5
  import localeData from "dayjs/plugin/localeData.js";
6
6
  import objectSupport from "dayjs/plugin/objectSupport.js";
7
7
  import relativeTime from "dayjs/plugin/relativeTime.js";
8
-
9
8
  //#region src/case.ts
10
9
  /**
11
10
  * snake2Kebab
@@ -150,7 +149,6 @@ const title2Pascal = (str) => str.replace(/ /g, "");
150
149
  * @example title2Camel('Hello World') // Hello World => helloWorld
151
150
  */
152
151
  const title2Camel = (str) => str.replace(/ /g, "").replace(/^[A-Z]/, (match) => match.toLowerCase());
153
-
154
152
  //#endregion
155
153
  //#region src/console/log-common.ts
156
154
  const sharedStyle = "color: white; padding: 1px 2px;";
@@ -185,7 +183,7 @@ const jGroup = console.group.bind(console, "%c[%s]", `${sharedStyle} background:
185
183
  /**
186
184
  * jade console.groupCollapsed
187
185
  */
188
- const jGroupC = console.groupCollapsed.bind(console, "%c[%s]", `${sharedStyle} background: slateblue;`);
186
+ const jGroupC = console?.groupCollapsed?.bind(console, "%c[%s]", `${sharedStyle} background: slateblue;`);
189
187
  /**
190
188
  * jade console.groupEnd
191
189
  */
@@ -194,17 +192,16 @@ const jGroupE = console.groupEnd.bind(console);
194
192
  * jade console.time
195
193
  * @param str
196
194
  */
197
- const jTime = (str) => console.time(`[${str}]`);
195
+ const jTime = (str) => console?.time(`[${str}]`);
198
196
  /**
199
197
  * jade console.timeEnd
200
198
  * @param str
201
199
  */
202
- const jTimeEnd = (str) => console.timeEnd(`[${str}]`);
200
+ const jTimeEnd = (str) => console?.timeEnd(`[${str}]`);
203
201
  /**
204
202
  * jade console.clear
205
203
  */
206
- const jClear = console.clear.bind(console);
207
-
204
+ const jClear = console?.clear?.bind(console);
208
205
  //#endregion
209
206
  //#region src/console/log-dedupe.ts
210
207
  let prevValue;
@@ -218,7 +215,6 @@ const jLogDedupe = (...args) => {
218
215
  jLog(...args);
219
216
  prevValue = args;
220
217
  };
221
-
222
218
  //#endregion
223
219
  //#region src/console/log-once.ts
224
220
  let logged = false;
@@ -231,7 +227,6 @@ const jLogOnce = (...args) => {
231
227
  jLog(...args);
232
228
  logged = true;
233
229
  };
234
-
235
230
  //#endregion
236
231
  //#region src/validate/core.ts
237
232
  /**
@@ -288,7 +283,18 @@ const isArray = (val) => Array.isArray(val);
288
283
  * 是否是函数,但也有可能是Class
289
284
  * @param {any} val 检测的目标
290
285
  */
291
- const isFunction = (val) => getVariableType(val) === "function";
286
+ const isFunction = (val) => typeof val === "function";
287
+ /**
288
+ * 是否是纯函数(不包含 Class,含原生构造函数如 Date、Array 等)
289
+ * @param {any} val 检测的目标
290
+ */
291
+ const isPureFunction = (val) => {
292
+ if (!isFunction(val)) return false;
293
+ const source = Function.prototype.toString.call(val);
294
+ if (source.startsWith("class")) return false;
295
+ if (source.includes("[native code]") && val.prototype !== void 0) return false;
296
+ return true;
297
+ };
292
298
  /**
293
299
  * 是否是日期
294
300
  * @param {any} val 检测的目标
@@ -407,7 +413,6 @@ const isValidKey = (key, object) => key in object;
407
413
  const isEmpty = (val) => {
408
414
  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;
409
415
  };
410
-
411
416
  //#endregion
412
417
  //#region src/validate/form.ts
413
418
  const cnPhoneRE = /^1[3-9]\d{9}$/;
@@ -508,7 +513,6 @@ const isValidFileType = (file, type) => {
508
513
  if (typeArrStr.length > 1) return typeArrStr.some(validate);
509
514
  return validate(type);
510
515
  };
511
-
512
516
  //#endregion
513
517
  //#region src/validate/request.ts
514
518
  /**
@@ -535,7 +539,6 @@ const isValidArrRes = (res, code) => isValidResCode(res, code) && isArray(res.da
535
539
  * @param code
536
540
  */
537
541
  const isValidPageRes = (res, code) => isValidResCode(res, code) && isDef(res.data) && isArray(res.data.list || res.data.records);
538
-
539
542
  //#endregion
540
543
  //#region src/desensitize.ts
541
544
  /**
@@ -723,7 +726,6 @@ const hideBankCard = (bankCard) => {
723
726
  }
724
727
  return "";
725
728
  };
726
-
727
729
  //#endregion
728
730
  //#region src/format/baseAttachUrl.ts
729
731
  /**
@@ -743,7 +745,6 @@ function setBaseAttachUrl(baseUrl) {
743
745
  function getBaseAttachUrl() {
744
746
  return __BASE_ATTACH_URL__;
745
747
  }
746
-
747
748
  //#endregion
748
749
  //#region src/format/core.ts
749
750
  /**
@@ -817,7 +818,6 @@ function reviver(_key, value) {
817
818
  * @param {boolean} reverse 为true时过滤取反
818
819
  */
819
820
  const filterObj = (obj, keys, reverse = false) => Object.fromEntries(Object.entries(obj).filter(([key]) => reverse ? !keys.includes(key) : keys.includes(key)));
820
-
821
821
  //#endregion
822
822
  //#region src/format/currency.ts
823
823
  /**
@@ -832,8 +832,12 @@ const toThousands = (num) => {
832
832
  };
833
833
  /**
834
834
  * 货币格式化
835
+ * @example
836
+ * ```ts
837
+ * currencyFmt(123456.789) // "¥123,456.79"
838
+ * ```
835
839
  * @param {number | string | bigint} currency - 金额
836
- * @param {number} [maximumFractionDigits] - 小数位数
840
+ * @param {number} [maximumFractionDigits] - 最多小数位数
837
841
  */
838
842
  const currencyFmt = (currency, maximumFractionDigits = 2) => {
839
843
  if (!isDef(currency)) return "";
@@ -845,7 +849,6 @@ const currencyFmt = (currency, maximumFractionDigits = 2) => {
845
849
  maximumFractionDigits
846
850
  }).format(num);
847
851
  };
848
-
849
852
  //#endregion
850
853
  //#region src/format/dayjs.ts
851
854
  dayjs.extend(localeData).locale("zh-cn", zhCN);
@@ -855,11 +858,16 @@ dayjs.extend(customParseFormat);
855
858
  dayjs.extend(objectSupport);
856
859
  /**
857
860
  * 格式化时间
861
+ * @example
862
+ * ```ts
863
+ * dateFmt(new Date(), 'date') // 2024-06-01
864
+ * dateFmt(dayjs(), 'datetime') // 2024-06-01 12:34:56
865
+ * ```
858
866
  * @param {import('dayjs').ConfigType} date 时间
859
- * @param {string} format 格式
867
+ * @param {string} [format = "datetime"] 格式化pattern
860
868
  * @returns {string} 格式化后的时间
861
869
  */
862
- const dateFmt = (date, format = "YYYY-MM-DD HH:mm:ss") => {
870
+ const dateFmt = (date, format = "datetime") => {
863
871
  const dd = dayjs(date);
864
872
  let fmt = format;
865
873
  if (!isDef(date) || !dd.isValid()) return "";
@@ -887,7 +895,6 @@ const dateDuration = (start, end) => {
887
895
  ].join("");
888
896
  return dd.format(format);
889
897
  };
890
-
891
898
  //#endregion
892
899
  //#region src/format/resource.ts
893
900
  /**
@@ -940,7 +947,6 @@ const attachFmt = (data) => {
940
947
  else if (prototype === "[object Object]") attachList = [data];
941
948
  return attachList.map(recoverFile);
942
949
  };
943
-
944
950
  //#endregion
945
951
  //#region src/format/sort.ts
946
952
  /**
@@ -977,7 +983,6 @@ const naturalSort = (arr) => {
977
983
  const naturalSortBy = (arr, key) => {
978
984
  return [...arr].sort((a, b) => naturalSort([a[key], b[key]])[0] === a[key] ? -1 : 1);
979
985
  };
980
-
981
986
  //#endregion
982
987
  //#region src/dom.ts
983
988
  /**
@@ -1056,7 +1061,6 @@ const getCodeOfDate = () => getCodeOfTime(dateFmt(/* @__PURE__ */ new Date(), "d
1056
1061
  * 获取当月的时间戳的异或值的绝对值, 当月内获取的值是唯一的
1057
1062
  */
1058
1063
  const getCodeOfMonth = () => getCodeOfTime(dateFmt(dayjs().endOf("month"), "date"));
1059
-
1060
1064
  //#endregion
1061
1065
  //#region src/mock.ts
1062
1066
  const getMockData = (data, code = 200, message = "success") => {
@@ -1078,6 +1082,5 @@ const getMockDataList = (data, total, code = 200, message = "success", options =
1078
1082
  message
1079
1083
  };
1080
1084
  };
1081
-
1082
1085
  //#endregion
1083
- export { attachFmt, camel2Kebab, camel2Pascal, camel2Snake, camel2Title, checkImg, cnIdCard15Re, cnIdCard18Re, cnPhoneRE, cnTelRE, cnTelRE2, cnUSCIRE, currencyFmt, dateDuration, dateFmt, dayjs, emailRE, filterObj, fixiOSInputAutoZoomIn, getBaseAttachUrl, getCodeOfDate, getCodeOfHour, getCodeOfMonth, getHostEnv, getMockData, getMockDataList, 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 };
1086
+ export { attachFmt, camel2Kebab, camel2Pascal, camel2Snake, camel2Title, checkImg, cnIdCard15Re, cnIdCard18Re, cnPhoneRE, cnTelRE, cnTelRE2, cnUSCIRE, currencyFmt, dateDuration, dateFmt, dayjs, emailRE, filterObj, fixiOSInputAutoZoomIn, getBaseAttachUrl, getCodeOfDate, getCodeOfHour, getCodeOfMonth, getHostEnv, getMockData, getMockDataList, 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, isPureFunction, 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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jhqn/utils-core",
3
- "version": "0.4.4",
3
+ "version": "0.4.6",
4
4
  "keywords": [
5
5
  "jade",
6
6
  "utils"
@@ -33,7 +33,7 @@
33
33
  }
34
34
  },
35
35
  "dependencies": {
36
- "dayjs": "^1.11.19",
36
+ "dayjs": "^1.11.21",
37
37
  "detect-browser-es": "0.1.1"
38
38
  },
39
39
  "scripts": {