@jhqn/utils-core 0.0.1-beta.23 → 0.0.1-beta.24
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 +96 -10
- package/dist/index.d.cts +738 -0
- package/dist/index.d.mts +738 -0
- package/dist/index.d.ts +139 -14
- package/dist/index.mjs +89 -12
- package/package.json +9 -5
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
/// <reference types="dayjs/
|
|
2
|
-
/// <reference types="dayjs/
|
|
3
|
-
/// <reference types="dayjs/
|
|
4
|
-
/// <reference types="dayjs/
|
|
5
|
-
/// <reference types="dayjs/
|
|
1
|
+
/// <reference types="dayjs/plugin/localeData" />
|
|
2
|
+
/// <reference types="dayjs/plugin/duration" />
|
|
3
|
+
/// <reference types="dayjs/plugin/relativeTime" />
|
|
4
|
+
/// <reference types="dayjs/plugin/customParseFormat" />
|
|
5
|
+
/// <reference types="dayjs/plugin/objectSupport" />
|
|
6
6
|
import * as detect_browser from 'detect-browser';
|
|
7
|
-
import { ConfigType } from 'dayjs
|
|
8
|
-
export { Dayjs, default as dayjs } from 'dayjs
|
|
7
|
+
import { ConfigType } from 'dayjs';
|
|
8
|
+
export { Dayjs, default as dayjs } from 'dayjs';
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* snake2Kebab
|
|
@@ -233,6 +233,10 @@ declare type PageResData<T = any> = Service.PageResponseData<T>;
|
|
|
233
233
|
* 分页请求结果
|
|
234
234
|
*/
|
|
235
235
|
declare type PageRes<T = any> = Service.PageResponse<T>;
|
|
236
|
+
/**
|
|
237
|
+
* 可能是数组
|
|
238
|
+
*/
|
|
239
|
+
declare type MaybeArray<T = any> = T | Array<T>;
|
|
236
240
|
|
|
237
241
|
/**
|
|
238
242
|
* 隐藏中间部分
|
|
@@ -512,18 +516,125 @@ declare const filterObj: (obj: Record<string, any>, keys: string[], reverse?: bo
|
|
|
512
516
|
|
|
513
517
|
/**
|
|
514
518
|
* 格式化时间
|
|
515
|
-
* @param {import('dayjs
|
|
519
|
+
* @param {import('dayjs').ConfigType} date 时间
|
|
516
520
|
* @param {string} format 格式
|
|
517
521
|
* @returns {string} 格式化后的时间
|
|
518
522
|
*/
|
|
519
523
|
declare const dateFmt: (date: ConfigType, format?: string | 'date' | 'datetime') => string;
|
|
520
524
|
/**
|
|
521
525
|
* 格式化时间间隔
|
|
522
|
-
* @param {import('dayjs
|
|
523
|
-
* @param {import('dayjs
|
|
526
|
+
* @param {import('dayjs').ConfigType} start 开始时间
|
|
527
|
+
* @param {import('dayjs').ConfigType} end 结束时间
|
|
524
528
|
*/
|
|
525
529
|
declare const dateDuration: (start: ConfigType, end: ConfigType) => string;
|
|
526
530
|
|
|
531
|
+
/**
|
|
532
|
+
* 设置文件基本路径
|
|
533
|
+
* @param key
|
|
534
|
+
* @param baseUrl
|
|
535
|
+
*/
|
|
536
|
+
declare function setBaseAttachUrl(baseUrl: string): void;
|
|
537
|
+
/**
|
|
538
|
+
* 获取文件基本路径
|
|
539
|
+
*/
|
|
540
|
+
declare function getBaseAttachUrl(): string;
|
|
541
|
+
|
|
542
|
+
/**
|
|
543
|
+
* 千位分隔符
|
|
544
|
+
* @param num
|
|
545
|
+
*/
|
|
546
|
+
declare const toThousands: (num: number) => string | undefined;
|
|
547
|
+
/**
|
|
548
|
+
* 货币格式化
|
|
549
|
+
* @param currency
|
|
550
|
+
* @param fraction
|
|
551
|
+
*/
|
|
552
|
+
declare const currencyFmt: (currency: NullOrUndefinable<number | bigint>, fraction?: number) => string;
|
|
553
|
+
|
|
554
|
+
type VantUploadFileStatus = '' | 'uploading' | 'done' | 'failed'
|
|
555
|
+
type AntdUploadFileStatus = 'removed' | 'uploading' | 'done' | 'error'
|
|
556
|
+
type ElementPlusUploadFileStatus = 'ready' | 'uploading' | 'success' | 'fail'
|
|
557
|
+
|
|
558
|
+
interface Resource {
|
|
559
|
+
/** origin */
|
|
560
|
+
group: string
|
|
561
|
+
id: string
|
|
562
|
+
name: string
|
|
563
|
+
uri: string
|
|
564
|
+
/** extend */
|
|
565
|
+
url?: string
|
|
566
|
+
size?: number
|
|
567
|
+
uid?: string
|
|
568
|
+
status?: VantUploadFileStatus | AntdUploadFileStatus | ElementPlusUploadFileStatus
|
|
569
|
+
/** vant */
|
|
570
|
+
objectUrl?: string
|
|
571
|
+
content?: string
|
|
572
|
+
isImage?: boolean
|
|
573
|
+
message?: string
|
|
574
|
+
deletable?: boolean
|
|
575
|
+
reupload?: boolean
|
|
576
|
+
/** antd */
|
|
577
|
+
fileName?: string
|
|
578
|
+
percent?: number
|
|
579
|
+
thumbUrl?: string
|
|
580
|
+
/** element-plus */
|
|
581
|
+
percentage?: number
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
/**
|
|
585
|
+
* 获取文件完整地址
|
|
586
|
+
* @param url
|
|
587
|
+
*/
|
|
588
|
+
declare const resUrl: (url: Undefinable<string>) => string | undefined;
|
|
589
|
+
/**
|
|
590
|
+
* 统一文件字段内容
|
|
591
|
+
* @param resource
|
|
592
|
+
*/
|
|
593
|
+
declare const recoverFile: (resource: Resource) => {
|
|
594
|
+
url: string | undefined;
|
|
595
|
+
content: string | undefined;
|
|
596
|
+
uri: string;
|
|
597
|
+
status: string;
|
|
598
|
+
percent: number;
|
|
599
|
+
group: string;
|
|
600
|
+
id: string;
|
|
601
|
+
name: string;
|
|
602
|
+
size?: number | undefined;
|
|
603
|
+
uid?: string | undefined;
|
|
604
|
+
objectUrl?: string | undefined;
|
|
605
|
+
isImage?: boolean | undefined;
|
|
606
|
+
message?: string | undefined;
|
|
607
|
+
deletable?: boolean | undefined;
|
|
608
|
+
reupload?: boolean | undefined;
|
|
609
|
+
fileName?: string | undefined;
|
|
610
|
+
thumbUrl?: string | undefined;
|
|
611
|
+
percentage?: number | undefined;
|
|
612
|
+
};
|
|
613
|
+
/**
|
|
614
|
+
* 附件格式
|
|
615
|
+
* @param data
|
|
616
|
+
*/
|
|
617
|
+
declare const attachFmt: (data: NullOrUndefinable<MaybeArray<Resource>> | string) => {
|
|
618
|
+
url: string | undefined;
|
|
619
|
+
content: string | undefined;
|
|
620
|
+
uri: string;
|
|
621
|
+
status: string;
|
|
622
|
+
percent: number;
|
|
623
|
+
group: string;
|
|
624
|
+
id: string;
|
|
625
|
+
name: string;
|
|
626
|
+
size?: number | undefined;
|
|
627
|
+
uid?: string | undefined;
|
|
628
|
+
objectUrl?: string | undefined;
|
|
629
|
+
isImage?: boolean | undefined;
|
|
630
|
+
message?: string | undefined;
|
|
631
|
+
deletable?: boolean | undefined;
|
|
632
|
+
reupload?: boolean | undefined;
|
|
633
|
+
fileName?: string | undefined;
|
|
634
|
+
thumbUrl?: string | undefined;
|
|
635
|
+
percentage?: number | undefined;
|
|
636
|
+
}[];
|
|
637
|
+
|
|
527
638
|
/**
|
|
528
639
|
* 是否有意义的值
|
|
529
640
|
* @param val
|
|
@@ -539,6 +650,11 @@ declare const isNumeric: (val: Numeric) => val is string;
|
|
|
539
650
|
* @param ext
|
|
540
651
|
*/
|
|
541
652
|
declare const checkImg: (ext?: string) => boolean;
|
|
653
|
+
/**
|
|
654
|
+
* 是否是空值/空数组/空对象 null undefined '' [] {} Set Map
|
|
655
|
+
* @param val
|
|
656
|
+
*/
|
|
657
|
+
declare const isEmpty: (val: NullOrUndefinable<Numeric | boolean | Record<any, any> | Array<any> | Set<any> | Map<any, any>>) => boolean;
|
|
542
658
|
/**
|
|
543
659
|
* 判断是否合法JSON字符串
|
|
544
660
|
* @param str
|
|
@@ -587,21 +703,30 @@ declare const isIdCard: (val: NullOrUndefinable<string>) => boolean;
|
|
|
587
703
|
* @param text
|
|
588
704
|
*/
|
|
589
705
|
declare const nonASCII: (text: string) => boolean;
|
|
706
|
+
/**
|
|
707
|
+
* 校验接口返回code
|
|
708
|
+
* @param res
|
|
709
|
+
* @param code
|
|
710
|
+
*/
|
|
711
|
+
declare const isValidResCode: (res: Res, code?: number) => boolean;
|
|
590
712
|
/**
|
|
591
713
|
* 校验接口返回内容
|
|
592
714
|
* @param res
|
|
715
|
+
* @param code
|
|
593
716
|
*/
|
|
594
|
-
declare const isValidRes: (res: Res) => boolean;
|
|
717
|
+
declare const isValidRes: (res: Res, code?: number) => boolean;
|
|
595
718
|
/**
|
|
596
719
|
* 校验接口数组返回内容
|
|
597
720
|
* @param res
|
|
721
|
+
* @param code
|
|
598
722
|
*/
|
|
599
|
-
declare const isValidArrRes: (res: Res<any[]
|
|
723
|
+
declare const isValidArrRes: (res: Res<any[]>, code?: number) => boolean;
|
|
600
724
|
/**
|
|
601
725
|
* 校验接口分页返回内容
|
|
602
726
|
* @param res
|
|
727
|
+
* @param code
|
|
603
728
|
*/
|
|
604
|
-
declare const isValidPageRes: (res: PageRes) => boolean;
|
|
729
|
+
declare const isValidPageRes: (res: PageRes, code?: number) => boolean;
|
|
605
730
|
/**
|
|
606
731
|
* 校验文件类型
|
|
607
732
|
* 1. XXX/YYY 2. .xxx 3. XXX/* 4.*
|
|
@@ -610,4 +735,4 @@ declare const isValidPageRes: (res: PageRes) => boolean;
|
|
|
610
735
|
*/
|
|
611
736
|
declare const isValidFileType: (file: File, type: string) => boolean;
|
|
612
737
|
|
|
613
|
-
export { NullOrUndefinable, Nullable, Numeric, PageRes, PageResData, Res, Undefinable, camel2Kebab, camel2Pascal, camel2Snake, camel2Title, checkImg, dateDuration, dateFmt, filterObj, fixiOSInputAutoZoomIn, getHostEnv, hideCardNo, hideEmail, hideFirstName, hideHead, hideMiddle, hideName, hidePhone, hideSurname, hideTail, hideThird, inBrowser, isArrStr, isDef, isEmail, isIdCard, isJSONStr, isNumeric, isPhone, isTel, isValidArrRes, isValidFileType, isValidKey, isValidPageRes, isValidRes, jClear, jDebug, jError, jGroup, jGroupC, jGroupE, jInfo, jLog, jTime, jTimeEnd, jWarn, kebab2Camel, kebab2Pascal, kebab2Snake, kebab2Title, nonASCII, parseToJSON, pascal2Camel, pascal2Kebab, pascal2Snake, pascal2Title, replacer, reviver, snake2Camel, snake2Kebab, snake2Pascal, snake2Title, title2Camel, title2Kebab, title2Pascal, title2Snake, userAgent, waitTime };
|
|
738
|
+
export { type NullOrUndefinable, type Nullable, type Numeric, type PageRes, type PageResData, type Res, type Undefinable, attachFmt, camel2Kebab, camel2Pascal, camel2Snake, camel2Title, checkImg, currencyFmt, dateDuration, dateFmt, filterObj, fixiOSInputAutoZoomIn, getBaseAttachUrl, getHostEnv, hideCardNo, hideEmail, hideFirstName, hideHead, hideMiddle, hideName, hidePhone, hideSurname, hideTail, hideThird, inBrowser, isArrStr, isDef, isEmail, isEmpty, isIdCard, isJSONStr, isNumeric, isPhone, isTel, isValidArrRes, isValidFileType, isValidKey, isValidPageRes, isValidRes, isValidResCode, jClear, jDebug, jError, jGroup, jGroupC, jGroupE, jInfo, jLog, 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 };
|
package/dist/index.mjs
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { detect } from 'detect-browser';
|
|
2
|
-
import dayjs from 'dayjs
|
|
3
|
-
export { default as dayjs } from 'dayjs
|
|
4
|
-
import localeData from 'dayjs/
|
|
5
|
-
import duration from 'dayjs/
|
|
6
|
-
import relativeTime from 'dayjs/
|
|
7
|
-
import customParseFormat from 'dayjs/
|
|
8
|
-
import objectSupport from 'dayjs/
|
|
9
|
-
import zhCN from 'dayjs/
|
|
2
|
+
import dayjs from 'dayjs';
|
|
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';
|
|
7
|
+
import customParseFormat from 'dayjs/plugin/customParseFormat';
|
|
8
|
+
import objectSupport from 'dayjs/plugin/objectSupport';
|
|
9
|
+
import zhCN from 'dayjs/locale/zh-cn';
|
|
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());
|
|
@@ -65,6 +65,9 @@ const isNumeric = (val) => typeof val === "number" || /^\d+(?:\.\d+)?$/.test(val
|
|
|
65
65
|
const checkImg = (ext) => {
|
|
66
66
|
return /\.(?:jpg|jpeg|png|gif)$/.test((ext || "").toLowerCase());
|
|
67
67
|
};
|
|
68
|
+
const isEmpty = (val) => {
|
|
69
|
+
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;
|
|
70
|
+
};
|
|
68
71
|
const isJSONStr = (str) => {
|
|
69
72
|
try {
|
|
70
73
|
JSON.parse(str);
|
|
@@ -105,9 +108,10 @@ const isIdCard = (val) => {
|
|
|
105
108
|
return card15.test(val) || card18.test(val);
|
|
106
109
|
};
|
|
107
110
|
const nonASCII = (text) => /[^\x20-\x7E]/.test(text);
|
|
108
|
-
const
|
|
109
|
-
const
|
|
110
|
-
const
|
|
111
|
+
const isValidResCode = (res, code = 200) => res.code === code;
|
|
112
|
+
const isValidRes = (res, code) => isValidResCode(res, code) && isDef(res.data);
|
|
113
|
+
const isValidArrRes = (res, code) => isValidResCode(res, code) && Array.isArray(res.data);
|
|
114
|
+
const isValidPageRes = (res, code) => isValidResCode(res, code) && isDef(res.data) && Array.isArray(res.data.list || res.data.records);
|
|
111
115
|
const isValidFileType = (file, type) => {
|
|
112
116
|
if (type === "*")
|
|
113
117
|
return true;
|
|
@@ -330,4 +334,77 @@ const dateDuration = (start, end) => {
|
|
|
330
334
|
return dd.format(format);
|
|
331
335
|
};
|
|
332
336
|
|
|
333
|
-
|
|
337
|
+
let BASE_ATTACH_URL;
|
|
338
|
+
function setBaseAttachUrl(baseUrl) {
|
|
339
|
+
BASE_ATTACH_URL = baseUrl;
|
|
340
|
+
}
|
|
341
|
+
function getBaseAttachUrl() {
|
|
342
|
+
return BASE_ATTACH_URL;
|
|
343
|
+
}
|
|
344
|
+
if (import.meta.env?.VITE_BASE_ATTACH_URL) {
|
|
345
|
+
setBaseAttachUrl(import.meta.env.VITE_BASE_ATTACH_URL);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const toThousands = (num) => {
|
|
349
|
+
const arr = num.toString().split(".");
|
|
350
|
+
const integer = arr[0]?.replace(/(\d)(?=(?:\d{3})+$)/g, "$1,");
|
|
351
|
+
if (arr.length > 1)
|
|
352
|
+
return `${integer}.${arr[1]}`;
|
|
353
|
+
return integer;
|
|
354
|
+
};
|
|
355
|
+
const currencyFmt = (currency, fraction = 0) => {
|
|
356
|
+
if (!isDef(currency))
|
|
357
|
+
return "";
|
|
358
|
+
return new Intl.NumberFormat("zh-CN", { style: "currency", currency: "CNY", maximumFractionDigits: fraction }).format(
|
|
359
|
+
currency
|
|
360
|
+
);
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
const resUrl = (url) => {
|
|
364
|
+
if (!isDef(url))
|
|
365
|
+
return void 0;
|
|
366
|
+
if (/^https?:\/\//.test(url) || /^blob:/.test(url) || /^data:/.test(url)) {
|
|
367
|
+
return url;
|
|
368
|
+
}
|
|
369
|
+
const baseUrl = getBaseAttachUrl();
|
|
370
|
+
if (!isDef(baseUrl)) {
|
|
371
|
+
throw new Error("\u8BF7\u5148\u8BBE\u7F6E import.meta\u200B.env.VITE_BASE_ATTACH_URL");
|
|
372
|
+
}
|
|
373
|
+
return `${baseUrl}/${url}`.replace(/([^:]\/)\/+/g, "$1");
|
|
374
|
+
};
|
|
375
|
+
const recoverFile = (resource) => {
|
|
376
|
+
const { uri, url, ...item } = resource;
|
|
377
|
+
const validUrl = uri || url;
|
|
378
|
+
return {
|
|
379
|
+
...item,
|
|
380
|
+
url: resUrl(validUrl),
|
|
381
|
+
content: resUrl(validUrl),
|
|
382
|
+
uri: validUrl || "",
|
|
383
|
+
status: "done",
|
|
384
|
+
percent: 100
|
|
385
|
+
};
|
|
386
|
+
};
|
|
387
|
+
const attachFmt = (data) => {
|
|
388
|
+
let attachList = [];
|
|
389
|
+
const prototype = Object.prototype.toString.call(data);
|
|
390
|
+
if (!data)
|
|
391
|
+
attachList = [];
|
|
392
|
+
else if (Array.isArray(data))
|
|
393
|
+
attachList = data.filter(Boolean);
|
|
394
|
+
else if (typeof data === "string") {
|
|
395
|
+
if (isArrStr(data)) {
|
|
396
|
+
attachList = (parseToJSON(data) || []).filter(Boolean);
|
|
397
|
+
} else {
|
|
398
|
+
const jsonData = parseToJSON(data);
|
|
399
|
+
const prototypeJson = Object.prototype.toString.call(jsonData);
|
|
400
|
+
if (prototypeJson === "[object Object]")
|
|
401
|
+
attachList = [jsonData].filter(Boolean);
|
|
402
|
+
else
|
|
403
|
+
attachList = [{ id: data, name: "name", uri: data, group: "default" }];
|
|
404
|
+
}
|
|
405
|
+
} else if (prototype === "[object Object]")
|
|
406
|
+
attachList = [data];
|
|
407
|
+
return attachList.map(recoverFile);
|
|
408
|
+
};
|
|
409
|
+
|
|
410
|
+
export { attachFmt, camel2Kebab, camel2Pascal, camel2Snake, camel2Title, checkImg, currencyFmt, dateDuration, dateFmt, filterObj, fixiOSInputAutoZoomIn, getBaseAttachUrl, getHostEnv, hideCardNo, hideEmail, hideFirstName, hideHead, hideMiddle, hideName, hidePhone, hideSurname, hideTail, hideThird, inBrowser, isArrStr, isDef, isEmail, isEmpty, isIdCard, isJSONStr, isNumeric, isPhone, isTel, isValidArrRes, isValidFileType, isValidKey, isValidPageRes, isValidRes, isValidResCode, jClear, jDebug, jError, jGroup, jGroupC, jGroupE, jInfo, jLog, 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 };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jhqn/utils-core",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.1-beta.
|
|
4
|
+
"version": "0.0.1-beta.24",
|
|
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",
|
|
@@ -20,9 +20,14 @@
|
|
|
20
20
|
"sideEffects": false,
|
|
21
21
|
"exports": {
|
|
22
22
|
".": {
|
|
23
|
-
"
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
"import": {
|
|
24
|
+
"types": "./dist/index.d.mts",
|
|
25
|
+
"default": "./dist/index.mjs"
|
|
26
|
+
},
|
|
27
|
+
"require": {
|
|
28
|
+
"types": "./dist/index.d.cts",
|
|
29
|
+
"default": "./dist/index.cjs"
|
|
30
|
+
}
|
|
26
31
|
}
|
|
27
32
|
},
|
|
28
33
|
"main": "dist/index.cjs",
|
|
@@ -37,7 +42,6 @@
|
|
|
37
42
|
},
|
|
38
43
|
"scripts": {
|
|
39
44
|
"test": "vitest",
|
|
40
|
-
"//build": "tsup",
|
|
41
45
|
"build": "unbuild",
|
|
42
46
|
"publint": "publint"
|
|
43
47
|
}
|