lyb-js 1.0.1

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.
Files changed (89) hide show
  1. package/README.md +34 -0
  2. package/dist/Base/LibGetDataType/index.d.ts +10 -0
  3. package/dist/Base/LibGetDataType/index.js +15 -0
  4. package/dist/Base/LibJsGetDataType/index.d.ts +7 -0
  5. package/dist/Base/LibJsGetDataType/index.js +11 -0
  6. package/dist/Base/LibPromiseTimeout/index.d.ts +10 -0
  7. package/dist/Base/LibPromiseTimeout/index.js +40 -0
  8. package/dist/Browser/LibColorConsole/index.d.ts +15 -0
  9. package/dist/Browser/LibColorConsole/index.js +38 -0
  10. package/dist/Browser/LibIsMobile/index.d.ts +6 -0
  11. package/dist/Browser/LibIsMobile/index.js +9 -0
  12. package/dist/Browser/LibIsPad/index.d.ts +6 -0
  13. package/dist/Browser/LibIsPad/index.js +15 -0
  14. package/dist/Browser/LibPathParams/index.d.ts +6 -0
  15. package/dist/Browser/LibPathParams/index.js +16 -0
  16. package/dist/Browser/LibSetTitleIcon/index.d.ts +7 -0
  17. package/dist/Browser/LibSetTitleIcon/index.js +14 -0
  18. package/dist/Browser/LibTagTitleTip/index.d.ts +7 -0
  19. package/dist/Browser/LibTagTitleTip/index.js +24 -0
  20. package/dist/Data/LibChunkArray/index.d.ts +9 -0
  21. package/dist/Data/LibChunkArray/index.js +15 -0
  22. package/dist/Data/LibDeepJSONParse/index.d.ts +6 -0
  23. package/dist/Data/LibDeepJSONParse/index.js +32 -0
  24. package/dist/Data/LibGroupArrayByKey/index.d.ts +10 -0
  25. package/dist/Data/LibGroupArrayByKey/index.js +20 -0
  26. package/dist/Data/LibMatchEmail/index.d.ts +10 -0
  27. package/dist/Data/LibMatchEmail/index.js +16 -0
  28. package/dist/Data/LibSearch/index.d.ts +8 -0
  29. package/dist/Data/LibSearch/index.js +45 -0
  30. package/dist/Data/LibShuffleArray/index.d.ts +7 -0
  31. package/dist/Data/LibShuffleArray/index.js +14 -0
  32. package/dist/Data/LibStepArray/index.d.ts +8 -0
  33. package/dist/Data/LibStepArray/index.js +14 -0
  34. package/dist/File/LibDownloadImageLink/index.d.ts +7 -0
  35. package/dist/File/LibDownloadImageLink/index.js +17 -0
  36. package/dist/File/LibFileSize/index.d.ts +5 -0
  37. package/dist/File/LibFileSize/index.js +11 -0
  38. package/dist/File/LibImageOptimizer/index.d.ts +32 -0
  39. package/dist/File/LibImageOptimizer/index.js +83 -0
  40. package/dist/File/LibImageOptimizerOptions/index.d.ts +18 -0
  41. package/dist/File/LibImageOptimizerOptions/index.js +69 -0
  42. package/dist/File/LibSaveExcel/index.d.ts +11 -0
  43. package/dist/File/LibSaveExcel/index.js +17 -0
  44. package/dist/File/LibSaveJson/index.d.ts +8 -0
  45. package/dist/File/LibSaveJson/index.js +16 -0
  46. package/dist/Formatter/LibFormatterByte/index.d.ts +9 -0
  47. package/dist/Formatter/LibFormatterByte/index.js +17 -0
  48. package/dist/Formatter/LibMaskPhoneNumber/index.d.ts +8 -0
  49. package/dist/Formatter/LibMaskPhoneNumber/index.js +11 -0
  50. package/dist/Formatter/LibNumComma/index.d.ts +9 -0
  51. package/dist/Formatter/LibNumComma/index.js +13 -0
  52. package/dist/Formatter/LibNumberUnit/index.d.ts +12 -0
  53. package/dist/Formatter/LibNumberUnit/index.js +23 -0
  54. package/dist/Math/LibCalculateExpression/index.d.ts +10 -0
  55. package/dist/Math/LibCalculateExpression/index.js +108 -0
  56. package/dist/Math/LibConvertAngle/index.d.ts +12 -0
  57. package/dist/Math/LibConvertAngle/index.js +22 -0
  58. package/dist/Math/LibCoordsAngle/index.d.ts +15 -0
  59. package/dist/Math/LibCoordsAngle/index.js +24 -0
  60. package/dist/Math/LibCoordsDistance/index.d.ts +15 -0
  61. package/dist/Math/LibCoordsDistance/index.js +16 -0
  62. package/dist/Math/LibDecimal/index.d.ts +11 -0
  63. package/dist/Math/LibDecimal/index.js +26 -0
  64. package/dist/Misc/LibRegFormValidate/index.d.ts +32 -0
  65. package/dist/Misc/LibRegFormValidate/index.js +34 -0
  66. package/dist/Misc/LibRetryRequest/index.d.ts +24 -0
  67. package/dist/Misc/LibRetryRequest/index.js +37 -0
  68. package/dist/Random/LibProbabilityResult/index.d.ts +8 -0
  69. package/dist/Random/LibProbabilityResult/index.js +8 -0
  70. package/dist/Random/LibRandom/index.d.ts +10 -0
  71. package/dist/Random/LibRandom/index.js +12 -0
  72. package/dist/Random/LibRandomColor/index.d.ts +7 -0
  73. package/dist/Random/LibRandomColor/index.js +12 -0
  74. package/dist/Random/LibUniqueRandomNumbers/index.d.ts +9 -0
  75. package/dist/Random/LibUniqueRandomNumbers/index.js +16 -0
  76. package/dist/Time/LibSameTimeCheck/index.d.ts +8 -0
  77. package/dist/Time/LibSameTimeCheck/index.js +20 -0
  78. package/dist/Time/LibSecondsFormatterChinese/index.d.ts +11 -0
  79. package/dist/Time/LibSecondsFormatterChinese/index.js +42 -0
  80. package/dist/Time/LibTimeAgo/index.d.ts +9 -0
  81. package/dist/Time/LibTimeAgo/index.js +27 -0
  82. package/dist/Time/LibTimeGreeting/index.d.ts +17 -0
  83. package/dist/Time/LibTimeGreeting/index.js +23 -0
  84. package/dist/index.d.ts +395 -0
  85. package/dist/index.js +405 -0
  86. package/dist/lib.d.ts +39 -0
  87. package/dist/lib.js +39 -0
  88. package/package.json +24 -0
  89. package/test/index.js +0 -0
package/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # Lib自用JS工具方法
2
+
3
+ ## 起步
4
+
5
+ > 完整使用
6
+
7
+ ```ts
8
+ import { Lib } from "lib-js-utils";
9
+
10
+ Lib.Base.libGetDataType("Hellow World!");
11
+ ```
12
+
13
+ > 按需引入
14
+
15
+ ```ts
16
+ import { libGetDataType } from "lib-js-utils/dist/libGetDataType";
17
+
18
+ libGetDataType("Hellow World!");
19
+ ```
20
+
21
+ > 按需使用
22
+
23
+ ## Base-基础
24
+
25
+ ### LibGetDataType
26
+
27
+ > 返回数据类型
28
+
29
+ ```ts
30
+ libGetDataType(123); // "number"
31
+ libGetDataType("hello"); // "string"
32
+ libGetDataType([1, 2, 3]); // "array"
33
+ ```
34
+
@@ -0,0 +1,10 @@
1
+ export type LibGetDataTypeReturnType = "string" | "number" | "boolean" | "array" | "function" | "object";
2
+ /**
3
+ * @description 返回数据类型
4
+ * @param v 需要判断类型的数据
5
+ * @example
6
+ * libGetDataType(123); // "number"
7
+ * libGetDataType("hello"); // "string"
8
+ * libGetDataType([1, 2, 3]); // "array"
9
+ */
10
+ export declare const libGetDataType: (v: any) => LibGetDataTypeReturnType;
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @description 返回数据类型
3
+ * @param v 需要判断类型的数据
4
+ * @example
5
+ * libGetDataType(123); // "number"
6
+ * libGetDataType("hello"); // "string"
7
+ * libGetDataType([1, 2, 3]); // "array"
8
+ */
9
+ export const libGetDataType = (v) => {
10
+ return Object.prototype.toString
11
+ .call(v)
12
+ .substring(8)
13
+ .replace(/]/g, "")
14
+ .toLowerCase();
15
+ };
@@ -0,0 +1,7 @@
1
+ type ReturnType = "string" | "number" | "boolean" | "array" | "function" | "object";
2
+ /**
3
+ * @description 返回数据类型
4
+ * @param v 需要判断类型的数据
5
+ */
6
+ export declare const libJsGetDataType: (v: any) => ReturnType;
7
+ export {};
@@ -0,0 +1,11 @@
1
+ /**
2
+ * @description 返回数据类型
3
+ * @param v 需要判断类型的数据
4
+ */
5
+ export const libJsGetDataType = (v) => {
6
+ return Object.prototype.toString
7
+ .call(v)
8
+ .substring(8)
9
+ .replace(/]/g, "")
10
+ .toLowerCase();
11
+ };
@@ -0,0 +1,10 @@
1
+ /**
2
+ * @description 延时执行,切换到其他页面会暂停
3
+ * @param delay 延时毫秒数
4
+ * @param fn 延时执行函数
5
+ * @example
6
+ * libPromiseTimeout(3000, () => {
7
+ * console.log("执行延时函数");
8
+ * });
9
+ */
10
+ export declare const libPromiseTimeout: (delay?: number, fn?: () => void) => Promise<void>;
@@ -0,0 +1,40 @@
1
+ /**
2
+ * @description 延时执行,切换到其他页面会暂停
3
+ * @param delay 延时毫秒数
4
+ * @param fn 延时执行函数
5
+ * @example
6
+ * libPromiseTimeout(3000, () => {
7
+ * console.log("执行延时函数");
8
+ * });
9
+ */
10
+ export const libPromiseTimeout = (delay = 1, fn) => {
11
+ let timeoutId;
12
+ let remainingTime = delay;
13
+ let startTime = Date.now();
14
+ let isPaused = false;
15
+ return new Promise((resolve) => {
16
+ const handleVisibilityChange = () => {
17
+ if (document.hidden) {
18
+ isPaused = true;
19
+ clearTimeout(timeoutId);
20
+ remainingTime -= Date.now() - startTime;
21
+ }
22
+ else {
23
+ if (isPaused) {
24
+ isPaused = false;
25
+ startTime = Date.now();
26
+ startTimeout(resolve);
27
+ }
28
+ }
29
+ };
30
+ const startTimeout = (resolve) => {
31
+ timeoutId = setTimeout(() => {
32
+ fn?.();
33
+ resolve();
34
+ document.removeEventListener("visibilitychange", handleVisibilityChange);
35
+ }, remainingTime);
36
+ };
37
+ document.addEventListener("visibilitychange", handleVisibilityChange);
38
+ startTimeout(resolve);
39
+ });
40
+ };
@@ -0,0 +1,15 @@
1
+ /** @description console颜色打印
2
+ * @param title 标题
3
+ * @param color 颜色
4
+ * @param logs 信息
5
+ * @example
6
+ * // 使用红色打印日志
7
+ * libColorConsole("错误提示", "red", [{ label: "错误代码", value: 500 }]);
8
+ *
9
+ * // 使用蓝色打印简单日志
10
+ * libColorConsole("信息", "blue", "操作成功");
11
+ */
12
+ export declare const libColorConsole: (title: string, color: "red" | "orange" | "yellow" | "green" | "blue" | "purple", logs?: {
13
+ label: string;
14
+ value: any;
15
+ }[] | any) => void;
@@ -0,0 +1,38 @@
1
+ /** @description console颜色打印
2
+ * @param title 标题
3
+ * @param color 颜色
4
+ * @param logs 信息
5
+ * @example
6
+ * // 使用红色打印日志
7
+ * libColorConsole("错误提示", "red", [{ label: "错误代码", value: 500 }]);
8
+ *
9
+ * // 使用蓝色打印简单日志
10
+ * libColorConsole("信息", "blue", "操作成功");
11
+ */
12
+ export const libColorConsole = (title, color, logs = []) => {
13
+ const colors = {
14
+ red: "#c0392b",
15
+ orange: "#d35400",
16
+ yellow: "#f1c40f",
17
+ green: "#27ae60",
18
+ blue: "#2980b9",
19
+ purple: "#be2edd",
20
+ };
21
+ // 时间戳生成函数
22
+ const getTimestamp = () => {
23
+ const now = new Date();
24
+ const hour = String(now.getHours()).padStart(2, "0");
25
+ const minutes = String(now.getMinutes()).padStart(2, "0");
26
+ const seconds = String(now.getSeconds()).padStart(2, "0");
27
+ return `[${hour}:${minutes}:${seconds}]`;
28
+ };
29
+ if (Array.isArray(logs)) {
30
+ const v = logs.map((log) => {
31
+ return [`\n${log.label}:`, log.value];
32
+ });
33
+ console.log(`%c${getTimestamp()}-${title}`, `color: #fff;background: ${colors[color]}; font-size:14px;border-radius:5px;padding:0.25em;margin:0.5em 0`, ...v.flat(Infinity));
34
+ }
35
+ else {
36
+ console.log(`%c${getTimestamp()}-${title}`, `color: #fff;background: ${colors[color]}; font-size:14px;border-radius:5px;padding:0.25em;margin:0.5em 0`, logs);
37
+ }
38
+ };
@@ -0,0 +1,6 @@
1
+ /** @description 判断是否为移动设备
2
+ * @example
3
+ * const isMobile = libIsMobile();
4
+ * console.log(isMobile); // true 或 false
5
+ */
6
+ export declare const libIsMobile: () => boolean;
@@ -0,0 +1,9 @@
1
+ /** @description 判断是否为移动设备
2
+ * @example
3
+ * const isMobile = libIsMobile();
4
+ * console.log(isMobile); // true 或 false
5
+ */
6
+ export const libIsMobile = () => {
7
+ const ua = navigator.userAgent.toLowerCase();
8
+ return /mobile|android|iphone|ipod/.test(ua);
9
+ };
@@ -0,0 +1,6 @@
1
+ /** @description 判断是否为平板
2
+ * @example
3
+ * const isPad = libIsPad();
4
+ * console.log(isPad); // true 或 false
5
+ */
6
+ export declare const libIsPad: () => boolean;
@@ -0,0 +1,15 @@
1
+ /** @description 判断是否为平板
2
+ * @example
3
+ * const isPad = libIsPad();
4
+ * console.log(isPad); // true 或 false
5
+ */
6
+ export const libIsPad = () => {
7
+ const ua = navigator.userAgent.toLowerCase();
8
+ const width = Math.max(window.screen.width, window.screen.height);
9
+ const height = Math.min(window.screen.width, window.screen.height);
10
+ const aspectRatio = width / height;
11
+ const isTabletUserAgent = /ipad|tablet|playbook|silk/.test(ua) && !/mobile/.test(ua);
12
+ const isTabletWidth = width >= 768 && width <= 1366;
13
+ const isTabletAspectRatio = aspectRatio >= 1.3 && aspectRatio <= 1.6;
14
+ return isTabletUserAgent || (isTabletWidth && isTabletAspectRatio);
15
+ };
@@ -0,0 +1,6 @@
1
+ /** @description 获取浏览器地址栏参数
2
+ * @example
3
+ * const params = libPathParams();
4
+ * console.log(params); // { param1: "value1", param2: "value2" }
5
+ */
6
+ export declare const libPathParams: () => Record<string, string>;
@@ -0,0 +1,16 @@
1
+ /** @description 获取浏览器地址栏参数
2
+ * @example
3
+ * const params = libPathParams();
4
+ * console.log(params); // { param1: "value1", param2: "value2" }
5
+ */
6
+ export const libPathParams = () => {
7
+ const v = location.href;
8
+ const url = v.split("?")[1];
9
+ if (!url)
10
+ return {};
11
+ const params = url.split("&").reduce((pre, cur) => {
12
+ const [k, v] = cur.split(/=(.+)/);
13
+ return (pre[k] = v), pre;
14
+ }, {}) || {};
15
+ return params;
16
+ };
@@ -0,0 +1,7 @@
1
+ /** @description 动态设置网站标题及图标
2
+ * @param title 网站标题
3
+ * @param url 网站图标地址
4
+ * @example
5
+ * libSetTitleIcon("我的网站", "https://example.com/favicon.ico");
6
+ */
7
+ export declare const libSetTitleIcon: (title: string, url: string) => void;
@@ -0,0 +1,14 @@
1
+ /** @description 动态设置网站标题及图标
2
+ * @param title 网站标题
3
+ * @param url 网站图标地址
4
+ * @example
5
+ * libSetTitleIcon("我的网站", "https://example.com/favicon.ico");
6
+ */
7
+ export const libSetTitleIcon = (title, url) => {
8
+ document.title = title;
9
+ const link = document.createElement("link");
10
+ link.setAttribute("rel", "icon");
11
+ link.setAttribute("href", url);
12
+ const head = document.head || document.getElementsByTagName("head")[0];
13
+ head.appendChild(link);
14
+ };
@@ -0,0 +1,7 @@
1
+ /** @description 网站标题交互,当从当前网页切换到其他网页,网站标题自动切换
2
+ * @param backTitle 从其他网页返回时显示的标题
3
+ * @param leaveTitle 从当前网页离开时显示的标题
4
+ * * @example
5
+ * libTagTitleTip("欢迎回来", "来和妲己玩耍吧!");
6
+ */
7
+ export declare const libTagTitleTip: (backTitle: string, leaveTitle: string) => void;
@@ -0,0 +1,24 @@
1
+ /** @description 网站标题交互,当从当前网页切换到其他网页,网站标题自动切换
2
+ * @param backTitle 从其他网页返回时显示的标题
3
+ * @param leaveTitle 从当前网页离开时显示的标题
4
+ * * @example
5
+ * libTagTitleTip("欢迎回来", "来和妲己玩耍吧!");
6
+ */
7
+ export const libTagTitleTip = (backTitle, leaveTitle) => {
8
+ let document_title = "";
9
+ let timer;
10
+ window.addEventListener("visibilitychange", () => {
11
+ clearTimeout(timer);
12
+ if (document.hidden) {
13
+ if (document.title !== backTitle) {
14
+ document_title = document.title;
15
+ }
16
+ document.title = leaveTitle;
17
+ return;
18
+ }
19
+ document.title = backTitle;
20
+ timer = setTimeout(() => {
21
+ document.title = document_title;
22
+ }, 1000);
23
+ });
24
+ };
@@ -0,0 +1,9 @@
1
+ /**
2
+ * @description 将数组拆分成指定数组元素数量的多个数组
3
+ * @param arr 需要拆分的数组
4
+ * @param chunkSize 每个数组的元素数量
5
+ * @example
6
+ * const chunks = libChunkArray([1, 2, 3, 4, 5, 6], 2);
7
+ * console.log(chunks); // [[1, 2], [3, 4], [5, 6]]
8
+ */
9
+ export declare const libChunkArray: <T>(arr: T[], chunkSize: number) => T[][];
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @description 将数组拆分成指定数组元素数量的多个数组
3
+ * @param arr 需要拆分的数组
4
+ * @param chunkSize 每个数组的元素数量
5
+ * @example
6
+ * const chunks = libChunkArray([1, 2, 3, 4, 5, 6], 2);
7
+ * console.log(chunks); // [[1, 2], [3, 4], [5, 6]]
8
+ */
9
+ export const libChunkArray = (arr, chunkSize) => {
10
+ const result = [];
11
+ for (let i = 0; i < arr.length; i += chunkSize) {
12
+ result.push(arr.slice(i, i + chunkSize));
13
+ }
14
+ return result;
15
+ };
@@ -0,0 +1,6 @@
1
+ /** @description 递归将JSON字符串深度解析为对象
2
+ * @example
3
+ * const obj = libDeepJSONParse('{"a": 1, "b": "{\"c\": 2}"}');
4
+ * console.log(obj); // { a: 1, b: { c: 2 } }
5
+ */
6
+ export declare const libDeepJSONParse: <T>(data: any) => T;
@@ -0,0 +1,32 @@
1
+ /** @description 递归将JSON字符串深度解析为对象
2
+ * @example
3
+ * const obj = libDeepJSONParse('{"a": 1, "b": "{\"c\": 2}"}');
4
+ * console.log(obj); // { a: 1, b: { c: 2 } }
5
+ */
6
+ export const libDeepJSONParse = (data) => {
7
+ // 检查是否为字符串并尝试解析
8
+ if (typeof data === "string") {
9
+ try {
10
+ const parsed = JSON.parse(data);
11
+ // 递归解析解析后的结果
12
+ return libDeepJSONParse(parsed);
13
+ }
14
+ catch {
15
+ // 如果解析失败,返回原始字符串
16
+ return data;
17
+ }
18
+ }
19
+ // 如果是数组,递归处理每个元素
20
+ if (Array.isArray(data)) {
21
+ return data.map((item) => libDeepJSONParse(item));
22
+ }
23
+ // 如果是对象,递归处理每个属性值
24
+ if (data !== null && typeof data === "object") {
25
+ return Object.keys(data).reduce((acc, key) => {
26
+ acc[key] = libDeepJSONParse(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 = libGroupArrayByKey([{ 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 libGroupArrayByKey: (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 = libGroupArrayByKey([{ 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 libGroupArrayByKey = (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 = 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,8 @@
1
+ /**
2
+ * @description 高级搜索
3
+ * @param data 需要搜索的数据
4
+ * @param value 搜索的值
5
+ * @param keys 搜索的键名
6
+ * @param highlight 是否高亮
7
+ */
8
+ export declare const libSearch: <T>(data: T[], value: string | string[], keys: string | string[], highlight?: boolean) => T[];
@@ -0,0 +1,45 @@
1
+ import { match as _pinyinMatch } from "pinyin-pro";
2
+ /**
3
+ * @description 高级搜索
4
+ * @param data 需要搜索的数据
5
+ * @param value 搜索的值
6
+ * @param keys 搜索的键名
7
+ * @param highlight 是否高亮
8
+ */
9
+ export const libSearch = (data, value, keys, highlight = false) => {
10
+ if (!Array.isArray(value) && value === "") {
11
+ return data;
12
+ }
13
+ const arr = [];
14
+ // 根据给定的值和键名进行搜索
15
+ const fn = (searchValue, key) => {
16
+ const reg = new RegExp(searchValue.toLowerCase(), "i"); // 创建正则表达式
17
+ data.forEach((item) => {
18
+ item[key] = item[key].toString(); // 确保属性为字符串
19
+ if (_pinyinMatch(item[key], searchValue, { precision: "start" })) {
20
+ // 匹配拼音或属性值
21
+ if (highlight && searchValue) {
22
+ item[key] = item[key].replace(reg, (match) => `<i style="color:var(--blue)">${match}</i>`); // 高亮匹配部分
23
+ }
24
+ arr.push(item);
25
+ }
26
+ });
27
+ };
28
+ if (Array.isArray(keys)) {
29
+ keys.forEach((key) => fn(value, key)); // 遍历键名进行搜索
30
+ }
31
+ else if (Array.isArray(value)) {
32
+ value.forEach((val) => {
33
+ if (Array.isArray(keys)) {
34
+ keys.forEach((key) => fn(val, key)); // 遍历值和键名进行搜索
35
+ }
36
+ else {
37
+ fn(val, keys); // 单个键名和多个值进行搜索
38
+ }
39
+ });
40
+ }
41
+ else {
42
+ fn(value, keys); // 单个值和单个键名进行搜索
43
+ }
44
+ return arr;
45
+ };
@@ -0,0 +1,7 @@
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 declare const libShuffleArray: <T>(arr: T[]) => T[];
@@ -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,8 @@
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 declare const libStepArray: <T>(arr: T[], step: number) => T[];
@@ -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,7 @@
1
+ /** @description 下载图片链接
2
+ * @param link 图片链接
3
+ * @param name 图片名称
4
+ * @example
5
+ * libDownloadImageLink("https://example.com/image.jpg", "downloaded-image.jpg");
6
+ */
7
+ export declare const libDownloadImageLink: (link: string, name: string) => void;
@@ -0,0 +1,17 @@
1
+ /** @description 下载图片链接
2
+ * @param link 图片链接
3
+ * @param name 图片名称
4
+ * @example
5
+ * libDownloadImageLink("https://example.com/image.jpg", "downloaded-image.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,5 @@
1
+ /** @description 计算文件大小
2
+ * @param file 文件对象
3
+ * @returns 文件大小KB
4
+ */
5
+ export declare const libFileSize: (file: any) => number;
@@ -0,0 +1,11 @@
1
+ /** @description 计算文件大小
2
+ * @param file 文件对象
3
+ * @returns 文件大小KB
4
+ */
5
+ export const libFileSize = (file) => {
6
+ const str = JSON.stringify(file, null, 2);
7
+ const blob = new Blob([str]);
8
+ const fileSizeInBytes = blob.size;
9
+ const fileSizeInKB = (fileSizeInBytes / 1024).toFixed(2);
10
+ return Number(fileSizeInKB);
11
+ };
@@ -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: (data: FormData, file: File, url: string) => void;
12
+ /** 失败回调 */
13
+ fail?: (error: ErrorEvent) => void;
14
+ };
15
+ /** @description 图片压缩
16
+ * @param obj 压缩参数
17
+ * @example
18
+ * // 图片压缩使用示例
19
+ * libImageOptimizerOptionsParams({
20
+ * file: myFile,
21
+ * ratio: 0.8,
22
+ * width: 800,
23
+ * maxSize: 1024,
24
+ * success: (data, file, url) => {
25
+ * console.log('压缩成功', data, file, url);
26
+ * },
27
+ * fail: (error) => {
28
+ * console.error('压缩失败', error);
29
+ * }
30
+ * });
31
+ */
32
+ export declare const libImageOptimizer: (obj: LibImageOptimizerOptionsParams) => void;