@qy_better_lib/core 0.1.3 → 0.1.5

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/lib/utils/is.d.ts CHANGED
@@ -3,50 +3,140 @@
3
3
  * @param val
4
4
  * @returns
5
5
  */
6
- export declare function isUndefined(val: any): boolean;
6
+ export declare function is_undefined(val: any): boolean;
7
+ /**
8
+ * 是否为null
9
+ * @param val
10
+ * @returns
11
+ */
12
+ export declare function is_null(val: any): boolean;
7
13
  /**
8
14
  * 是否为数组
9
15
  * @param val
10
16
  * @returns
11
17
  */
12
- export declare function isArray(val: any): boolean;
18
+ export declare function is_array(val: any): boolean;
13
19
  /**
14
20
  * 是否为对象
15
21
  * @param val
16
22
  * @returns
17
23
  */
18
- export declare function isObject(val: any): boolean;
24
+ export declare function is_object(val: any): boolean;
25
+ /**
26
+ * 是否为纯粹的对象(排除数组、null等)
27
+ * @param val
28
+ * @returns
29
+ */
30
+ export declare function is_plain_object(val: any): boolean;
19
31
  /**
20
32
  * 是否为日期时间
21
33
  * @param val
22
34
  * @returns
23
35
  */
24
- export declare function isDate(val: any): boolean;
36
+ export declare function is_date(val: any): boolean;
37
+ /**
38
+ * 是否为有效的日期
39
+ * @param val
40
+ * @returns
41
+ */
42
+ export declare function is_valid_date(val: any): boolean;
43
+ /**
44
+ * 是否为布尔值
45
+ * @param val
46
+ * @returns
47
+ */
48
+ export declare function is_boolean(val: any): boolean;
49
+ /**
50
+ * 是否为函数
51
+ * @param val
52
+ * @returns
53
+ */
54
+ export declare function is_function(val: any): boolean;
55
+ /**
56
+ * 是否为正则表达式
57
+ * @param val
58
+ * @returns
59
+ */
60
+ export declare function is_reg_exp(val: any): boolean;
61
+ /**
62
+ * 是否为Symbol
63
+ * @param val
64
+ * @returns
65
+ */
66
+ export declare function is_symbol(val: any): boolean;
67
+ /**
68
+ * 是否为Promise
69
+ * @param val
70
+ * @returns
71
+ */
72
+ export declare function is_promise(val: any): boolean;
25
73
  /**
26
74
  * 是否为空
27
75
  * @param val
28
76
  * @returns
29
77
  */
30
- export declare function isEmpty(val: unknown): boolean;
78
+ export declare function is_empty(val: unknown): boolean;
79
+ /**
80
+ * 是否为真值
81
+ * @param val
82
+ * @returns
83
+ */
84
+ export declare function is_truthy(val: any): boolean;
85
+ /**
86
+ * 是否为假值
87
+ * @param val
88
+ * @returns
89
+ */
90
+ export declare function is_falsy(val: any): boolean;
31
91
  /**
32
92
  * 判断对象是否为DOM元素
33
93
  * @param e
34
94
  * @returns
35
95
  */
36
- export declare function isElement(e: unknown): e is Element;
96
+ export declare function is_element(e: unknown): e is Element;
97
+ /**
98
+ * 判断对象是否为HTMLElement
99
+ * @param e
100
+ * @returns
101
+ */
102
+ export declare function is_html_element(e: unknown): e is HTMLElement;
37
103
  /**
38
104
  * 是否是客户端
39
105
  */
40
- export declare const isClient: boolean;
41
- /**是否未数值 */
42
- export declare const isNumber: (val: any) => val is number;
43
- /**是否为字符串 */
44
- export declare const isString: (val: any) => val is string;
45
- /**是否为字符串数值 */
46
- export declare const isStringNumber: (val: string) => boolean;
47
- /**是否为手机号(国内手机号) */
48
- export declare function isMobile(str: string): boolean;
49
- /**是否为有效IP地址 */
50
- export declare function isIp(val: string): boolean;
51
- /**是否为有效端口 */
52
- export declare function isPort(val: string): boolean;
106
+ export declare const is_client: boolean;
107
+ /**
108
+ * 是否为数值
109
+ */
110
+ export declare function is_number(val: any): boolean;
111
+ /**
112
+ * 是否为字符串
113
+ */
114
+ export declare function is_string(val: any): boolean;
115
+ /**
116
+ * 是否为字符串数值
117
+ */
118
+ export declare function is_string_number(val: any): boolean;
119
+ /**
120
+ * 是否为手机号(国内手机号)
121
+ */
122
+ export declare function is_mobile(str: string): boolean;
123
+ /**
124
+ * 是否为邮箱地址
125
+ */
126
+ export declare function is_email(str: string): boolean;
127
+ /**
128
+ * 是否为URL地址
129
+ */
130
+ export declare function is_url(str: string): boolean;
131
+ /**
132
+ * 是否为十六进制颜色值
133
+ */
134
+ export declare function is_hex_color(str: string): boolean;
135
+ /**
136
+ * 是否为有效IP地址
137
+ */
138
+ export declare function is_ip(val: string): boolean;
139
+ /**
140
+ * 是否为有效端口
141
+ */
142
+ export declare function is_port(val: string): boolean;
package/lib/utils/is.js CHANGED
@@ -1,43 +1,126 @@
1
- function o(t) {
2
- return t === void 0;
1
+ function is_undefined(val) {
2
+ return val === void 0;
3
3
  }
4
- function e(t) {
5
- return Object.prototype.toString.call(t) === "[object Array]";
4
+ function is_null(val) {
5
+ return val === null;
6
6
  }
7
- function n(t) {
8
- return typeof t == "object" && Object.prototype.toString.call(t) === "[object Object]";
7
+ function is_array(val) {
8
+ return Array.isArray(val);
9
9
  }
10
- function c(t) {
11
- return Object.prototype.toString.call(t) === "[object Date]";
10
+ function is_object(val) {
11
+ return typeof val === "object" && val !== null && Object.prototype.toString.call(val) === "[object Object]";
12
12
  }
13
- function u(t) {
14
- return !t && t !== 0 || e(t) && t.length === 0 || n(t) && !Object.keys(t).length;
13
+ function is_plain_object(val) {
14
+ if (typeof val !== "object" || val === null) return false;
15
+ let proto = val;
16
+ while (Object.getPrototypeOf(proto) !== null) {
17
+ proto = Object.getPrototypeOf(proto);
18
+ }
19
+ return Object.getPrototypeOf(val) === proto;
15
20
  }
16
- function s(t) {
17
- return typeof Element > "u" ? !1 : t instanceof Element;
21
+ function is_date(val) {
22
+ return Object.prototype.toString.call(val) === "[object Date]";
18
23
  }
19
- const f = typeof window < "u", b = (t) => typeof t == "number", r = (t) => typeof t == "string", d = (t) => r(t) ? !Number.isNaN(Number(t)) : !1;
20
- function p(t) {
21
- return /^1[3-9]\d{9}$/.test(t);
24
+ function is_valid_date(val) {
25
+ return is_date(val) && !isNaN(val.getTime());
22
26
  }
23
- function y(t) {
24
- return /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(t);
27
+ function is_boolean(val) {
28
+ return typeof val === "boolean";
25
29
  }
26
- function g(t) {
27
- return /^(6553[0-5]|655[0-2]\d|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]\d{4}|[1-9]\d{0,3}|0)$/.test(t);
30
+ function is_function(val) {
31
+ return typeof val === "function";
32
+ }
33
+ function is_reg_exp(val) {
34
+ return Object.prototype.toString.call(val) === "[object RegExp]";
35
+ }
36
+ function is_symbol(val) {
37
+ return typeof val === "symbol";
38
+ }
39
+ function is_promise(val) {
40
+ return typeof val === "object" && val !== null && typeof val.then === "function" && typeof val.catch === "function";
41
+ }
42
+ function is_empty(val) {
43
+ return !val && val !== 0 || is_array(val) && val.length === 0 || is_object(val) && !Object.keys(val).length;
44
+ }
45
+ function is_truthy(val) {
46
+ return !!val;
47
+ }
48
+ function is_falsy(val) {
49
+ return !val;
50
+ }
51
+ function is_element(e) {
52
+ if (typeof Element === "undefined") return false;
53
+ return e instanceof Element;
54
+ }
55
+ function is_html_element(e) {
56
+ if (typeof HTMLElement === "undefined") return false;
57
+ return e instanceof HTMLElement;
58
+ }
59
+ const is_client = typeof window !== "undefined";
60
+ function is_number(val) {
61
+ return typeof val === "number" && !isNaN(val);
62
+ }
63
+ function is_string(val) {
64
+ return typeof val === "string";
65
+ }
66
+ function is_string_number(val) {
67
+ if (!is_string(val)) {
68
+ return false;
69
+ }
70
+ return !Number.isNaN(Number(val));
71
+ }
72
+ function is_mobile(str) {
73
+ const regex = /^1[3-9]\d{9}$/;
74
+ return regex.test(str);
75
+ }
76
+ function is_email(str) {
77
+ const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
78
+ return regex.test(str);
79
+ }
80
+ function is_url(str) {
81
+ try {
82
+ new URL(str);
83
+ return true;
84
+ } catch {
85
+ return false;
86
+ }
87
+ }
88
+ function is_hex_color(str) {
89
+ const regex = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/;
90
+ return regex.test(str);
91
+ }
92
+ function is_ip(val) {
93
+ return /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(val);
94
+ }
95
+ function is_port(val) {
96
+ return /^(6553[0-5]|655[0-2]\d|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]\d{4}|[1-9]\d{0,3}|0)$/.test(val);
28
97
  }
29
98
  export {
30
- e as isArray,
31
- f as isClient,
32
- c as isDate,
33
- s as isElement,
34
- u as isEmpty,
35
- y as isIp,
36
- p as isMobile,
37
- b as isNumber,
38
- n as isObject,
39
- g as isPort,
40
- r as isString,
41
- d as isStringNumber,
42
- o as isUndefined
99
+ is_array,
100
+ is_boolean,
101
+ is_client,
102
+ is_date,
103
+ is_element,
104
+ is_email,
105
+ is_empty,
106
+ is_falsy,
107
+ is_function,
108
+ is_hex_color,
109
+ is_html_element,
110
+ is_ip,
111
+ is_mobile,
112
+ is_null,
113
+ is_number,
114
+ is_object,
115
+ is_plain_object,
116
+ is_port,
117
+ is_promise,
118
+ is_reg_exp,
119
+ is_string,
120
+ is_string_number,
121
+ is_symbol,
122
+ is_truthy,
123
+ is_undefined,
124
+ is_url,
125
+ is_valid_date
43
126
  };
@@ -1,18 +1,88 @@
1
1
  /**
2
2
  * 数字转成千分位
3
- * @param num
4
- * @returns
3
+ * @param num 数字或数字字符串
4
+ * @returns 千分位格式的字符串
5
5
  */
6
- export declare function thousandSeparator(num: number | any): string;
6
+ export declare function thousand_separator(num: any): string;
7
7
  /**
8
8
  * 转换成数值
9
9
  * @param num 源
10
10
  * @param fixed 保留小数点,默认不设置
11
11
  */
12
- export declare function toNumber(num: string | undefined, fixed?: number): number | undefined;
12
+ export declare function to_number(num: any): number;
13
13
  /**
14
14
  * 保留小数点
15
+ * @param num 数字或数字字符串
16
+ * @param fixed 小数位数
17
+ */
18
+ export declare function to_fixed(num: any, fixed: number): number;
19
+ /**
20
+ * 格式化货币
21
+ * @param num 数字
22
+ * @param currency 货币符号,默认为 ¥
23
+ * @param fixed 小数位数,默认为 2
24
+ * @returns 格式化后的货币字符串
25
+ */
26
+ export declare function format_currency(num: number, currency?: string, fixed?: number): string;
27
+ /**
28
+ * 限制数字范围
29
+ * @param num 数字
30
+ * @param min 最小值
31
+ * @param max 最大值
32
+ * @returns 限制在范围内的数字
33
+ */
34
+ export declare function clamp(num: number, min: number, max: number): number;
35
+ /**
36
+ * 转换为百分比
37
+ * @param num 数字或数字字符串
38
+ * @param fixed 小数位数,默认为 2
39
+ * @returns 百分比字符串
40
+ */
41
+ export declare function to_percentage(num: any, fixed?: number): string;
42
+ /**
43
+ * 判断数字奇偶性
15
44
  * @param num 数字
16
- * @param fixed 小数点
45
+ * @returns 是否为偶数
46
+ */
47
+ export declare function is_even(num: number): boolean;
48
+ /**
49
+ * 获取数字的整数部分
50
+ * @param num 数字
51
+ * @returns 整数部分
52
+ */
53
+ export declare function get_integer_part(num: number): number;
54
+ /**
55
+ * 获取数字的小数部分
56
+ * @param num 数字
57
+ * @returns 小数部分
58
+ */
59
+ export declare function get_decimal_part(num: number): number;
60
+ /**
61
+ * 数字四舍五入
62
+ * @param num 数字
63
+ * @param precision 精度,默认为 0
64
+ * @returns 四舍五入后的数字
65
+ */
66
+ export declare function round(num: number, precision?: number): number;
67
+ /**
68
+ * 数字向上取整
69
+ * @param num 数字
70
+ * @param precision 精度,默认为 0
71
+ * @returns 向上取整后的数字
72
+ */
73
+ export declare function ceil(num: number, precision?: number): number;
74
+ /**
75
+ * 数字向下取整
76
+ * @param num 数字
77
+ * @param precision 精度,默认为 0
78
+ * @returns 向下取整后的数字
79
+ */
80
+ export declare function floor(num: number, precision?: number): number;
81
+ /**
82
+ * 计算两个数字之间的百分比
83
+ * @param current 当前值
84
+ * @param total 总值
85
+ * @param fixed 小数位数,默认为 2
86
+ * @returns 百分比值
17
87
  */
18
- export declare function toFixed(num: number, fixed: number): number;
88
+ export declare function calculate_percentage(current: number, total: number, fixed?: number): number;
@@ -1,15 +1,99 @@
1
- import { isNumber as t } from "./is.js";
2
- function u(r) {
3
- return r ? Object.prototype.toString.call(1) !== "[object Number]" ? `${r}` : `${r}`.replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, "$1,") : "0";
1
+ import { is_number, is_string } from "./is.js";
2
+ function thousand_separator(num) {
3
+ if (!num && num !== 0) {
4
+ return "0";
5
+ }
6
+ const parsed_num = to_number(num);
7
+ if (isNaN(parsed_num)) {
8
+ return `${num}`;
9
+ }
10
+ return `${parsed_num}`.replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, "$1,");
4
11
  }
5
- function i(r, e) {
6
- return t(r) ? Number(e ? Number(r).toFixed(e) : r) : Number.NaN;
12
+ function to_number(num) {
13
+ if (is_number(num)) {
14
+ return num;
15
+ }
16
+ if (is_string(num)) {
17
+ const parsedNum = parseFloat(num);
18
+ if (isNaN(parsedNum)) {
19
+ return Number.NaN;
20
+ }
21
+ return parsedNum;
22
+ }
23
+ return Number.NaN;
7
24
  }
8
- function N(r, e) {
9
- return t(r) ? Number(r.toFixed(e)) : Number.NaN;
25
+ function to_fixed(num, fixed) {
26
+ const parsedNum = to_number(num);
27
+ if (isNaN(parsedNum)) return Number.NaN;
28
+ return Number(parsedNum.toFixed(fixed));
29
+ }
30
+ function format_currency(num, currency = "¥", fixed = 2) {
31
+ if (!is_number(num)) return currency + "0.00";
32
+ let fixedNum;
33
+ if (fixed === 0) {
34
+ const flooredNum = Math.floor(num);
35
+ fixedNum = flooredNum.toString();
36
+ } else {
37
+ fixedNum = num.toFixed(fixed);
38
+ }
39
+ const formattedNum = thousand_separator(fixedNum);
40
+ return currency + formattedNum;
41
+ }
42
+ function clamp(num, min, max) {
43
+ if (!is_number(num)) return Number.NaN;
44
+ return Math.max(min, Math.min(max, num));
45
+ }
46
+ function to_percentage(num, fixed = 2) {
47
+ const parsedNum = to_number(num);
48
+ if (isNaN(parsedNum)) return "0%";
49
+ const percentage = parsedNum * 100;
50
+ const formattedPercentage = percentage.toFixed(fixed);
51
+ return formattedPercentage + "%";
52
+ }
53
+ function is_even(num) {
54
+ if (!is_number(num)) return false;
55
+ return num % 2 === 0;
56
+ }
57
+ function get_integer_part(num) {
58
+ if (!is_number(num)) return Number.NaN;
59
+ return Math.trunc(num);
60
+ }
61
+ function get_decimal_part(num) {
62
+ if (!is_number(num)) return Number.NaN;
63
+ return num - Math.trunc(num);
64
+ }
65
+ function round(num, precision = 0) {
66
+ if (!is_number(num)) return Number.NaN;
67
+ const factor = Math.pow(10, precision);
68
+ return Math.round(num * factor) / factor;
69
+ }
70
+ function ceil(num, precision = 0) {
71
+ if (!is_number(num)) return Number.NaN;
72
+ const factor = Math.pow(10, precision);
73
+ return Math.ceil(num * factor) / factor;
74
+ }
75
+ function floor(num, precision = 0) {
76
+ if (!is_number(num)) return Number.NaN;
77
+ const factor = Math.pow(10, precision);
78
+ return Math.floor(num * factor) / factor;
79
+ }
80
+ function calculate_percentage(current, total, fixed = 2) {
81
+ if (!is_number(current) || !is_number(total) || total === 0) return Number.NaN;
82
+ const percentage = current / total * 100;
83
+ return to_fixed(percentage, fixed);
10
84
  }
11
85
  export {
12
- u as thousandSeparator,
13
- N as toFixed,
14
- i as toNumber
86
+ calculate_percentage,
87
+ ceil,
88
+ clamp,
89
+ floor,
90
+ format_currency,
91
+ get_decimal_part,
92
+ get_integer_part,
93
+ is_even,
94
+ round,
95
+ thousand_separator,
96
+ to_fixed,
97
+ to_number,
98
+ to_percentage
15
99
  };
@@ -1,11 +1,81 @@
1
- export declare function deepClone(obj: any): any;
2
1
  /**
3
- *深度合并多个对象的方法
2
+ * 深拷贝对象
3
+ * @param obj 要拷贝的对象
4
+ * @returns 拷贝后的新对象
4
5
  */
5
- export declare function deepAssign(...arg: any[]): Record<string, any>;
6
+ export declare function deep_clone(obj: any): any;
7
+ /**
8
+ * 深度合并多个对象
9
+ * @param args 要合并的对象列表
10
+ * @returns 合并后的对象
11
+ */
12
+ export declare function deep_assign(...args: any[]): Record<string, any>;
6
13
  /**
7
14
  * 深度比较两个对象值是否一致
8
- * 不考虑值,不考虑引用地址
9
- * @returns boolean
15
+ * @param a 第一个对象
16
+ * @param b 第二个对象
17
+ * @returns 是否相等
18
+ */
19
+ export declare function deep_equal(a: any, b: any): boolean;
20
+ /**
21
+ * 检查对象是否为空
22
+ * @param obj 要检查的对象
23
+ * @returns 是否为空对象
24
+ */
25
+ export declare function is_empty_object(obj: any): boolean;
26
+ /**
27
+ * 从对象中提取指定属性
28
+ * @param obj 源对象
29
+ * @param keys 要提取的属性名数组
30
+ * @returns 提取后的新对象
31
+ */
32
+ export declare function pick(obj: any, keys: string[]): Record<string, any>;
33
+ /**
34
+ * 从对象中排除指定属性
35
+ * @param obj 源对象
36
+ * @param keys 要排除的属性名数组
37
+ * @returns 排除后的新对象
38
+ */
39
+ export declare function omit(obj: any, keys: string[]): Record<string, any>;
40
+ /**
41
+ * 将嵌套对象扁平化为一维对象
42
+ * @param obj 源对象
43
+ * @param prefix 前缀
44
+ * @returns 扁平化后的对象
45
+ */
46
+ export declare function flatten_object(obj: any, prefix?: string): Record<string, any>;
47
+ /**
48
+ * 将扁平对象转换为嵌套对象
49
+ * @param obj 扁平对象
50
+ * @returns 嵌套对象
51
+ */
52
+ export declare function unflatten_object(obj: any): Record<string, any>;
53
+ /**
54
+ * 根据路径获取对象值
55
+ * @param obj 源对象
56
+ * @param path 路径字符串,如 "a.b.c"
57
+ * @param defaultValue 默认值
58
+ * @returns 获取的值或默认值
59
+ */
60
+ export declare function get_object_value(obj: any, path: string, defaultValue?: any): any;
61
+ /**
62
+ * 根据路径设置对象值
63
+ * @param obj 源对象
64
+ * @param path 路径字符串,如 "a.b.c"
65
+ * @param value 要设置的值
66
+ * @returns 修改后的对象
67
+ */
68
+ export declare function set_object_value(obj: any, path: string, value: any): Record<string, any>;
69
+ /**
70
+ * 浅合并多个对象
71
+ * @param args 要合并的对象列表
72
+ * @returns 合并后的对象
73
+ */
74
+ export declare function merge_objects(...args: any[]): Record<string, any>;
75
+ /**
76
+ * 比较两个对象的差异
77
+ * @param obj1 第一个对象
78
+ * @param obj2 第二个对象
79
+ * @returns 差异对象
10
80
  */
11
- export declare function deepEqual(a: any, b: any): boolean;
81
+ export declare function diff_objects(obj1: any, obj2: any): Record<string, any>;