lyb-js 1.0.6 → 1.0.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/README.md +143 -144
- package/dist/Base/LibGetDataType/index.d.ts +10 -0
- package/dist/Base/LibGetDataType/index.js +15 -0
- package/dist/Base/LibJsGetDataType/index.d.ts +10 -0
- package/dist/Base/LibJsGetDataType/index.js +15 -0
- package/dist/Base/LibJsPromiseTimeout/index.d.ts +10 -0
- package/dist/Base/LibJsPromiseTimeout/index.js +40 -0
- package/dist/Base/LibPromiseTimeout/index.d.ts +10 -0
- package/dist/Base/LibPromiseTimeout/index.js +40 -0
- package/dist/Browser/LibColorConsole/index.d.ts +15 -0
- package/dist/Browser/LibColorConsole/index.js +38 -0
- package/dist/Browser/LibIsMobile/index.d.ts +6 -0
- package/dist/Browser/LibIsMobile/index.js +9 -0
- package/dist/Browser/LibIsPad/index.d.ts +6 -0
- package/dist/Browser/LibIsPad/index.js +15 -0
- package/dist/Browser/LibJsColorConsole/index.d.ts +15 -0
- package/dist/Browser/LibJsColorConsole/index.js +38 -0
- package/dist/Browser/LibJsIsMobile/index.d.ts +6 -0
- package/dist/Browser/LibJsIsMobile/index.js +9 -0
- package/dist/Browser/LibJsIsPad/index.d.ts +6 -0
- package/dist/Browser/LibJsIsPad/index.js +15 -0
- package/dist/Browser/LibJsPathParams/index.d.ts +6 -0
- package/dist/Browser/LibJsPathParams/index.js +16 -0
- package/dist/Browser/LibJsSetTitleIcon/index.d.ts +7 -0
- package/dist/Browser/LibJsSetTitleIcon/index.js +14 -0
- package/dist/Browser/LibJsTagTitleTip/index.d.ts +7 -0
- package/dist/Browser/LibJsTagTitleTip/index.js +24 -0
- package/dist/Browser/LibPathParams/index.d.ts +6 -0
- package/dist/Browser/LibPathParams/index.js +16 -0
- package/dist/Browser/LibSetTitleIcon/index.d.ts +7 -0
- package/dist/Browser/LibSetTitleIcon/index.js +14 -0
- package/dist/Browser/LibTagTitleTip/index.d.ts +7 -0
- package/dist/Browser/LibTagTitleTip/index.js +24 -0
- package/dist/Data/LibChunkArray/index.d.ts +9 -0
- package/dist/Data/LibChunkArray/index.js +15 -0
- package/dist/Data/LibDeepJSONParse/index.d.ts +6 -0
- package/dist/Data/LibDeepJSONParse/index.js +32 -0
- package/dist/Data/LibGroupArrayByKey/index.d.ts +10 -0
- package/dist/Data/LibGroupArrayByKey/index.js +20 -0
- package/dist/Data/LibJsChunkArray/index.d.ts +9 -0
- package/dist/Data/LibJsChunkArray/index.js +15 -0
- package/dist/Data/LibJsDeepJSONParse/index.d.ts +6 -0
- package/dist/Data/LibJsDeepJSONParse/index.js +32 -0
- package/dist/Data/LibJsGroupArrayByKey/index.d.ts +10 -0
- package/dist/Data/LibJsGroupArrayByKey/index.js +20 -0
- package/dist/Data/LibJsMatchEmail/index.d.ts +10 -0
- package/dist/Data/LibJsMatchEmail/index.js +16 -0
- package/dist/Data/LibJsShuffleArray/index.d.ts +7 -0
- package/dist/Data/LibJsShuffleArray/index.js +14 -0
- package/dist/Data/LibJsStepArray/index.d.ts +8 -0
- package/dist/Data/LibJsStepArray/index.js +14 -0
- package/dist/Data/LibMatchEmail/index.d.ts +10 -0
- package/dist/Data/LibMatchEmail/index.js +16 -0
- package/dist/Data/LibShuffleArray/index.d.ts +7 -0
- package/dist/Data/LibShuffleArray/index.js +14 -0
- package/dist/Data/LibStepArray/index.d.ts +8 -0
- package/dist/Data/LibStepArray/index.js +14 -0
- package/dist/File/LibDownloadImageLink/index.d.ts +7 -0
- package/dist/File/LibDownloadImageLink/index.js +17 -0
- package/dist/File/LibImageOptimizer/index.d.ts +32 -0
- package/dist/File/LibImageOptimizer/index.js +83 -0
- package/dist/File/LibJsDownloadImageLink/index.d.ts +7 -0
- package/dist/File/LibJsDownloadImageLink/index.js +17 -0
- package/dist/File/LibJsImageOptimizer/index.d.ts +31 -0
- package/dist/File/LibJsImageOptimizer/index.js +82 -0
- package/dist/File/LibJsSaveJson/index.d.ts +9 -0
- package/dist/File/LibJsSaveJson/index.js +17 -0
- package/dist/File/LibSaveJson/index.d.ts +9 -0
- package/dist/File/LibSaveJson/index.js +17 -0
- package/dist/Formatter/LibFormatterByte/index.d.ts +9 -0
- package/dist/Formatter/LibFormatterByte/index.js +17 -0
- package/dist/Formatter/LibJsFormatterByte/index.d.ts +9 -0
- package/dist/Formatter/LibJsFormatterByte/index.js +17 -0
- package/dist/Formatter/LibJsMaskPhoneNumber/index.d.ts +8 -0
- package/dist/Formatter/LibJsMaskPhoneNumber/index.js +11 -0
- package/dist/Formatter/LibJsNumComma/index.d.ts +9 -0
- package/dist/Formatter/LibJsNumComma/index.js +13 -0
- package/dist/Formatter/LibJsNumberUnit/index.d.ts +12 -0
- package/dist/Formatter/LibJsNumberUnit/index.js +23 -0
- package/dist/Formatter/LibJsSecondsFormatterChinese/index.d.ts +11 -0
- package/dist/Formatter/LibJsSecondsFormatterChinese/index.js +42 -0
- package/dist/Formatter/LibMaskPhoneNumber/index.d.ts +8 -0
- package/dist/Formatter/LibMaskPhoneNumber/index.js +11 -0
- package/dist/Formatter/LibNumComma/index.d.ts +9 -0
- package/dist/Formatter/LibNumComma/index.js +13 -0
- package/dist/Formatter/LibNumberUnit/index.d.ts +12 -0
- package/dist/Formatter/LibNumberUnit/index.js +23 -0
- package/dist/Formatter/LibSecondsFormatterChinese/index.d.ts +11 -0
- package/dist/Formatter/LibSecondsFormatterChinese/index.js +42 -0
- package/dist/Math/LibCalculateExpression/index.d.ts +9 -0
- package/dist/Math/LibCalculateExpression/index.js +107 -0
- package/dist/Math/LibConvertAngle/index.d.ts +12 -0
- package/dist/Math/LibConvertAngle/index.js +22 -0
- package/dist/Math/LibCoordsAngle/index.d.ts +15 -0
- package/dist/Math/LibCoordsAngle/index.js +24 -0
- package/dist/Math/LibCoordsDistance/index.d.ts +15 -0
- package/dist/Math/LibCoordsDistance/index.js +16 -0
- package/dist/Math/LibDecimal/index.d.ts +10 -0
- package/dist/Math/LibDecimal/index.js +25 -0
- package/dist/Math/LibJsCalculateExpression/index.d.ts +9 -0
- package/dist/Math/LibJsCalculateExpression/index.js +107 -0
- package/dist/Math/LibJsConvertAngle/index.d.ts +12 -0
- package/dist/Math/LibJsConvertAngle/index.js +22 -0
- package/dist/Math/LibJsCoordsAngle/index.d.ts +15 -0
- package/dist/Math/LibJsCoordsAngle/index.js +24 -0
- package/dist/Math/LibJsCoordsDistance/index.d.ts +15 -0
- package/dist/Math/LibJsCoordsDistance/index.js +16 -0
- package/dist/Math/LibJsDecimal/index.d.ts +10 -0
- package/dist/Math/LibJsDecimal/index.js +25 -0
- package/dist/Misc/LibJsRegFormValidate/index.d.ts +32 -0
- package/dist/Misc/LibJsRegFormValidate/index.js +34 -0
- package/dist/Misc/LibJsRetryRequest/index.d.ts +24 -0
- package/dist/Misc/LibJsRetryRequest/index.js +37 -0
- package/dist/Misc/LibRegFormValidate/index.d.ts +32 -0
- package/dist/Misc/LibRegFormValidate/index.js +34 -0
- package/dist/Misc/LibRetryRequest/index.d.ts +24 -0
- package/dist/Misc/LibRetryRequest/index.js +37 -0
- package/dist/Random/LibJsProbabilityResult/index.d.ts +8 -0
- package/dist/Random/LibJsProbabilityResult/index.js +8 -0
- package/dist/Random/LibJsRandom/index.d.ts +9 -0
- package/dist/Random/LibJsRandom/index.js +11 -0
- package/dist/Random/LibJsRandomColor/index.d.ts +7 -0
- package/dist/Random/LibJsRandomColor/index.js +12 -0
- package/dist/Random/LibJsUniqueRandomNumbers/index.d.ts +9 -0
- package/dist/Random/LibJsUniqueRandomNumbers/index.js +16 -0
- package/dist/Random/LibProbabilityResult/index.d.ts +8 -0
- package/dist/Random/LibProbabilityResult/index.js +8 -0
- package/dist/Random/LibRandom/index.d.ts +9 -0
- package/dist/Random/LibRandom/index.js +11 -0
- package/dist/Random/LibRandomColor/index.d.ts +7 -0
- package/dist/Random/LibRandomColor/index.js +12 -0
- package/dist/Random/LibUniqueRandomNumbers/index.d.ts +9 -0
- package/dist/Random/LibUniqueRandomNumbers/index.js +16 -0
- package/dist/Time/LibJsSameTimeCheck/index.d.ts +12 -0
- package/dist/Time/LibJsSameTimeCheck/index.js +24 -0
- package/dist/Time/LibJsTimeAgo/index.d.ts +9 -0
- package/dist/Time/LibJsTimeAgo/index.js +27 -0
- package/dist/Time/LibJsTimeGreeting/index.d.ts +17 -0
- package/dist/Time/LibJsTimeGreeting/index.js +23 -0
- package/dist/Time/LibSameTimeCheck/index.d.ts +12 -0
- package/dist/Time/LibSameTimeCheck/index.js +24 -0
- package/dist/Time/LibTimeAgo/index.d.ts +9 -0
- package/dist/Time/LibTimeAgo/index.js +27 -0
- package/dist/Time/LibTimeGreeting/index.d.ts +17 -0
- package/dist/Time/LibTimeGreeting/index.js +23 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/lib.d.ts +394 -0
- package/dist/lib.js +404 -0
- package/dist/libJs.d.ts +394 -0
- package/dist/libJs.js +404 -0
- package/package.json +1 -1
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** @description 递归将JSON字符串深度解析为对象
|
|
2
|
+
* @example
|
|
3
|
+
* const obj = libJsDeepJSONParse('{"a": 1, "b": "{\"c\": 2}"}');
|
|
4
|
+
* console.log(obj); //{ a: 1, b: { c: 2 } }
|
|
5
|
+
*/
|
|
6
|
+
export const libJsDeepJSONParse = (data) => {
|
|
7
|
+
//检查是否为字符串并尝试解析
|
|
8
|
+
if (typeof data === "string") {
|
|
9
|
+
try {
|
|
10
|
+
const parsed = JSON.parse(data);
|
|
11
|
+
//递归解析解析后的结果
|
|
12
|
+
return libJsDeepJSONParse(parsed);
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
//如果解析失败,返回原始字符串
|
|
16
|
+
return data;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
//如果是数组,递归处理每个元素
|
|
20
|
+
if (Array.isArray(data)) {
|
|
21
|
+
return data.map((item) => libJsDeepJSONParse(item));
|
|
22
|
+
}
|
|
23
|
+
//如果是对象,递归处理每个属性值
|
|
24
|
+
if (data !== null && typeof data === "object") {
|
|
25
|
+
return Object.keys(data).reduce((acc, key) => {
|
|
26
|
+
acc[key] = libJsDeepJSONParse(data[key]);
|
|
27
|
+
return acc;
|
|
28
|
+
}, {});
|
|
29
|
+
}
|
|
30
|
+
//其他情况返回原始值
|
|
31
|
+
return data;
|
|
32
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description 分类汇总,将数组对象按照指定键值整理成一个以键值为键名的对象
|
|
3
|
+
* @param arr 要分组的数组
|
|
4
|
+
* @param key 分组的键
|
|
5
|
+
* @returns 分组后的对象
|
|
6
|
+
* @example
|
|
7
|
+
* const grouped = libJsGroupArrayByKey([{ id: 1, name: 'A' }, { id: 2, name: 'B' }, { id: 1, name: 'C' }], 'id');
|
|
8
|
+
* console.log(grouped); //{ 1: [{ id: 1, name: 'A' }, { id: 1, name: 'C' }], 2: [{ id: 2, name: 'B' }] }
|
|
9
|
+
*/
|
|
10
|
+
export declare const libJsGroupArrayByKey: (arr: any[] | undefined, key: string) => any;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description 分类汇总,将数组对象按照指定键值整理成一个以键值为键名的对象
|
|
3
|
+
* @param arr 要分组的数组
|
|
4
|
+
* @param key 分组的键
|
|
5
|
+
* @returns 分组后的对象
|
|
6
|
+
* @example
|
|
7
|
+
* const grouped = libJsGroupArrayByKey([{ id: 1, name: 'A' }, { id: 2, name: 'B' }, { id: 1, name: 'C' }], 'id');
|
|
8
|
+
* console.log(grouped); //{ 1: [{ id: 1, name: 'A' }, { id: 1, name: 'C' }], 2: [{ id: 2, name: 'B' }] }
|
|
9
|
+
*/
|
|
10
|
+
export const libJsGroupArrayByKey = (arr = [], key) => {
|
|
11
|
+
return key
|
|
12
|
+
? arr.reduce((t, v) => {
|
|
13
|
+
if (!t[v[key]]) {
|
|
14
|
+
t[v[key]] = [];
|
|
15
|
+
}
|
|
16
|
+
t[v[key]].push(v);
|
|
17
|
+
return t;
|
|
18
|
+
}, {})
|
|
19
|
+
: {};
|
|
20
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description 匹配电子邮件,可用于实时输入时,自动补全常用邮箱后缀
|
|
3
|
+
* @param str 要匹配的字符串
|
|
4
|
+
* @param emailList 电子邮件后缀列表
|
|
5
|
+
* @returns 匹配结果数组
|
|
6
|
+
* @example
|
|
7
|
+
* const emails = libJsMatchEmail("user", ["@gmail.com", "@yahoo.com"]);
|
|
8
|
+
* console.log(emails); //["user@gmail.com", "user@yahoo.com"]
|
|
9
|
+
*/
|
|
10
|
+
export declare const libJsMatchEmail: (str: string, emailList: string[]) => string[];
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description 匹配电子邮件,可用于实时输入时,自动补全常用邮箱后缀
|
|
3
|
+
* @param str 要匹配的字符串
|
|
4
|
+
* @param emailList 电子邮件后缀列表
|
|
5
|
+
* @returns 匹配结果数组
|
|
6
|
+
* @example
|
|
7
|
+
* const emails = libJsMatchEmail("user", ["@gmail.com", "@yahoo.com"]);
|
|
8
|
+
* console.log(emails); //["user@gmail.com", "user@yahoo.com"]
|
|
9
|
+
*/
|
|
10
|
+
export const libJsMatchEmail = (str, emailList) => {
|
|
11
|
+
return str.includes("@")
|
|
12
|
+
? emailList
|
|
13
|
+
.filter((item) => item.includes(str.slice(str.indexOf("@"))))
|
|
14
|
+
.map((item) => (str.includes("@") ? str.split("@")[0] : str) + item)
|
|
15
|
+
: emailList.map((item) => str + item);
|
|
16
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** @description 将数组打乱顺序
|
|
2
|
+
* @param arr 需要乱序的数组
|
|
3
|
+
* @example
|
|
4
|
+
* const shuffled = libJsShuffleArray([1, 2, 3, 4, 5]);
|
|
5
|
+
* console.log(shuffled); //[3, 5, 2, 1, 4] (结果每次不同)
|
|
6
|
+
*/
|
|
7
|
+
export const libJsShuffleArray = (arr) => {
|
|
8
|
+
const newArr = [...arr];
|
|
9
|
+
for (let i = newArr.length - 1; i > 0; i--) {
|
|
10
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
11
|
+
[newArr[i], newArr[j]] = [newArr[j], newArr[i]];
|
|
12
|
+
}
|
|
13
|
+
return newArr;
|
|
14
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** @description 数组元素整体步数移动
|
|
2
|
+
* @param arr 移动的数组
|
|
3
|
+
* @param step 负数为向后移动,正数为向前移动
|
|
4
|
+
* @example
|
|
5
|
+
* const moved = libJsStepArray([1, 2, 3, 4, 5], 2);
|
|
6
|
+
* console.log(moved); //[4, 5, 1, 2, 3]
|
|
7
|
+
*/
|
|
8
|
+
export declare const libJsStepArray: <T>(arr: T[], step: number) => T[];
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** @description 数组元素整体步数移动
|
|
2
|
+
* @param arr 移动的数组
|
|
3
|
+
* @param step 负数为向后移动,正数为向前移动
|
|
4
|
+
* @example
|
|
5
|
+
* const moved = libJsStepArray([1, 2, 3, 4, 5], 2);
|
|
6
|
+
* console.log(moved); //[4, 5, 1, 2, 3]
|
|
7
|
+
*/
|
|
8
|
+
export const libJsStepArray = (arr, step) => {
|
|
9
|
+
const length = arr.length;
|
|
10
|
+
if (length === 0 || step % length === 0)
|
|
11
|
+
return arr;
|
|
12
|
+
const normalizedStep = ((step % length) + length) % length;
|
|
13
|
+
return arr.slice(-normalizedStep).concat(arr.slice(0, -normalizedStep));
|
|
14
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description 匹配电子邮件,可用于实时输入时,自动补全常用邮箱后缀
|
|
3
|
+
* @param str 要匹配的字符串
|
|
4
|
+
* @param emailList 电子邮件后缀列表
|
|
5
|
+
* @returns 匹配结果数组
|
|
6
|
+
* @example
|
|
7
|
+
* const emails = libMatchEmail("user", ["@gmail.com", "@yahoo.com"]);
|
|
8
|
+
* console.log(emails); //["user@gmail.com", "user@yahoo.com"]
|
|
9
|
+
*/
|
|
10
|
+
export declare const libMatchEmail: (str: string, emailList: string[]) => string[];
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description 匹配电子邮件,可用于实时输入时,自动补全常用邮箱后缀
|
|
3
|
+
* @param str 要匹配的字符串
|
|
4
|
+
* @param emailList 电子邮件后缀列表
|
|
5
|
+
* @returns 匹配结果数组
|
|
6
|
+
* @example
|
|
7
|
+
* const emails = libMatchEmail("user", ["@gmail.com", "@yahoo.com"]);
|
|
8
|
+
* console.log(emails); //["user@gmail.com", "user@yahoo.com"]
|
|
9
|
+
*/
|
|
10
|
+
export const libMatchEmail = (str, emailList) => {
|
|
11
|
+
return str.includes("@")
|
|
12
|
+
? emailList
|
|
13
|
+
.filter((item) => item.includes(str.slice(str.indexOf("@"))))
|
|
14
|
+
.map((item) => (str.includes("@") ? str.split("@")[0] : str) + item)
|
|
15
|
+
: emailList.map((item) => str + item);
|
|
16
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** @description 将数组打乱顺序
|
|
2
|
+
* @param arr 需要乱序的数组
|
|
3
|
+
* @example
|
|
4
|
+
* const shuffled = libShuffleArray([1, 2, 3, 4, 5]);
|
|
5
|
+
* console.log(shuffled); //[3, 5, 2, 1, 4] (结果每次不同)
|
|
6
|
+
*/
|
|
7
|
+
export const libShuffleArray = (arr) => {
|
|
8
|
+
const newArr = [...arr];
|
|
9
|
+
for (let i = newArr.length - 1; i > 0; i--) {
|
|
10
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
11
|
+
[newArr[i], newArr[j]] = [newArr[j], newArr[i]];
|
|
12
|
+
}
|
|
13
|
+
return newArr;
|
|
14
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** @description 数组元素整体步数移动
|
|
2
|
+
* @param arr 移动的数组
|
|
3
|
+
* @param step 负数为向后移动,正数为向前移动
|
|
4
|
+
* @example
|
|
5
|
+
* const moved = libStepArray([1, 2, 3, 4, 5], 2);
|
|
6
|
+
* console.log(moved); //[4, 5, 1, 2, 3]
|
|
7
|
+
*/
|
|
8
|
+
export const libStepArray = (arr, step) => {
|
|
9
|
+
const length = arr.length;
|
|
10
|
+
if (length === 0 || step % length === 0)
|
|
11
|
+
return arr;
|
|
12
|
+
const normalizedStep = ((step % length) + length) % length;
|
|
13
|
+
return arr.slice(-normalizedStep).concat(arr.slice(0, -normalizedStep));
|
|
14
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** @description 将链接图片下载到本地
|
|
2
|
+
* @param link 图片链接
|
|
3
|
+
* @param name 图片名称
|
|
4
|
+
* @example
|
|
5
|
+
* libDownloadImageLink("https://example.com/image.jpg", "图片.jpg");
|
|
6
|
+
*/
|
|
7
|
+
export const libDownloadImageLink = (link, name) => {
|
|
8
|
+
fetch(link)
|
|
9
|
+
.then((res) => res.blob())
|
|
10
|
+
.then((blob) => {
|
|
11
|
+
const a = document.createElement("a");
|
|
12
|
+
const url = window.URL.createObjectURL(blob);
|
|
13
|
+
a.href = url;
|
|
14
|
+
a.download = name;
|
|
15
|
+
a.click();
|
|
16
|
+
});
|
|
17
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** @description 图片压缩参数 */
|
|
2
|
+
export type LibImageOptimizerOptionsParams = {
|
|
3
|
+
file?: File;
|
|
4
|
+
/** 压缩尺寸 */
|
|
5
|
+
width?: number;
|
|
6
|
+
/** 压缩率 */
|
|
7
|
+
ratio?: number;
|
|
8
|
+
/** 超过多大进行压缩 */
|
|
9
|
+
maxSize?: number;
|
|
10
|
+
/** 成功回调 */
|
|
11
|
+
success: (formData: FormData, file: File, url: string) => void;
|
|
12
|
+
/** 失败回调 */
|
|
13
|
+
fail?: (error: ErrorEvent) => void;
|
|
14
|
+
};
|
|
15
|
+
/** @description 图片压缩,支持png压缩,保留透明背景
|
|
16
|
+
* @param obj 压缩参数
|
|
17
|
+
* @example
|
|
18
|
+
* //图片压缩使用示例
|
|
19
|
+
* libImageOptimizerOptionsParams({
|
|
20
|
+
* file: myFile,
|
|
21
|
+
* ratio: 0.8,
|
|
22
|
+
* width: 800,
|
|
23
|
+
* maxSize: 1024,
|
|
24
|
+
* success: (formData, file, url) => {
|
|
25
|
+
* console.log('压缩成功', formData, file, url);
|
|
26
|
+
* },
|
|
27
|
+
* fail: (error) => {
|
|
28
|
+
* console.error('压缩失败', error);
|
|
29
|
+
* }
|
|
30
|
+
* });
|
|
31
|
+
*/
|
|
32
|
+
export declare const libImageOptimizer: (obj: LibImageOptimizerOptionsParams) => void;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/** @description 图片压缩,支持png压缩,保留透明背景
|
|
2
|
+
* @param obj 压缩参数
|
|
3
|
+
* @example
|
|
4
|
+
* //图片压缩使用示例
|
|
5
|
+
* libImageOptimizerOptionsParams({
|
|
6
|
+
* file: myFile,
|
|
7
|
+
* ratio: 0.8,
|
|
8
|
+
* width: 800,
|
|
9
|
+
* maxSize: 1024,
|
|
10
|
+
* success: (formData, file, url) => {
|
|
11
|
+
* console.log('压缩成功', formData, file, url);
|
|
12
|
+
* },
|
|
13
|
+
* fail: (error) => {
|
|
14
|
+
* console.error('压缩失败', error);
|
|
15
|
+
* }
|
|
16
|
+
* });
|
|
17
|
+
*/
|
|
18
|
+
export const libImageOptimizer = (obj) => {
|
|
19
|
+
const canvas = document.createElement("canvas");
|
|
20
|
+
canvas.classList.add("imageOptimizer");
|
|
21
|
+
document.body.appendChild(canvas);
|
|
22
|
+
const files = obj.file;
|
|
23
|
+
if (!files)
|
|
24
|
+
return;
|
|
25
|
+
const name = files.name;
|
|
26
|
+
const extension = name.split(".").pop().toLowerCase();
|
|
27
|
+
const ratio = obj.ratio || 1;
|
|
28
|
+
const maxSize = obj.maxSize || 1024;
|
|
29
|
+
const width = obj.width || 10000;
|
|
30
|
+
const mimeType = files.type;
|
|
31
|
+
function dataURLtoFile(dataURL, filename) {
|
|
32
|
+
const arr = dataURL.split(",");
|
|
33
|
+
const mime = arr[0].match(/:(.*?);/)[1];
|
|
34
|
+
const bstr = window.atob(arr[1]);
|
|
35
|
+
const n = bstr.length;
|
|
36
|
+
const u8arr = new Uint8Array(n);
|
|
37
|
+
for (let i = 0; i < n; i++) {
|
|
38
|
+
u8arr[i] = bstr.charCodeAt(i);
|
|
39
|
+
}
|
|
40
|
+
return new File([u8arr], filename, { type: mime });
|
|
41
|
+
}
|
|
42
|
+
function formData(file) {
|
|
43
|
+
const data = new FormData();
|
|
44
|
+
data.append("file", file);
|
|
45
|
+
return data;
|
|
46
|
+
}
|
|
47
|
+
const reader = new FileReader();
|
|
48
|
+
reader.readAsDataURL(files);
|
|
49
|
+
reader.onload = (e) => {
|
|
50
|
+
const result = e.target.result;
|
|
51
|
+
if (e.total / 1024 > maxSize) {
|
|
52
|
+
const image = new Image();
|
|
53
|
+
image.src = result;
|
|
54
|
+
image.onload = () => {
|
|
55
|
+
const context = canvas.getContext("2d");
|
|
56
|
+
const scale = width / image.width;
|
|
57
|
+
if (scale < 1) {
|
|
58
|
+
canvas.width = image.width * scale;
|
|
59
|
+
canvas.height = image.height * scale;
|
|
60
|
+
context.drawImage(image, 0, 0, image.width * scale, image.height * scale);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
canvas.width = image.width;
|
|
64
|
+
canvas.height = image.height;
|
|
65
|
+
context.drawImage(image, 0, 0, image.width, image.height);
|
|
66
|
+
}
|
|
67
|
+
const dataUrl = canvas.toDataURL(mimeType, ratio);
|
|
68
|
+
const newName = `${name.split(".")[0]}.${extension}`;
|
|
69
|
+
const file = dataURLtoFile(dataUrl, newName);
|
|
70
|
+
obj.success(formData(file), file, dataUrl);
|
|
71
|
+
canvas.remove();
|
|
72
|
+
};
|
|
73
|
+
image.onerror = obj.fail;
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
const newName = `${name.split(".")[0]}.${extension}`;
|
|
77
|
+
const file = dataURLtoFile(result, newName);
|
|
78
|
+
obj.success(formData(file), file, result);
|
|
79
|
+
canvas.remove();
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
reader.onerror = obj.fail;
|
|
83
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** @description 将链接图片下载到本地
|
|
2
|
+
* @param link 图片链接
|
|
3
|
+
* @param name 图片名称
|
|
4
|
+
* @example
|
|
5
|
+
* libJsDownloadImageLink("https://example.com/image.jpg", "图片.jpg");
|
|
6
|
+
*/
|
|
7
|
+
export const libJsDownloadImageLink = (link, name) => {
|
|
8
|
+
fetch(link)
|
|
9
|
+
.then((res) => res.blob())
|
|
10
|
+
.then((blob) => {
|
|
11
|
+
const a = document.createElement("a");
|
|
12
|
+
const url = window.URL.createObjectURL(blob);
|
|
13
|
+
a.href = url;
|
|
14
|
+
a.download = name;
|
|
15
|
+
a.click();
|
|
16
|
+
});
|
|
17
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** @description 图片压缩参数 */
|
|
2
|
+
export type LibJsImageOptimizerOptionsParams = {
|
|
3
|
+
file?: File;
|
|
4
|
+
/** 压缩尺寸 */
|
|
5
|
+
width?: number;
|
|
6
|
+
/** 压缩率 */
|
|
7
|
+
ratio?: number;
|
|
8
|
+
/** 超过多大进行压缩 */
|
|
9
|
+
maxSize?: number;
|
|
10
|
+
/** 成功回调 */
|
|
11
|
+
success: (formData: FormData, file: File, url: string) => void;
|
|
12
|
+
/** 失败回调 */
|
|
13
|
+
fail?: (error: ErrorEvent) => void;
|
|
14
|
+
};
|
|
15
|
+
/** @description 图片压缩,支持png压缩,保留透明背景
|
|
16
|
+
* @param obj 压缩参数
|
|
17
|
+
* @example
|
|
18
|
+
* libJsImageOptimizerOptionsParams({
|
|
19
|
+
* file: myFile,
|
|
20
|
+
* ratio: 0.8,
|
|
21
|
+
* width: 800,
|
|
22
|
+
* maxSize: 1024,
|
|
23
|
+
* success: (formData, file, url) => {
|
|
24
|
+
* console.log('压缩成功', formData, file, url);
|
|
25
|
+
* },
|
|
26
|
+
* fail: (error) => {
|
|
27
|
+
* console.error('压缩失败', error);
|
|
28
|
+
* }
|
|
29
|
+
* });
|
|
30
|
+
*/
|
|
31
|
+
export declare const libJsImageOptimizer: (obj: LibJsImageOptimizerOptionsParams) => void;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/** @description 图片压缩,支持png压缩,保留透明背景
|
|
2
|
+
* @param obj 压缩参数
|
|
3
|
+
* @example
|
|
4
|
+
* libJsImageOptimizerOptionsParams({
|
|
5
|
+
* file: myFile,
|
|
6
|
+
* ratio: 0.8,
|
|
7
|
+
* width: 800,
|
|
8
|
+
* maxSize: 1024,
|
|
9
|
+
* success: (formData, file, url) => {
|
|
10
|
+
* console.log('压缩成功', formData, file, url);
|
|
11
|
+
* },
|
|
12
|
+
* fail: (error) => {
|
|
13
|
+
* console.error('压缩失败', error);
|
|
14
|
+
* }
|
|
15
|
+
* });
|
|
16
|
+
*/
|
|
17
|
+
export const libJsImageOptimizer = (obj) => {
|
|
18
|
+
const canvas = document.createElement("canvas");
|
|
19
|
+
canvas.classList.add("imageOptimizer");
|
|
20
|
+
document.body.appendChild(canvas);
|
|
21
|
+
const files = obj.file;
|
|
22
|
+
if (!files)
|
|
23
|
+
return;
|
|
24
|
+
const name = files.name;
|
|
25
|
+
const extension = name.split(".").pop().toLowerCase();
|
|
26
|
+
const ratio = obj.ratio || 1;
|
|
27
|
+
const maxSize = obj.maxSize || 1024;
|
|
28
|
+
const width = obj.width || 10000;
|
|
29
|
+
const mimeType = files.type;
|
|
30
|
+
function dataURLtoFile(dataURL, filename) {
|
|
31
|
+
const arr = dataURL.split(",");
|
|
32
|
+
const mime = arr[0].match(/:(.*?);/)[1];
|
|
33
|
+
const bstr = window.atob(arr[1]);
|
|
34
|
+
const n = bstr.length;
|
|
35
|
+
const u8arr = new Uint8Array(n);
|
|
36
|
+
for (let i = 0; i < n; i++) {
|
|
37
|
+
u8arr[i] = bstr.charCodeAt(i);
|
|
38
|
+
}
|
|
39
|
+
return new File([u8arr], filename, { type: mime });
|
|
40
|
+
}
|
|
41
|
+
function formData(file) {
|
|
42
|
+
const data = new FormData();
|
|
43
|
+
data.append("file", file);
|
|
44
|
+
return data;
|
|
45
|
+
}
|
|
46
|
+
const reader = new FileReader();
|
|
47
|
+
reader.readAsDataURL(files);
|
|
48
|
+
reader.onload = (e) => {
|
|
49
|
+
const result = e.target.result;
|
|
50
|
+
if (e.total / 1024 > maxSize) {
|
|
51
|
+
const image = new Image();
|
|
52
|
+
image.src = result;
|
|
53
|
+
image.onload = () => {
|
|
54
|
+
const context = canvas.getContext("2d");
|
|
55
|
+
const scale = width / image.width;
|
|
56
|
+
if (scale < 1) {
|
|
57
|
+
canvas.width = image.width * scale;
|
|
58
|
+
canvas.height = image.height * scale;
|
|
59
|
+
context.drawImage(image, 0, 0, image.width * scale, image.height * scale);
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
canvas.width = image.width;
|
|
63
|
+
canvas.height = image.height;
|
|
64
|
+
context.drawImage(image, 0, 0, image.width, image.height);
|
|
65
|
+
}
|
|
66
|
+
const dataUrl = canvas.toDataURL(mimeType, ratio);
|
|
67
|
+
const newName = `${name.split(".")[0]}.${extension}`;
|
|
68
|
+
const file = dataURLtoFile(dataUrl, newName);
|
|
69
|
+
obj.success(formData(file), file, dataUrl);
|
|
70
|
+
canvas.remove();
|
|
71
|
+
};
|
|
72
|
+
image.onerror = obj.fail;
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
const newName = `${name.split(".")[0]}.${extension}`;
|
|
76
|
+
const file = dataURLtoFile(result, newName);
|
|
77
|
+
obj.success(formData(file), file, result);
|
|
78
|
+
canvas.remove();
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
reader.onerror = obj.fail;
|
|
82
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description 保存文件到本地
|
|
3
|
+
* @param data 要保存的数据
|
|
4
|
+
* @param name 文件名
|
|
5
|
+
* @example
|
|
6
|
+
* libJsSaveJson(JSON.stringify({ key: "value" }), "example.json");
|
|
7
|
+
* libJsSaveJson("Hellow World!", "example.txt");
|
|
8
|
+
*/
|
|
9
|
+
export declare const libJsSaveJson: (data: BlobPart, name: string) => void;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description 保存文件到本地
|
|
3
|
+
* @param data 要保存的数据
|
|
4
|
+
* @param name 文件名
|
|
5
|
+
* @example
|
|
6
|
+
* libJsSaveJson(JSON.stringify({ key: "value" }), "example.json");
|
|
7
|
+
* libJsSaveJson("Hellow World!", "example.txt");
|
|
8
|
+
*/
|
|
9
|
+
export const libJsSaveJson = (data, name) => {
|
|
10
|
+
const urlObject = window.URL || window.webkitURL;
|
|
11
|
+
const exportBlob = new Blob([data]);
|
|
12
|
+
const saveLink = document.createElement("a");
|
|
13
|
+
saveLink.href = urlObject.createObjectURL(exportBlob);
|
|
14
|
+
saveLink.download = name;
|
|
15
|
+
saveLink.click();
|
|
16
|
+
urlObject.revokeObjectURL(saveLink.href);
|
|
17
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description 保存文件到本地
|
|
3
|
+
* @param data 要保存的数据
|
|
4
|
+
* @param name 文件名
|
|
5
|
+
* @example
|
|
6
|
+
* libSaveJson(JSON.stringify({ key: "value" }), "example.json");
|
|
7
|
+
* libSaveJson("Hellow World!", "example.txt");
|
|
8
|
+
*/
|
|
9
|
+
export declare const libSaveJson: (data: BlobPart, name: string) => void;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description 保存文件到本地
|
|
3
|
+
* @param data 要保存的数据
|
|
4
|
+
* @param name 文件名
|
|
5
|
+
* @example
|
|
6
|
+
* libSaveJson(JSON.stringify({ key: "value" }), "example.json");
|
|
7
|
+
* libSaveJson("Hellow World!", "example.txt");
|
|
8
|
+
*/
|
|
9
|
+
export const libSaveJson = (data, name) => {
|
|
10
|
+
const urlObject = window.URL || window.webkitURL;
|
|
11
|
+
const exportBlob = new Blob([data]);
|
|
12
|
+
const saveLink = document.createElement("a");
|
|
13
|
+
saveLink.href = urlObject.createObjectURL(exportBlob);
|
|
14
|
+
saveLink.download = name;
|
|
15
|
+
saveLink.click();
|
|
16
|
+
urlObject.revokeObjectURL(saveLink.href);
|
|
17
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description 格式化字节大小
|
|
3
|
+
* @param bytes 字节数
|
|
4
|
+
* @returns ['大小', '单位', '大小及单位']
|
|
5
|
+
* @example
|
|
6
|
+
* const [size, unit, formatted] = libFormatterByte(2048);
|
|
7
|
+
* console.log(size, unit, formatted); //2.00 KB 2.00 KB
|
|
8
|
+
*/
|
|
9
|
+
export declare const libFormatterByte: (bytes: number) => (string | number)[];
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description 格式化字节大小
|
|
3
|
+
* @param bytes 字节数
|
|
4
|
+
* @returns ['大小', '单位', '大小及单位']
|
|
5
|
+
* @example
|
|
6
|
+
* const [size, unit, formatted] = libFormatterByte(2048);
|
|
7
|
+
* console.log(size, unit, formatted); //2.00 KB 2.00 KB
|
|
8
|
+
*/
|
|
9
|
+
export const libFormatterByte = (bytes) => {
|
|
10
|
+
if (bytes <= 0)
|
|
11
|
+
return [0, "B", "0 B"];
|
|
12
|
+
const k = 1024;
|
|
13
|
+
const sizes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
|
|
14
|
+
const i = Math.min(Math.floor(Math.log(bytes) / Math.log(k)), sizes.length - 1);
|
|
15
|
+
const size = (bytes / k ** i).toFixed(2);
|
|
16
|
+
return [size, sizes[i], `${size} ${sizes[i]}`];
|
|
17
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description 格式化字节大小
|
|
3
|
+
* @param bytes 字节数
|
|
4
|
+
* @returns ['大小', '单位', '大小及单位']
|
|
5
|
+
* @example
|
|
6
|
+
* const [size, unit, formatted] = libJsFormatterByte(2048);
|
|
7
|
+
* console.log(size, unit, formatted); //2.00 KB 2.00 KB
|
|
8
|
+
*/
|
|
9
|
+
export declare const libJsFormatterByte: (bytes: number) => (string | number)[];
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description 格式化字节大小
|
|
3
|
+
* @param bytes 字节数
|
|
4
|
+
* @returns ['大小', '单位', '大小及单位']
|
|
5
|
+
* @example
|
|
6
|
+
* const [size, unit, formatted] = libJsFormatterByte(2048);
|
|
7
|
+
* console.log(size, unit, formatted); //2.00 KB 2.00 KB
|
|
8
|
+
*/
|
|
9
|
+
export const libJsFormatterByte = (bytes) => {
|
|
10
|
+
if (bytes <= 0)
|
|
11
|
+
return [0, "B", "0 B"];
|
|
12
|
+
const k = 1024;
|
|
13
|
+
const sizes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
|
|
14
|
+
const i = Math.min(Math.floor(Math.log(bytes) / Math.log(k)), sizes.length - 1);
|
|
15
|
+
const size = (bytes / k ** i).toFixed(2);
|
|
16
|
+
return [size, sizes[i], `${size} ${sizes[i]}`];
|
|
17
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description 隐藏手机号码中间的四位数字
|
|
3
|
+
* @param mobile 需要处理的手机号码
|
|
4
|
+
* @example
|
|
5
|
+
* const masked = libJsMaskPhoneNumber("13812345678");
|
|
6
|
+
* console.log(masked); //138****5678
|
|
7
|
+
*/
|
|
8
|
+
export const libJsMaskPhoneNumber = (mobile) => {
|
|
9
|
+
const m = mobile.toString();
|
|
10
|
+
return m.replace(/^(\d{3})\d{4}(\d{4})$/, "$1****$2");
|
|
11
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description 数字每三位添加逗号
|
|
3
|
+
* @param num 需要格式化的数字
|
|
4
|
+
* @param reserve 保留小数位数
|
|
5
|
+
* @example
|
|
6
|
+
* const formatted = libJsNumComma(1234567.89);
|
|
7
|
+
* console.log(formatted); //1,234,567.89
|
|
8
|
+
*/
|
|
9
|
+
export declare const libJsNumComma: (num: number, reserve?: number) => string;
|