@jhqn/utils-core 0.0.3-beta.0 → 0.0.3-beta.10
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 +107 -53
- package/dist/index.d.cts +161 -114
- package/dist/index.d.mts +161 -114
- package/dist/index.d.ts +161 -114
- package/dist/index.mjs +101 -50
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -7,6 +7,59 @@ import { ConfigType } from 'dayjs';
|
|
|
7
7
|
export { Dayjs, default as dayjs } from 'dayjs';
|
|
8
8
|
export { detect } from 'detect-browser';
|
|
9
9
|
|
|
10
|
+
/**
|
|
11
|
+
* 数字或字符串
|
|
12
|
+
*/
|
|
13
|
+
type Numeric = number | string;
|
|
14
|
+
/**
|
|
15
|
+
* 可 Null
|
|
16
|
+
*/
|
|
17
|
+
type Nullable<T> = T | null;
|
|
18
|
+
/**
|
|
19
|
+
* 可 Undefined
|
|
20
|
+
*/
|
|
21
|
+
type Undefinable<T> = T | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* 可 Null 或 Undefined
|
|
24
|
+
*/
|
|
25
|
+
type NullOrUndefinable<T> = Nullable<Undefinable<T>>;
|
|
26
|
+
/**
|
|
27
|
+
* alias for NullOrUndefinable
|
|
28
|
+
*/
|
|
29
|
+
type UnDef<T> = NullOrUndefinable<T>;
|
|
30
|
+
/**
|
|
31
|
+
* 请求结果
|
|
32
|
+
*/
|
|
33
|
+
type Res<T = any> = Service.Response<T>;
|
|
34
|
+
/**
|
|
35
|
+
* 分页请求结果数据
|
|
36
|
+
*/
|
|
37
|
+
type PageResData<T = any> = Service.PageResponseData<T>;
|
|
38
|
+
/**
|
|
39
|
+
* 分页请求结果
|
|
40
|
+
*/
|
|
41
|
+
type PageRes<T = any> = Service.PageResponse<T>;
|
|
42
|
+
/**
|
|
43
|
+
* 分页请求参数
|
|
44
|
+
*/
|
|
45
|
+
type PageReq<T extends Record<string, any> = NonNullable<unknown>> = Service.PageRequestParams<T>;
|
|
46
|
+
/**
|
|
47
|
+
* 可能是数组
|
|
48
|
+
*/
|
|
49
|
+
type MaybeArray<T = any> = T | Array<T>;
|
|
50
|
+
/**
|
|
51
|
+
* 枚举键值对
|
|
52
|
+
*/
|
|
53
|
+
type VK<T = string> = Record<string | number, T>;
|
|
54
|
+
/**
|
|
55
|
+
* 字典
|
|
56
|
+
*/
|
|
57
|
+
type Dict<T = string> = Record<string, T>;
|
|
58
|
+
/**
|
|
59
|
+
* 使某键可选
|
|
60
|
+
*/
|
|
61
|
+
type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
62
|
+
|
|
10
63
|
/**
|
|
11
64
|
* snake2Kebab
|
|
12
65
|
* @param {string} str 字符串
|
|
@@ -217,43 +270,6 @@ declare const jLogDedupe: (...args: any[]) => void;
|
|
|
217
270
|
*/
|
|
218
271
|
declare const jLogOnce: (...args: any[]) => void;
|
|
219
272
|
|
|
220
|
-
/**
|
|
221
|
-
* 数字或字符串
|
|
222
|
-
*/
|
|
223
|
-
declare type Numeric = number | string;
|
|
224
|
-
/**
|
|
225
|
-
* 可 Null
|
|
226
|
-
*/
|
|
227
|
-
declare type Nullable<T> = T | null;
|
|
228
|
-
/**
|
|
229
|
-
* 可 Undefined
|
|
230
|
-
*/
|
|
231
|
-
declare type Undefinable<T> = T | undefined;
|
|
232
|
-
/**
|
|
233
|
-
* 可 Null 或 Undefined
|
|
234
|
-
*/
|
|
235
|
-
declare type NullOrUndefinable<T> = Nullable<Undefinable<T>>;
|
|
236
|
-
/**
|
|
237
|
-
* alias for NullOrUndefinable
|
|
238
|
-
*/
|
|
239
|
-
declare type UnDef<T> = NullOrUndefinable<T>;
|
|
240
|
-
/**
|
|
241
|
-
* 请求结果
|
|
242
|
-
*/
|
|
243
|
-
declare type Res<T = any> = Service.Response<T>;
|
|
244
|
-
/**
|
|
245
|
-
* 分页请求结果数据
|
|
246
|
-
*/
|
|
247
|
-
declare type PageResData<T = any> = Service.PageResponseData<T>;
|
|
248
|
-
/**
|
|
249
|
-
* 分页请求结果
|
|
250
|
-
*/
|
|
251
|
-
declare type PageRes<T = any> = Service.PageResponse<T>;
|
|
252
|
-
/**
|
|
253
|
-
* 可能是数组
|
|
254
|
-
*/
|
|
255
|
-
declare type MaybeArray<T = any> = T | Array<T>;
|
|
256
|
-
|
|
257
273
|
/**
|
|
258
274
|
* 隐藏中间部分
|
|
259
275
|
* @param {string} str 需要隐藏的字符串
|
|
@@ -327,6 +343,10 @@ interface HostEnv {
|
|
|
327
343
|
* 是否是浙政钉
|
|
328
344
|
*/
|
|
329
345
|
readonly zzd: boolean
|
|
346
|
+
/**
|
|
347
|
+
* 是否是钉钉/专有钉/浙政钉
|
|
348
|
+
*/
|
|
349
|
+
readonly dd: boolean
|
|
330
350
|
/**
|
|
331
351
|
* 是否是微信
|
|
332
352
|
*/
|
|
@@ -362,6 +382,16 @@ declare const fixiOSInputAutoZoomIn: () => void;
|
|
|
362
382
|
declare const userAgent: string;
|
|
363
383
|
/**
|
|
364
384
|
* 获取宿主环境信息
|
|
385
|
+
* @returns {HostEnv} 宿主环境信息
|
|
386
|
+
* @example
|
|
387
|
+
* const hostEnv = getHostEnv()
|
|
388
|
+
* hostEnv.zlb // 浙里办
|
|
389
|
+
* hostEnv.zyd // 专有钉
|
|
390
|
+
* hostEnv.zzd // 浙政钉
|
|
391
|
+
* hostEnv.dd // 钉钉/专有钉/浙政钉
|
|
392
|
+
* hostEnv.wx // 微信
|
|
393
|
+
* hostEnv.zfb // 支付宝
|
|
394
|
+
* hostEnv.mini // 小程序
|
|
365
395
|
*/
|
|
366
396
|
declare const getHostEnv: () => HostEnv;
|
|
367
397
|
|
|
@@ -378,12 +408,22 @@ declare const getCodeOfDate: () => number;
|
|
|
378
408
|
*/
|
|
379
409
|
declare const getCodeOfMonth: () => number;
|
|
380
410
|
|
|
411
|
+
/**
|
|
412
|
+
* 设置文件基本路径
|
|
413
|
+
* @param baseUrl
|
|
414
|
+
*/
|
|
415
|
+
declare function setBaseAttachUrl(baseUrl: string): void;
|
|
416
|
+
/**
|
|
417
|
+
* 获取文件基本路径
|
|
418
|
+
*/
|
|
419
|
+
declare function getBaseAttachUrl(): string;
|
|
420
|
+
|
|
381
421
|
/**
|
|
382
422
|
* Json字符串转对象
|
|
383
423
|
* @param str
|
|
384
424
|
* @param reviver
|
|
385
425
|
*/
|
|
386
|
-
declare const parseToJSON: <T = any>(str: string, reviver?: (
|
|
426
|
+
declare const parseToJSON: <T = any>(str: string, reviver?: (this: any, key: string, value: any) => any) => Nullable<T>;
|
|
387
427
|
/**
|
|
388
428
|
* JSON.stringify() second param
|
|
389
429
|
* @param _
|
|
@@ -406,13 +446,25 @@ declare const filterObj: (obj: Record<string, any>, keys: string[], reverse?: bo
|
|
|
406
446
|
[k: string]: any;
|
|
407
447
|
};
|
|
408
448
|
|
|
449
|
+
/**
|
|
450
|
+
* 千位分隔符
|
|
451
|
+
* @param num
|
|
452
|
+
*/
|
|
453
|
+
declare const toThousands: (num: number) => string | undefined;
|
|
454
|
+
/**
|
|
455
|
+
* 货币格式化
|
|
456
|
+
* @param {number | string | bigint} currency - 金额
|
|
457
|
+
* @param {number} [maximumFractionDigits] - 小数位数
|
|
458
|
+
*/
|
|
459
|
+
declare const currencyFmt: (currency: UnDef<Numeric | bigint>, maximumFractionDigits?: number) => string;
|
|
460
|
+
|
|
409
461
|
/**
|
|
410
462
|
* 格式化时间
|
|
411
463
|
* @param {import('dayjs').ConfigType} date 时间
|
|
412
464
|
* @param {string} format 格式
|
|
413
465
|
* @returns {string} 格式化后的时间
|
|
414
466
|
*/
|
|
415
|
-
declare const dateFmt: (date: ConfigType, format?: string |
|
|
467
|
+
declare const dateFmt: (date: ConfigType, format?: string | "date" | "datetime") => string;
|
|
416
468
|
/**
|
|
417
469
|
* 格式化时间间隔
|
|
418
470
|
* @param {import('dayjs').ConfigType} start 开始时间
|
|
@@ -420,29 +472,6 @@ declare const dateFmt: (date: ConfigType, format?: string | 'date' | 'datetime')
|
|
|
420
472
|
*/
|
|
421
473
|
declare const dateDuration: (start: ConfigType, end: ConfigType) => string;
|
|
422
474
|
|
|
423
|
-
/**
|
|
424
|
-
* 设置文件基本路径
|
|
425
|
-
* @param key
|
|
426
|
-
* @param baseUrl
|
|
427
|
-
*/
|
|
428
|
-
declare function setBaseAttachUrl(baseUrl: string): void;
|
|
429
|
-
/**
|
|
430
|
-
* 获取文件基本路径
|
|
431
|
-
*/
|
|
432
|
-
declare function getBaseAttachUrl(): string;
|
|
433
|
-
|
|
434
|
-
/**
|
|
435
|
-
* 千位分隔符
|
|
436
|
-
* @param num
|
|
437
|
-
*/
|
|
438
|
-
declare const toThousands: (num: number) => string | undefined;
|
|
439
|
-
/**
|
|
440
|
-
* 货币格式化
|
|
441
|
-
* @param {number | string | bigint} currency - 金额
|
|
442
|
-
* @param {number} [maximumFractionDigits] - 小数位数
|
|
443
|
-
*/
|
|
444
|
-
declare const currencyFmt: (currency: UnDef<Numeric | bigint>, maximumFractionDigits?: number) => string;
|
|
445
|
-
|
|
446
475
|
type VantUploadFileStatus = '' | 'uploading' | 'done' | 'failed'
|
|
447
476
|
type AntdUploadFileStatus = 'removed' | 'uploading' | 'done' | 'error'
|
|
448
477
|
type ElementPlusUploadFileStatus = 'ready' | 'uploading' | 'success' | 'fail'
|
|
@@ -491,19 +520,16 @@ declare const recoverFile: (resource: Resource) => {
|
|
|
491
520
|
group: string;
|
|
492
521
|
id: string;
|
|
493
522
|
name: string;
|
|
494
|
-
size?: number
|
|
495
|
-
uid?: string
|
|
496
|
-
objectUrl?: string
|
|
497
|
-
isImage?: boolean
|
|
498
|
-
message?: string
|
|
499
|
-
deletable?: boolean
|
|
500
|
-
reupload?: boolean
|
|
501
|
-
fileName?: string
|
|
502
|
-
thumbUrl?: string
|
|
503
|
-
|
|
504
|
-
* @param resource
|
|
505
|
-
*/
|
|
506
|
-
percentage?: number | undefined;
|
|
523
|
+
size?: number;
|
|
524
|
+
uid?: string;
|
|
525
|
+
objectUrl?: string;
|
|
526
|
+
isImage?: boolean;
|
|
527
|
+
message?: string;
|
|
528
|
+
deletable?: boolean;
|
|
529
|
+
reupload?: boolean;
|
|
530
|
+
fileName?: string;
|
|
531
|
+
thumbUrl?: string;
|
|
532
|
+
percentage?: number;
|
|
507
533
|
};
|
|
508
534
|
/**
|
|
509
535
|
* 附件格式
|
|
@@ -518,21 +544,32 @@ declare const attachFmt: (data: UnDef<MaybeArray<Resource>> | string) => {
|
|
|
518
544
|
group: string;
|
|
519
545
|
id: string;
|
|
520
546
|
name: string;
|
|
521
|
-
size?: number
|
|
522
|
-
uid?: string
|
|
523
|
-
objectUrl?: string
|
|
524
|
-
isImage?: boolean
|
|
525
|
-
message?: string
|
|
526
|
-
deletable?: boolean
|
|
527
|
-
reupload?: boolean
|
|
528
|
-
fileName?: string
|
|
529
|
-
thumbUrl?: string
|
|
530
|
-
|
|
531
|
-
* @param resource
|
|
532
|
-
*/
|
|
533
|
-
percentage?: number | undefined;
|
|
547
|
+
size?: number;
|
|
548
|
+
uid?: string;
|
|
549
|
+
objectUrl?: string;
|
|
550
|
+
isImage?: boolean;
|
|
551
|
+
message?: string;
|
|
552
|
+
deletable?: boolean;
|
|
553
|
+
reupload?: boolean;
|
|
554
|
+
fileName?: string;
|
|
555
|
+
thumbUrl?: string;
|
|
556
|
+
percentage?: number;
|
|
534
557
|
}[];
|
|
535
558
|
|
|
559
|
+
/**
|
|
560
|
+
* 自然排序
|
|
561
|
+
* @param {UnDef<string>[] | UnDef<number>[]} arr - 数字或字符串数组
|
|
562
|
+
* @returns {UnDef<string>[] | UnDef<number>[]} - 排序后的数组
|
|
563
|
+
*/
|
|
564
|
+
declare const naturalSort: (arr: UnDef<string>[] | UnDef<number>[]) => UnDef<string>[] | UnDef<number>[];
|
|
565
|
+
/**
|
|
566
|
+
* 根据 key 自然排序
|
|
567
|
+
* @param arr - 数组
|
|
568
|
+
* @param {string} key - 键名
|
|
569
|
+
* @returns {any[]} - 排序后的数组
|
|
570
|
+
*/
|
|
571
|
+
declare const naturalSortBy: <T extends Record<string, UnDef<Numeric>>>(arr: T[], key: keyof T) => T[];
|
|
572
|
+
|
|
536
573
|
/**
|
|
537
574
|
* 检测变量类型
|
|
538
575
|
* @param {any} val 检测的目标
|
|
@@ -582,7 +619,7 @@ declare const isObject: (val: any) => val is Record<any, any>;
|
|
|
582
619
|
* 是否是数组
|
|
583
620
|
* @param {any} val 检测的目标
|
|
584
621
|
*/
|
|
585
|
-
declare const isArray: (val: any) => val is any
|
|
622
|
+
declare const isArray: (val: any) => val is Array<any>;
|
|
586
623
|
/**
|
|
587
624
|
* 是否是函数,但也有可能是Class
|
|
588
625
|
* @param {any} val 检测的目标
|
|
@@ -670,7 +707,7 @@ declare const isArrStr: (str: UnDef<string>) => boolean;
|
|
|
670
707
|
* ```
|
|
671
708
|
* @returns {boolean} boolean
|
|
672
709
|
*/
|
|
673
|
-
declare const isValidKey: (key: string | number | symbol, object: object) => key is
|
|
710
|
+
declare const isValidKey: (key: string | number | symbol, object: object) => key is keyof typeof object;
|
|
674
711
|
/**
|
|
675
712
|
* 是否是空值/空数组/空对象 null undefined '' [] {} Set Map
|
|
676
713
|
* @param val
|
|
@@ -678,30 +715,15 @@ declare const isValidKey: (key: string | number | symbol, object: object) => key
|
|
|
678
715
|
declare const isEmpty: (val: UnDef<Numeric | boolean | Record<any, any> | Array<any> | Set<any> | Map<any, any>>) => boolean;
|
|
679
716
|
|
|
680
717
|
/**
|
|
681
|
-
*
|
|
682
|
-
* @
|
|
683
|
-
*
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
*
|
|
688
|
-
* @param res
|
|
689
|
-
* @param code
|
|
690
|
-
*/
|
|
691
|
-
declare const isValidRes: (res: Res, code?: number) => boolean;
|
|
692
|
-
/**
|
|
693
|
-
* 校验接口数组返回内容
|
|
694
|
-
* @param res
|
|
695
|
-
* @param code
|
|
696
|
-
*/
|
|
697
|
-
declare const isValidArrRes: (res: Res<any[]>, code?: number) => boolean;
|
|
698
|
-
/**
|
|
699
|
-
* 校验接口分页返回内容
|
|
700
|
-
* @param res
|
|
701
|
-
* @param code
|
|
718
|
+
* 是否是[统一社会信用代码](https://zh.wikipedia.org/zh-cn/%E7%BB%9F%E4%B8%80%E7%A4%BE%E4%BC%9A%E4%BF%A1%E7%94%A8%E4%BB%A3%E7%A0%81)
|
|
719
|
+
* @example
|
|
720
|
+
* ```ts
|
|
721
|
+
* isUSCI('91350100M000100Y43') // true
|
|
722
|
+
* ```
|
|
723
|
+
* @param {UnDef<string>} val - 待校验的值
|
|
724
|
+
* @returns {boolean} 是否是统一社会信用代码
|
|
702
725
|
*/
|
|
703
|
-
declare const
|
|
704
|
-
|
|
726
|
+
declare const isUSCI: (val: UnDef<string>) => boolean;
|
|
705
727
|
/**
|
|
706
728
|
* 是否是中国手机号
|
|
707
729
|
* @param val
|
|
@@ -746,4 +768,29 @@ declare const nonASCII: (text: string) => boolean;
|
|
|
746
768
|
*/
|
|
747
769
|
declare const isValidFileType: (file: File, type: string) => boolean;
|
|
748
770
|
|
|
749
|
-
|
|
771
|
+
/**
|
|
772
|
+
* 校验接口返回code
|
|
773
|
+
* @param res
|
|
774
|
+
* @param code
|
|
775
|
+
*/
|
|
776
|
+
declare const isValidResCode: (res: Res, code?: number) => boolean;
|
|
777
|
+
/**
|
|
778
|
+
* 校验接口返回内容
|
|
779
|
+
* @param res
|
|
780
|
+
* @param code
|
|
781
|
+
*/
|
|
782
|
+
declare const isValidRes: (res: Res, code?: number) => boolean;
|
|
783
|
+
/**
|
|
784
|
+
* 校验接口数组返回内容
|
|
785
|
+
* @param res
|
|
786
|
+
* @param code
|
|
787
|
+
*/
|
|
788
|
+
declare const isValidArrRes: (res: Res<any[]>, code?: number) => boolean;
|
|
789
|
+
/**
|
|
790
|
+
* 校验接口分页返回内容
|
|
791
|
+
* @param res
|
|
792
|
+
* @param code
|
|
793
|
+
*/
|
|
794
|
+
declare const isValidPageRes: (res: PageRes, code?: number) => boolean;
|
|
795
|
+
|
|
796
|
+
export { type Dict, 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, 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/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
|
|
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
|
|
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());
|
|
@@ -104,22 +104,26 @@ const isURL = (val) => getVariableType(val) === "url";
|
|
|
104
104
|
const isError = (val) => getVariableType(val) === "error";
|
|
105
105
|
const isDef = (val) => !isNull(val) && !isUndefined(val);
|
|
106
106
|
const isJSONStr = (str) => {
|
|
107
|
-
if (isDef(str))
|
|
107
|
+
if (isDef(str)) {
|
|
108
108
|
try {
|
|
109
109
|
JSON.parse(str);
|
|
110
110
|
return true;
|
|
111
111
|
} catch (e) {
|
|
112
|
+
jError("isJSONStr", e);
|
|
112
113
|
return false;
|
|
113
114
|
}
|
|
115
|
+
}
|
|
114
116
|
return false;
|
|
115
117
|
};
|
|
116
118
|
const isArrStr = (str) => {
|
|
117
|
-
if (isDef(str))
|
|
119
|
+
if (isDef(str)) {
|
|
118
120
|
try {
|
|
119
121
|
return Array.isArray(JSON.parse(str));
|
|
120
122
|
} catch (e) {
|
|
123
|
+
jError("isArrStr", e);
|
|
121
124
|
return false;
|
|
122
125
|
}
|
|
126
|
+
}
|
|
123
127
|
return false;
|
|
124
128
|
};
|
|
125
129
|
const isValidKey = (key, object) => key in object;
|
|
@@ -127,13 +131,14 @@ const isEmpty = (val) => {
|
|
|
127
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;
|
|
128
132
|
};
|
|
129
133
|
|
|
130
|
-
const isValidResCode = (res, code = 200) => res.code === code;
|
|
131
|
-
const isValidRes = (res, code) => isValidResCode(res, code) && isDef(res.data);
|
|
132
|
-
const isValidArrRes = (res, code) => isValidResCode(res, code) && Array.isArray(res.data);
|
|
133
|
-
const isValidPageRes = (res, code) => isValidResCode(res, code) && isDef(res.data) && Array.isArray(res.data.list || res.data.records);
|
|
134
|
-
|
|
135
134
|
const cnPhoneRE = /^1[3-9]\d{9}$/;
|
|
136
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
|
+
};
|
|
137
142
|
const isPhone = (val) => {
|
|
138
143
|
if (!isDef(val))
|
|
139
144
|
return false;
|
|
@@ -147,14 +152,14 @@ const isTel = (val) => {
|
|
|
147
152
|
const isEmail = (val) => {
|
|
148
153
|
if (!isDef(val))
|
|
149
154
|
return false;
|
|
150
|
-
const reg = /^[a-
|
|
155
|
+
const reg = /^[a-z\d](?:\w|-)+@[a-z\d]+\.[a-z]{2,5}$/i;
|
|
151
156
|
return reg.test(val);
|
|
152
157
|
};
|
|
153
158
|
const isIdCard = (val, compatible = true) => {
|
|
154
159
|
if (!isDef(val))
|
|
155
160
|
return false;
|
|
156
|
-
const card15 = /^[1-9]\d{7}(?:0[1-9]|10|11|12)(?:[0-2][1-9]|10|20|30|31)\d{2}[\
|
|
157
|
-
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}[\
|
|
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;
|
|
158
163
|
return card18.test(val) || (compatible ? card15.test(val) : false);
|
|
159
164
|
};
|
|
160
165
|
const isNumeric = (val) => {
|
|
@@ -190,10 +195,17 @@ const isValidFileType = (file, type) => {
|
|
|
190
195
|
return validate(type);
|
|
191
196
|
};
|
|
192
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
|
+
|
|
193
203
|
const hideMiddle = (str) => {
|
|
194
204
|
if (!isDef(str))
|
|
195
205
|
return "";
|
|
196
206
|
const len = str.length;
|
|
207
|
+
if (len === 0)
|
|
208
|
+
return "";
|
|
197
209
|
if (len === 1)
|
|
198
210
|
return "*";
|
|
199
211
|
if (len === 2)
|
|
@@ -216,6 +228,8 @@ const hideHead = (str, count) => {
|
|
|
216
228
|
return "";
|
|
217
229
|
const len = str.length;
|
|
218
230
|
const cnt = count !== void 0 ? Math.max(Math.min(len, count), 0) : len - 1;
|
|
231
|
+
if (len === 0)
|
|
232
|
+
return "";
|
|
219
233
|
if (len === 1)
|
|
220
234
|
return "*";
|
|
221
235
|
return "*".repeat(cnt) + str.substring(cnt);
|
|
@@ -225,6 +239,8 @@ const hideTail = (str, count) => {
|
|
|
225
239
|
return "";
|
|
226
240
|
const len = str.length;
|
|
227
241
|
const cnt = count !== void 0 ? Math.max(Math.min(len, count), 0) : len - 1;
|
|
242
|
+
if (len === 0)
|
|
243
|
+
return "";
|
|
228
244
|
if (len === 1)
|
|
229
245
|
return "*";
|
|
230
246
|
return str.substring(0, len - cnt) + "*".repeat(cnt);
|
|
@@ -300,14 +316,34 @@ const hideBankCard = (bankCard) => {
|
|
|
300
316
|
return "";
|
|
301
317
|
};
|
|
302
318
|
|
|
319
|
+
let __BASE_ATTACH_URL__;
|
|
320
|
+
function setBaseAttachUrl(baseUrl) {
|
|
321
|
+
__BASE_ATTACH_URL__ = baseUrl;
|
|
322
|
+
}
|
|
323
|
+
function getBaseAttachUrl() {
|
|
324
|
+
return __BASE_ATTACH_URL__;
|
|
325
|
+
}
|
|
326
|
+
|
|
303
327
|
const parseToJSON = (str, reviver2) => isJSONStr(str) ? JSON.parse(str, reviver2) : null;
|
|
304
328
|
const replacer = (_, value) => {
|
|
305
|
-
if (value
|
|
329
|
+
if (isMap(value)) {
|
|
306
330
|
return {
|
|
307
331
|
dataType: "Map",
|
|
308
332
|
value: [...value]
|
|
309
333
|
};
|
|
310
334
|
}
|
|
335
|
+
if (isSet(value)) {
|
|
336
|
+
return {
|
|
337
|
+
dataType: "Set",
|
|
338
|
+
value: [...value]
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
if (isDate(value)) {
|
|
342
|
+
return {
|
|
343
|
+
dataType: "Date",
|
|
344
|
+
value: value.toISOString()
|
|
345
|
+
};
|
|
346
|
+
}
|
|
311
347
|
return value;
|
|
312
348
|
};
|
|
313
349
|
const reviver = (_, value) => {
|
|
@@ -315,11 +351,33 @@ const reviver = (_, value) => {
|
|
|
315
351
|
if (value.dataType === "Map") {
|
|
316
352
|
return new Map(value.value);
|
|
317
353
|
}
|
|
354
|
+
if (value.dataType === "Set") {
|
|
355
|
+
return new Set(value.value);
|
|
356
|
+
}
|
|
357
|
+
if (value.dataType === "Date") {
|
|
358
|
+
return new Date(value.value);
|
|
359
|
+
}
|
|
318
360
|
}
|
|
319
361
|
return value;
|
|
320
362
|
};
|
|
321
363
|
const filterObj = (obj, keys, reverse = false) => Object.fromEntries(Object.entries(obj).filter(([key]) => reverse ? !keys.includes(key) : keys.includes(key)));
|
|
322
364
|
|
|
365
|
+
const toThousands = (num) => {
|
|
366
|
+
const arr = num.toString().split(".");
|
|
367
|
+
const integer = arr[0]?.replace(/(\d)(?=(?:\d{3})+$)/g, "$1,");
|
|
368
|
+
if (arr.length > 1)
|
|
369
|
+
return `${integer}.${arr[1]}`;
|
|
370
|
+
return integer;
|
|
371
|
+
};
|
|
372
|
+
const currencyFmt = (currency, maximumFractionDigits = 2) => {
|
|
373
|
+
if (!isDef(currency))
|
|
374
|
+
return "";
|
|
375
|
+
const num = Number(currency);
|
|
376
|
+
if (Number.isNaN(num))
|
|
377
|
+
return "";
|
|
378
|
+
return new Intl.NumberFormat("zh-CN", { style: "currency", currency: "CNY", maximumFractionDigits }).format(num);
|
|
379
|
+
};
|
|
380
|
+
|
|
323
381
|
dayjs.extend(localeData).locale("zh-cn", zhCN);
|
|
324
382
|
dayjs.extend(duration);
|
|
325
383
|
dayjs.extend(relativeTime);
|
|
@@ -353,42 +411,15 @@ const dateDuration = (start, end) => {
|
|
|
353
411
|
return dd.format(format);
|
|
354
412
|
};
|
|
355
413
|
|
|
356
|
-
let BASE_ATTACH_URL;
|
|
357
|
-
function setBaseAttachUrl(baseUrl) {
|
|
358
|
-
BASE_ATTACH_URL = baseUrl;
|
|
359
|
-
}
|
|
360
|
-
function getBaseAttachUrl() {
|
|
361
|
-
return BASE_ATTACH_URL;
|
|
362
|
-
}
|
|
363
|
-
if (import.meta.env?.VITE_BASE_ATTACH_URL) {
|
|
364
|
-
setBaseAttachUrl(import.meta.env.VITE_BASE_ATTACH_URL);
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
const toThousands = (num) => {
|
|
368
|
-
const arr = num.toString().split(".");
|
|
369
|
-
const integer = arr[0]?.replace(/(\d)(?=(?:\d{3})+$)/g, "$1,");
|
|
370
|
-
if (arr.length > 1)
|
|
371
|
-
return `${integer}.${arr[1]}`;
|
|
372
|
-
return integer;
|
|
373
|
-
};
|
|
374
|
-
const currencyFmt = (currency, maximumFractionDigits = 2) => {
|
|
375
|
-
if (!isDef(currency))
|
|
376
|
-
return "";
|
|
377
|
-
const num = Number(currency);
|
|
378
|
-
if (Number.isNaN(num))
|
|
379
|
-
return "";
|
|
380
|
-
return new Intl.NumberFormat("zh-CN", { style: "currency", currency: "CNY", maximumFractionDigits }).format(num);
|
|
381
|
-
};
|
|
382
|
-
|
|
383
414
|
const resUrl = (url) => {
|
|
384
415
|
if (!isDef(url))
|
|
385
416
|
return void 0;
|
|
386
|
-
if (/^https?:\/\//.test(url) ||
|
|
417
|
+
if (/^https?:\/\//.test(url) || url.startsWith("blob:") || url.startsWith("data:")) {
|
|
387
418
|
return url;
|
|
388
419
|
}
|
|
389
420
|
const baseUrl = getBaseAttachUrl();
|
|
390
421
|
if (!isDef(baseUrl)) {
|
|
391
|
-
throw new Error("\u8BF7\u5148\u8BBE\u7F6E
|
|
422
|
+
throw new Error("\u8BF7\u5148\u4F7F\u7528 setBaseAttachUrl \u8BBE\u7F6E\u6587\u4EF6\u57FA\u672C\u8DEF\u5F84");
|
|
392
423
|
}
|
|
393
424
|
return `${baseUrl}/${url}`.replace(/([^:]\/)\/+/g, "$1");
|
|
394
425
|
};
|
|
@@ -407,11 +438,11 @@ const recoverFile = (resource) => {
|
|
|
407
438
|
const attachFmt = (data) => {
|
|
408
439
|
let attachList = [];
|
|
409
440
|
const prototype = Object.prototype.toString.call(data);
|
|
410
|
-
if (!data)
|
|
441
|
+
if (!data) {
|
|
411
442
|
attachList = [];
|
|
412
|
-
else if (Array.isArray(data))
|
|
443
|
+
} else if (Array.isArray(data)) {
|
|
413
444
|
attachList = data.filter(Boolean);
|
|
414
|
-
else if (typeof data === "string") {
|
|
445
|
+
} else if (typeof data === "string") {
|
|
415
446
|
if (isArrStr(data)) {
|
|
416
447
|
attachList = (parseToJSON(data) || []).filter(Boolean);
|
|
417
448
|
} else {
|
|
@@ -422,11 +453,29 @@ const attachFmt = (data) => {
|
|
|
422
453
|
else
|
|
423
454
|
attachList = [{ id: data, name: "name", uri: data, group: "default" }];
|
|
424
455
|
}
|
|
425
|
-
} else if (prototype === "[object Object]")
|
|
456
|
+
} else if (prototype === "[object Object]") {
|
|
426
457
|
attachList = [data];
|
|
458
|
+
}
|
|
427
459
|
return attachList.map(recoverFile);
|
|
428
460
|
};
|
|
429
461
|
|
|
462
|
+
const isNumberArray = (arr) => arr.every((item) => typeof item === "number");
|
|
463
|
+
const isStringArray = (arr) => arr.every((item) => typeof item === "string");
|
|
464
|
+
const naturalSort = (arr) => {
|
|
465
|
+
if (isNumberArray(arr))
|
|
466
|
+
return [...arr].sort((a, b) => a - b);
|
|
467
|
+
if (isStringArray(arr)) {
|
|
468
|
+
return [...arr].sort((a, b) => a.localeCompare(b, "zh-CN")).sort((a, b) => a.length - b.length);
|
|
469
|
+
}
|
|
470
|
+
const newArr = [...arr].sort();
|
|
471
|
+
const validArr = newArr.filter((a) => isDef(a));
|
|
472
|
+
const invalidArr = newArr.filter((a) => !isDef(a));
|
|
473
|
+
return naturalSort(validArr).concat(invalidArr);
|
|
474
|
+
};
|
|
475
|
+
const naturalSortBy = (arr, key) => {
|
|
476
|
+
return [...arr].sort((a, b) => naturalSort([a[key], b[key]])[0] === a[key] ? -1 : 1);
|
|
477
|
+
};
|
|
478
|
+
|
|
430
479
|
const inBrowser = typeof window !== "undefined";
|
|
431
480
|
const waitTime = (time = 100) => {
|
|
432
481
|
return new Promise((resolve) => {
|
|
@@ -448,7 +497,8 @@ const getHostEnv = () => {
|
|
|
448
497
|
const ua = userAgent.toLowerCase();
|
|
449
498
|
const zlb = ua.includes("@zlb");
|
|
450
499
|
const zyd = ua.includes("saas");
|
|
451
|
-
const zzd = ua.includes("zhejiang") || ua.includes("tauruszjd");
|
|
500
|
+
const zzd = ua.includes("zhejiang") || ua.includes("tauruszjd") || ua.includes("dingtalk-win");
|
|
501
|
+
const dd = ua.includes("taurusapp");
|
|
452
502
|
const wx = ua.includes("micromessenger");
|
|
453
503
|
const zfb = ua.includes("alipay");
|
|
454
504
|
const mini = ua.includes("miniprogram");
|
|
@@ -456,6 +506,7 @@ const getHostEnv = () => {
|
|
|
456
506
|
zlb,
|
|
457
507
|
zyd,
|
|
458
508
|
zzd,
|
|
509
|
+
dd,
|
|
459
510
|
wx,
|
|
460
511
|
zfb,
|
|
461
512
|
mini
|
|
@@ -468,4 +519,4 @@ const getCodeOfHour = () => getCodeOfTime(`${dateFmt( new Date(), "YYYY-MM-DD HH
|
|
|
468
519
|
const getCodeOfDate = () => getCodeOfTime(dateFmt(/* @__PURE__ */ new Date(), "date"));
|
|
469
520
|
const getCodeOfMonth = () => getCodeOfTime(dateFmt(dayjs().endOf("month"), "date"));
|
|
470
521
|
|
|
471
|
-
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 };
|
|
522
|
+
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.
|
|
4
|
+
"version": "0.0.3-beta.10",
|
|
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.
|
|
40
|
+
"dayjs": "^1.11.13",
|
|
41
41
|
"detect-browser": "^5.3.0"
|
|
42
42
|
},
|
|
43
43
|
"scripts": {
|