@jhqn/utils-core 0.6.0 → 1.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.d.ts +2 -40
- package/dist/index.js +23 -38
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -4,7 +4,6 @@
|
|
|
4
4
|
/// <reference types="dayjs/plugin/customParseFormat" />
|
|
5
5
|
/// <reference types="dayjs/plugin/objectSupport" />
|
|
6
6
|
import dayjs, { ConfigType, Dayjs } from "dayjs";
|
|
7
|
-
//#region types/index.d.ts
|
|
8
7
|
/**
|
|
9
8
|
* 数字或字符串
|
|
10
9
|
*/
|
|
@@ -57,8 +56,6 @@ type Dict<T = string> = Record<string, T>;
|
|
|
57
56
|
* 使某键可选
|
|
58
57
|
*/
|
|
59
58
|
type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
60
|
-
//#endregion
|
|
61
|
-
//#region types/dom.d.ts
|
|
62
59
|
interface HostEnv {
|
|
63
60
|
/**
|
|
64
61
|
* 是否是浙里办
|
|
@@ -89,8 +86,6 @@ interface HostEnv {
|
|
|
89
86
|
*/
|
|
90
87
|
readonly mini: boolean;
|
|
91
88
|
}
|
|
92
|
-
//#endregion
|
|
93
|
-
//#region src/case.d.ts
|
|
94
89
|
/**
|
|
95
90
|
* snake2Kebab
|
|
96
91
|
* @param {string} str 字符串
|
|
@@ -231,8 +226,6 @@ export declare const title2Pascal: (str: string) => string;
|
|
|
231
226
|
* @example title2Camel('Hello World') // Hello World => helloWorld
|
|
232
227
|
*/
|
|
233
228
|
export declare const title2Camel: (str: string) => string;
|
|
234
|
-
//#endregion
|
|
235
|
-
//#region src/console/log-common.d.ts
|
|
236
229
|
/**
|
|
237
230
|
* jade console.log
|
|
238
231
|
* only for development
|
|
@@ -283,22 +276,16 @@ export declare const jTimeEnd: (str: string) => void;
|
|
|
283
276
|
* jade console.clear
|
|
284
277
|
*/
|
|
285
278
|
export declare const jClear: () => void;
|
|
286
|
-
//#endregion
|
|
287
|
-
//#region src/console/log-dedupe.d.ts
|
|
288
279
|
/**
|
|
289
280
|
* 不连续打印相同的值
|
|
290
281
|
* @param args
|
|
291
282
|
*/
|
|
292
283
|
export declare const jLogDedupe: (...args: any[]) => void;
|
|
293
|
-
//#endregion
|
|
294
|
-
//#region src/console/log-once.d.ts
|
|
295
284
|
/**
|
|
296
285
|
* 只打印一次
|
|
297
286
|
* @param args
|
|
298
287
|
*/
|
|
299
288
|
export declare const jLogOnce: (...args: any[]) => void;
|
|
300
|
-
//#endregion
|
|
301
|
-
//#region src/desensitize.d.ts
|
|
302
289
|
/**
|
|
303
290
|
* 隐藏中间部分
|
|
304
291
|
* @param {string} str 需要隐藏的字符串
|
|
@@ -421,8 +408,6 @@ export declare const hideEmail: (email: UnDef<string>) => string;
|
|
|
421
408
|
* ```
|
|
422
409
|
*/
|
|
423
410
|
export declare const hideBankCard: (bankCard: UnDef<string>) => string;
|
|
424
|
-
//#endregion
|
|
425
|
-
//#region src/dom.d.ts
|
|
426
411
|
/**
|
|
427
412
|
* 判断是否是浏览器环境
|
|
428
413
|
*/
|
|
@@ -468,8 +453,6 @@ export declare const getCodeOfDate: () => number;
|
|
|
468
453
|
* 获取当月的时间戳的异或值的绝对值, 当月内获取的值是唯一的
|
|
469
454
|
*/
|
|
470
455
|
export declare const getCodeOfMonth: () => number;
|
|
471
|
-
//#endregion
|
|
472
|
-
//#region src/format/baseAttachUrl.d.ts
|
|
473
456
|
/**
|
|
474
457
|
* 设置文件基本路径
|
|
475
458
|
* @param baseUrl
|
|
@@ -479,8 +462,6 @@ export declare function setBaseAttachUrl(baseUrl: string): void;
|
|
|
479
462
|
* 获取文件基本路径
|
|
480
463
|
*/
|
|
481
464
|
export declare function getBaseAttachUrl(): string;
|
|
482
|
-
//#endregion
|
|
483
|
-
//#region src/format/core.d.ts
|
|
484
465
|
/**
|
|
485
466
|
* JSON 字符串转对象
|
|
486
467
|
* @template T - 解析数据类型
|
|
@@ -508,10 +489,10 @@ export declare function parseJSON<T = any>(str: string, reviverFn?: (this: any,
|
|
|
508
489
|
export declare function stringifyJSON(value: any, replacerFn?: (this: any, key: string, value: any) => any): string;
|
|
509
490
|
/**
|
|
510
491
|
* JSON.stringify() second param
|
|
511
|
-
* @param
|
|
492
|
+
* @param key
|
|
512
493
|
* @param value
|
|
513
494
|
*/
|
|
514
|
-
export declare function replacer(
|
|
495
|
+
export declare function replacer(this: any, key: any, value: any): any;
|
|
515
496
|
/**
|
|
516
497
|
* JSON.parse() second param
|
|
517
498
|
* @param _key
|
|
@@ -527,8 +508,6 @@ export declare function reviver(_key: any, value: any): any;
|
|
|
527
508
|
export declare const filterObj: (obj: Record<string, any>, keys: string[], reverse?: boolean) => {
|
|
528
509
|
[k: string]: any;
|
|
529
510
|
};
|
|
530
|
-
//#endregion
|
|
531
|
-
//#region src/format/currency.d.ts
|
|
532
511
|
/**
|
|
533
512
|
* 千位分隔符
|
|
534
513
|
* @param num
|
|
@@ -544,8 +523,6 @@ export declare const toThousands: (num: number) => string;
|
|
|
544
523
|
* @param {number} [maximumFractionDigits] - 最多小数位数
|
|
545
524
|
*/
|
|
546
525
|
export declare const currencyFmt: (currency: UnDef<Numeric | bigint>, maximumFractionDigits?: number) => string;
|
|
547
|
-
//#endregion
|
|
548
|
-
//#region src/format/dayjs.d.ts
|
|
549
526
|
/**
|
|
550
527
|
* 格式化时间
|
|
551
528
|
* @example
|
|
@@ -564,8 +541,6 @@ export declare const dateFmt: (date: ConfigType, format?: string | "date" | "dat
|
|
|
564
541
|
* @param {import('dayjs').ConfigType} end 结束时间
|
|
565
542
|
*/
|
|
566
543
|
export declare const dateDuration: (start: ConfigType, end: ConfigType) => string;
|
|
567
|
-
//#endregion
|
|
568
|
-
//#region types/upload.d.ts
|
|
569
544
|
type VantUploadFileStatus = '' | 'uploading' | 'done' | 'failed';
|
|
570
545
|
type AntdUploadFileStatus = 'removed' | 'uploading' | 'done' | 'error';
|
|
571
546
|
type ElementPlusUploadFileStatus = 'ready' | 'uploading' | 'success' | 'fail';
|
|
@@ -594,8 +569,6 @@ interface Resource {
|
|
|
594
569
|
/** element-plus */
|
|
595
570
|
percentage?: number;
|
|
596
571
|
}
|
|
597
|
-
//#endregion
|
|
598
|
-
//#region src/format/resource.d.ts
|
|
599
572
|
/**
|
|
600
573
|
* 获取文件完整地址
|
|
601
574
|
* @param url
|
|
@@ -649,8 +622,6 @@ export declare const attachFmt: (data: UnDef<MaybeArray<Resource>> | string) =>
|
|
|
649
622
|
thumbUrl?: string;
|
|
650
623
|
percentage?: number;
|
|
651
624
|
}[];
|
|
652
|
-
//#endregion
|
|
653
|
-
//#region src/format/sort.d.ts
|
|
654
625
|
/**
|
|
655
626
|
* 自然排序
|
|
656
627
|
* @param {UnDef<string>[] | UnDef<number>[]} arr - 数字或字符串数组
|
|
@@ -664,12 +635,8 @@ export declare const naturalSort: (arr: UnDef<string>[] | UnDef<number>[]) => Un
|
|
|
664
635
|
* @returns {any[]} - 排序后的数组
|
|
665
636
|
*/
|
|
666
637
|
export declare const naturalSortBy: <T extends Record<string, UnDef<Numeric>>>(arr: T[], key: keyof T) => T[];
|
|
667
|
-
//#endregion
|
|
668
|
-
//#region src/mock.d.ts
|
|
669
638
|
export declare const getMockData: <T>(data: T, code?: number, message?: string) => Res<T>;
|
|
670
639
|
export declare const getMockDataList: <T>(data: T[], total: number, code?: number, message?: string, options?: Partial<PageResData<T>>) => PageRes<T>;
|
|
671
|
-
//#endregion
|
|
672
|
-
//#region src/validate/core.d.ts
|
|
673
640
|
/**
|
|
674
641
|
* 检测变量类型
|
|
675
642
|
* @param {any} val 检测的目标
|
|
@@ -818,8 +785,6 @@ export declare const isValidKey: (key: string | number | symbol, object: object)
|
|
|
818
785
|
* @param val
|
|
819
786
|
*/
|
|
820
787
|
export declare const isEmpty: (val: UnDef<Numeric | boolean | Record<any, any> | Array<any> | Set<any> | Map<any, any>>) => boolean;
|
|
821
|
-
//#endregion
|
|
822
|
-
//#region src/validate/form.d.ts
|
|
823
788
|
export declare const cnPhoneRE: RegExp;
|
|
824
789
|
export declare const cnTelRE: RegExp;
|
|
825
790
|
export declare const cnTelRE2: RegExp;
|
|
@@ -880,8 +845,6 @@ export declare const nonASCII: (text: string) => boolean;
|
|
|
880
845
|
* @param {string} type 文件类型, 与input.accept一致
|
|
881
846
|
*/
|
|
882
847
|
export declare const isValidFileType: (file: File, type: string) => boolean;
|
|
883
|
-
//#endregion
|
|
884
|
-
//#region src/validate/request.d.ts
|
|
885
848
|
/**
|
|
886
849
|
* 校验接口返回code
|
|
887
850
|
* @param res
|
|
@@ -906,5 +869,4 @@ export declare const isValidArrRes: (res: Res<any[]>, code?: number) => boolean;
|
|
|
906
869
|
* @param code
|
|
907
870
|
*/
|
|
908
871
|
export declare const isValidPageRes: (res: PageRes, code?: number) => boolean;
|
|
909
|
-
//#endregion
|
|
910
872
|
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, dayjs };
|
package/dist/index.js
CHANGED
|
@@ -5,7 +5,7 @@ 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
|
-
|
|
8
|
+
// src/case.ts
|
|
9
9
|
/**
|
|
10
10
|
* snake2Kebab
|
|
11
11
|
* @param {string} str 字符串
|
|
@@ -149,8 +149,7 @@ const title2Pascal = (str) => str.replace(/ /g, "");
|
|
|
149
149
|
* @example title2Camel('Hello World') // Hello World => helloWorld
|
|
150
150
|
*/
|
|
151
151
|
const title2Camel = (str) => str.replace(/ /g, "").replace(/^[A-Z]/, (match) => match.toLowerCase());
|
|
152
|
-
|
|
153
|
-
//#region src/console/log-common.ts
|
|
152
|
+
// src/console/log-common.ts
|
|
154
153
|
const sharedStyle = "color: white; padding: 1px 2px;";
|
|
155
154
|
/**
|
|
156
155
|
* jade console.log
|
|
@@ -202,8 +201,7 @@ const jTimeEnd = (str) => console?.timeEnd(`[${str}]`);
|
|
|
202
201
|
* jade console.clear
|
|
203
202
|
*/
|
|
204
203
|
const jClear = console?.clear?.bind(console);
|
|
205
|
-
|
|
206
|
-
//#region src/console/log-dedupe.ts
|
|
204
|
+
// src/console/log-dedupe.ts
|
|
207
205
|
let prevValue;
|
|
208
206
|
/**
|
|
209
207
|
* 不连续打印相同的值
|
|
@@ -215,8 +213,7 @@ const jLogDedupe = (...args) => {
|
|
|
215
213
|
jLog(...args);
|
|
216
214
|
prevValue = args;
|
|
217
215
|
};
|
|
218
|
-
|
|
219
|
-
//#region src/console/log-once.ts
|
|
216
|
+
// src/console/log-once.ts
|
|
220
217
|
let logged = false;
|
|
221
218
|
/**
|
|
222
219
|
* 只打印一次
|
|
@@ -227,8 +224,7 @@ const jLogOnce = (...args) => {
|
|
|
227
224
|
jLog(...args);
|
|
228
225
|
logged = true;
|
|
229
226
|
};
|
|
230
|
-
|
|
231
|
-
//#region src/validate/core.ts
|
|
227
|
+
// src/validate/core.ts
|
|
232
228
|
/**
|
|
233
229
|
* 检测变量类型
|
|
234
230
|
* @param {any} val 检测的目标
|
|
@@ -413,8 +409,7 @@ const isValidKey = (key, object) => key in object;
|
|
|
413
409
|
const isEmpty = (val) => {
|
|
414
410
|
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;
|
|
415
411
|
};
|
|
416
|
-
|
|
417
|
-
//#region src/validate/form.ts
|
|
412
|
+
// src/validate/form.ts
|
|
418
413
|
const cnPhoneRE = /^1[3-9]\d{9}$/;
|
|
419
414
|
const cnTelRE = /^0\d{2,3}-?\d{7,8}$/;
|
|
420
415
|
const cnTelRE2 = /^\(0\d{2,3}\)\d{7,8}$/;
|
|
@@ -513,8 +508,7 @@ const isValidFileType = (file, type) => {
|
|
|
513
508
|
if (typeArrStr.length > 1) return typeArrStr.some(validate);
|
|
514
509
|
return validate(type);
|
|
515
510
|
};
|
|
516
|
-
|
|
517
|
-
//#region src/validate/request.ts
|
|
511
|
+
// src/validate/request.ts
|
|
518
512
|
/**
|
|
519
513
|
* 校验接口返回code
|
|
520
514
|
* @param res
|
|
@@ -539,8 +533,7 @@ const isValidArrRes = (res, code) => isValidResCode(res, code) && isArray(res.da
|
|
|
539
533
|
* @param code
|
|
540
534
|
*/
|
|
541
535
|
const isValidPageRes = (res, code) => isValidResCode(res, code) && isDef(res.data) && isArray(res.data.list || res.data.records);
|
|
542
|
-
|
|
543
|
-
//#region src/desensitize.ts
|
|
536
|
+
// src/desensitize.ts
|
|
544
537
|
/**
|
|
545
538
|
* 隐藏中间部分
|
|
546
539
|
* @param {string} str 需要隐藏的字符串
|
|
@@ -726,8 +719,7 @@ const hideBankCard = (bankCard) => {
|
|
|
726
719
|
}
|
|
727
720
|
return "";
|
|
728
721
|
};
|
|
729
|
-
|
|
730
|
-
//#region src/format/baseAttachUrl.ts
|
|
722
|
+
// src/format/baseAttachUrl.ts
|
|
731
723
|
/**
|
|
732
724
|
* 文件基本路径
|
|
733
725
|
*/
|
|
@@ -745,8 +737,7 @@ function setBaseAttachUrl(baseUrl) {
|
|
|
745
737
|
function getBaseAttachUrl() {
|
|
746
738
|
return __BASE_ATTACH_URL__;
|
|
747
739
|
}
|
|
748
|
-
|
|
749
|
-
//#region src/format/core.ts
|
|
740
|
+
// src/format/core.ts
|
|
750
741
|
/**
|
|
751
742
|
* JSON 字符串转对象
|
|
752
743
|
* @template T - 解析数据类型
|
|
@@ -780,10 +771,15 @@ function stringifyJSON(value, replacerFn = replacer) {
|
|
|
780
771
|
}
|
|
781
772
|
/**
|
|
782
773
|
* JSON.stringify() second param
|
|
783
|
-
* @param
|
|
774
|
+
* @param key
|
|
784
775
|
* @param value
|
|
785
776
|
*/
|
|
786
|
-
function replacer(
|
|
777
|
+
function replacer(key, value) {
|
|
778
|
+
const rawValue = this === void 0 ? value : this[key];
|
|
779
|
+
if (isDate(rawValue)) return {
|
|
780
|
+
dataType: "Date",
|
|
781
|
+
value: rawValue.toISOString()
|
|
782
|
+
};
|
|
787
783
|
if (isMap(value)) return {
|
|
788
784
|
dataType: "Map",
|
|
789
785
|
value: [...value]
|
|
@@ -792,10 +788,6 @@ function replacer(_key, value) {
|
|
|
792
788
|
dataType: "Set",
|
|
793
789
|
value: [...value]
|
|
794
790
|
};
|
|
795
|
-
if (isDate(value)) return {
|
|
796
|
-
dataType: "Date",
|
|
797
|
-
value: value.toISOString()
|
|
798
|
-
};
|
|
799
791
|
return value;
|
|
800
792
|
}
|
|
801
793
|
/**
|
|
@@ -818,8 +810,7 @@ function reviver(_key, value) {
|
|
|
818
810
|
* @param {boolean} reverse 为true时过滤取反
|
|
819
811
|
*/
|
|
820
812
|
const filterObj = (obj, keys, reverse = false) => Object.fromEntries(Object.entries(obj).filter(([key]) => reverse ? !keys.includes(key) : keys.includes(key)));
|
|
821
|
-
|
|
822
|
-
//#region src/format/currency.ts
|
|
813
|
+
// src/format/currency.ts
|
|
823
814
|
/**
|
|
824
815
|
* 千位分隔符
|
|
825
816
|
* @param num
|
|
@@ -849,8 +840,7 @@ const currencyFmt = (currency, maximumFractionDigits = 2) => {
|
|
|
849
840
|
maximumFractionDigits
|
|
850
841
|
}).format(num);
|
|
851
842
|
};
|
|
852
|
-
|
|
853
|
-
//#region src/format/dayjs.ts
|
|
843
|
+
// src/format/dayjs.ts
|
|
854
844
|
dayjs.extend(localeData).locale("zh-cn", zhCN);
|
|
855
845
|
dayjs.extend(duration);
|
|
856
846
|
dayjs.extend(relativeTime);
|
|
@@ -895,8 +885,7 @@ const dateDuration = (start, end) => {
|
|
|
895
885
|
].join("");
|
|
896
886
|
return dd.format(format);
|
|
897
887
|
};
|
|
898
|
-
|
|
899
|
-
//#region src/format/resource.ts
|
|
888
|
+
// src/format/resource.ts
|
|
900
889
|
/**
|
|
901
890
|
* 获取文件完整地址
|
|
902
891
|
* @param url
|
|
@@ -948,8 +937,7 @@ const attachFmt = (data) => {
|
|
|
948
937
|
} else if (prototype === "[object Object]") attachList = [data];
|
|
949
938
|
return attachList.map(recoverFile);
|
|
950
939
|
};
|
|
951
|
-
|
|
952
|
-
//#region src/format/sort.ts
|
|
940
|
+
// src/format/sort.ts
|
|
953
941
|
/**
|
|
954
942
|
* 判断是否为数字数组
|
|
955
943
|
* @param {UnDef<string>[] | UnDef<number>[]} arr - 数字或字符串数组
|
|
@@ -984,8 +972,7 @@ const naturalSort = (arr) => {
|
|
|
984
972
|
const naturalSortBy = (arr, key) => {
|
|
985
973
|
return [...arr].sort((a, b) => naturalSort([a[key], b[key]])[0] === a[key] ? -1 : 1);
|
|
986
974
|
};
|
|
987
|
-
|
|
988
|
-
//#region src/dom.ts
|
|
975
|
+
// src/dom.ts
|
|
989
976
|
/**
|
|
990
977
|
* 判断是否是浏览器环境
|
|
991
978
|
*/
|
|
@@ -1062,8 +1049,7 @@ const getCodeOfDate = () => getCodeOfTime(dateFmt(/* @__PURE__ */ new Date(), "d
|
|
|
1062
1049
|
* 获取当月的时间戳的异或值的绝对值, 当月内获取的值是唯一的
|
|
1063
1050
|
*/
|
|
1064
1051
|
const getCodeOfMonth = () => getCodeOfTime(dateFmt(dayjs().endOf("month"), "date"));
|
|
1065
|
-
|
|
1066
|
-
//#region src/mock.ts
|
|
1052
|
+
// src/mock.ts
|
|
1067
1053
|
const getMockData = (data, code = 200, message = "success") => {
|
|
1068
1054
|
return {
|
|
1069
1055
|
data,
|
|
@@ -1083,5 +1069,4 @@ const getMockDataList = (data, total, code = 200, message = "success", options =
|
|
|
1083
1069
|
message
|
|
1084
1070
|
};
|
|
1085
1071
|
};
|
|
1086
|
-
//#endregion
|
|
1087
1072
|
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 };
|