nhanh-pure-function 1.2.8 → 1.3.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.
package/lib/Index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
- export * from "./Utility";
2
- export * from "./User";
1
+ export * from "./Utility/Utility";
2
+ export * from "./User/User";
3
+ export * from "./Math/Math";
3
4
 
4
5
  // 提取单个函数的参数类型
5
6
  export type ExtractParameters<T> = T extends (...args: infer P) => any
package/lib/Index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import "./index.css";
2
2
 
3
- export * from "./Utility";
4
- export * from "./User";
3
+ export * from "./Utility/Utility.js";
4
+ export * from "./User/User.js";
5
+ export * from "./Math/Math.js";
@@ -0,0 +1,73 @@
1
+ /**
2
+ * 转为百分比字符串
3
+ * @param value 分子
4
+ * @param totalValue 分母
5
+ * @param decimalPlaces 保留小数位
6
+ * @returns 10.00%
7
+ */
8
+ export function _ConvertToPercentage(
9
+ value: number,
10
+ totalValue: number,
11
+ decimalPlaces?: number
12
+ ): number;
13
+
14
+ /**
15
+ * 误差范围
16
+ * @param value 需要判断的数字
17
+ * @param target 目标数字
18
+ * @param errorMargin 正负误差范围
19
+ * @returns 是否在误差内
20
+ */
21
+ export function _IsWithinErrorMargin(
22
+ value: number,
23
+ target: number,
24
+ errorMargin: number
25
+ ): boolean;
26
+
27
+ /**
28
+ * 进度
29
+ * @param {(schedule)=>void} callback callback( 进度百分比 )
30
+ * @param {Number} TIME 总时长
31
+ */
32
+ export function _Schedule(
33
+ callback: (schedule: number) => void,
34
+ TIME: number
35
+ ): void;
36
+
37
+ /**
38
+ * 格式化数字,给数字加上千位分隔符。
39
+ * @param {number} number - 要格式化的数字。
40
+ * @returns {string} - 格式化后的字符串。
41
+ */
42
+ export function _FormatNumber(number: number): string;
43
+
44
+ /**
45
+ * 纯数字转 数字加单位
46
+ * @param number 数字或字符串数字
47
+ * @param config : {
48
+ * join 拼接起来吗
49
+ * suffix 后缀
50
+ * integer 不超过万位的数字时保持整数吗
51
+ * }
52
+ * @returns 123456 --> 12.34万 | [ 12.34 , 万 ]
53
+ */
54
+ export function _FormatNumberWithUnit(
55
+ number: string | number,
56
+ config?: {
57
+ join?: boolean;
58
+ suffix?: string;
59
+ integer?: boolean;
60
+ }
61
+ ): string | [number, string];
62
+
63
+ interface Point {
64
+ x: number;
65
+ y: number;
66
+ }
67
+ /**
68
+ * 判断点是否在多边形内
69
+ * @param point - 待检测的点,包含 x 和 y 坐标
70
+ * @param polygon - 多边形的点集,数组形式,每个点包含 x 和 y 坐标
71
+ * @returns boolean - 点是否在多边形内
72
+ */
73
+ export function _IsPointInPolygon(point: Point, polygon: Point[]): boolean;
@@ -0,0 +1,163 @@
1
+ /**
2
+ * 转为百分比字符串
3
+ * @param value 分子
4
+ * @param totalValue 分母
5
+ * @param decimalPlaces 保留小数位
6
+ * @returns 10.00%
7
+ */
8
+ export function _ConvertToPercentage(value, totalValue, decimalPlaces = 2) {
9
+ if (
10
+ typeof value !== "number" ||
11
+ typeof totalValue !== "number" ||
12
+ typeof decimalPlaces !== "number" ||
13
+ totalValue == 0
14
+ ) {
15
+ console.error("异常输入:", arguments);
16
+ return "0.00%";
17
+ }
18
+ return (
19
+ Number(
20
+ parseInt((value / totalValue) * Math.pow(10, 2 + decimalPlaces)) /
21
+ Math.pow(10, decimalPlaces)
22
+ ) || 0
23
+ );
24
+ }
25
+
26
+ /**
27
+ * 误差范围
28
+ * @param value 需要判断的数字
29
+ * @param target 目标数字
30
+ * @param errorMargin 正负误差范围
31
+ * @returns 是否在误差内
32
+ */
33
+ export function _IsWithinErrorMargin(value, target, errorMargin) {
34
+ return Math.abs(value - target) <= errorMargin;
35
+ }
36
+
37
+ /**
38
+ * 进度
39
+ * @param {(schedule)=>void} callback callback( 进度百分比 )
40
+ * @param {Number} TIME 总时长
41
+ */
42
+ export function _Schedule(callback, TIME = 500) {
43
+ let t;
44
+ function loop(time) {
45
+ if (!t) t = time;
46
+ let percentage = Math.min((time - t) / TIME, 1);
47
+ callback(percentage);
48
+ if (time - t < TIME) requestAnimationFrame(loop);
49
+ }
50
+ requestAnimationFrame(loop);
51
+ }
52
+
53
+ /**
54
+ * 格式化数字,给数字加上千位分隔符。
55
+ * @param {number} number - 要格式化的数字。
56
+ * @returns {string} - 格式化后的字符串。
57
+ */
58
+ export function _FormatNumber(number) {
59
+ return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
60
+ }
61
+
62
+ /**
63
+ * 纯数字转 数字加单位
64
+ * @param number 数字或字符串数字
65
+ * @param config : {
66
+ * join 拼接起来吗
67
+ * suffix 后缀
68
+ * integer 不超过万位的数字时保持整数吗
69
+ * }
70
+ * @returns 123456 --> 12.34万 | [ 12.34 , 万 ]
71
+ */
72
+ export function _FormatNumberWithUnit(number, config = {}) {
73
+ const { join, suffix, integer } = Object.assign(
74
+ {
75
+ join: true,
76
+ suffix: "",
77
+ integer: false,
78
+ },
79
+ config
80
+ );
81
+
82
+ function _join(value, suffix, plus = true) {
83
+ value = (plus ? "" : "-") + value;
84
+ if (join) return value + suffix;
85
+ else return [value, suffix];
86
+ }
87
+
88
+ if (typeof number == "string") {
89
+ if (!/^\d+$/.test(number.trim())) {
90
+ console.error("错误输入:", number);
91
+ return _join(0, suffix);
92
+ }
93
+ } else if (typeof number != "number") {
94
+ console.error("错误输入:", number);
95
+ return _join(0, suffix);
96
+ }
97
+
98
+ if (Math.abs(number) == Infinity || number == 0) {
99
+ return _join(0, suffix);
100
+ }
101
+
102
+ number = Number(number);
103
+ const plus = number >= 0;
104
+ number = Math.abs(number);
105
+
106
+ const units = [
107
+ "",
108
+ "万",
109
+ "亿",
110
+ "兆",
111
+ "京",
112
+ "垓",
113
+ "秭",
114
+ "穰",
115
+ "沟",
116
+ "涧",
117
+ "正",
118
+ "载",
119
+ "极",
120
+ ];
121
+ const digits = Math.floor(Math.log10(number) / 4); // 计算位数
122
+
123
+ // 不超过万位的数字直接返回
124
+ if (digits === 0) {
125
+ if (integer) {
126
+ return _join(number, suffix, plus);
127
+ } else {
128
+ return _join(number.toFixed(2), suffix, plus);
129
+ }
130
+ }
131
+
132
+ const dividedNumber = number / Math.pow(10000, digits);
133
+ const formattedNumber = dividedNumber.toFixed(2);
134
+
135
+ return _join(formattedNumber, units[digits] + suffix, plus);
136
+ }
137
+
138
+ /**
139
+ * 判断点是否在多边形内
140
+ * @param point - 待检测的点,包含 x 和 y 坐标
141
+ * @param polygon - 多边形的点集,数组形式,每个点包含 x 和 y 坐标
142
+ * @returns boolean - 点是否在多边形内
143
+ */
144
+ export function _IsPointInPolygon(point, polygon) {
145
+ let isInside = false;
146
+
147
+ const { x, y } = point;
148
+ const len = polygon.length;
149
+
150
+ for (let i = 0, j = len - 1; i < len; j = i++) {
151
+ const xi = polygon[i].x,
152
+ yi = polygon[i].y;
153
+ const xj = polygon[j].x,
154
+ yj = polygon[j].y;
155
+
156
+ const intersect =
157
+ yi > y !== yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi;
158
+
159
+ if (intersect) isInside = !isInside;
160
+ }
161
+
162
+ return isInside;
163
+ }
@@ -1,30 +1,11 @@
1
1
  /**
2
2
  * 滚动结束监听器
3
- * @param {(trigger?: "vertical" | "horizontal") => void} callback
3
+ * @param {(trigger: "vertical" | "horizontal") => void} callback
4
4
  */
5
5
  export function _ScrollEndListener(
6
- callback: (trigger?: "vertical" | "horizontal") => void
6
+ callback: (trigger: "vertical" | "horizontal") => void
7
7
  ): (payload: Event) => void;
8
8
 
9
- /**
10
- * 纯数字转 数字加单位
11
- * @param number 数字或字符串数字
12
- * @param config : {
13
- * join 拼接起来吗
14
- * suffix 后缀
15
- * integer 不超过万位的数字时保持整数吗
16
- * }
17
- * @returns 123456 --> 12.34万 | [ 12.34 , 万 ]
18
- */
19
- export function _FormatNumberWithUnit(
20
- number: string | number,
21
- config?: {
22
- join?: boolean;
23
- suffix?: string;
24
- integer?: boolean;
25
- }
26
- ): string | [number, string];
27
-
28
9
  /**
29
10
  * 设置量词属性
30
11
  * @param {*} data 需修改对象
@@ -44,82 +44,6 @@ export function _ScrollEndListener(callback) {
44
44
  };
45
45
  }
46
46
 
47
- /**
48
- * 纯数字转 数字加单位
49
- * @param number 数字或字符串数字
50
- * @param config : {
51
- * join 拼接起来吗
52
- * suffix 后缀
53
- * integer 不超过万位的数字时保持整数吗
54
- * }
55
- * @returns 123456 --> 12.34万 | [ 12.34 , 万 ]
56
- */
57
- export function _FormatNumberWithUnit(number, config = {}) {
58
- const { join, suffix, integer } = Object.assign(
59
- {
60
- join: true,
61
- suffix: "",
62
- integer: false,
63
- },
64
- config
65
- );
66
-
67
- function _join(value, suffix, plus = true) {
68
- value = (plus ? "" : "-") + value;
69
- if (join) return value + suffix;
70
- else return [value, suffix];
71
- }
72
-
73
- if (typeof number == "string") {
74
- if (!/^\d+$/.test(number.trim())) {
75
- console.error("错误输入:", number);
76
- return _join(0, suffix);
77
- }
78
- } else if (typeof number != "number") {
79
- console.error("错误输入:", number);
80
- return _join(0, suffix);
81
- }
82
-
83
- if (Math.abs(number) == Infinity || number == 0) {
84
- return _join(0, suffix);
85
- }
86
-
87
- number = Number(number);
88
- const plus = number >= 0;
89
- number = Math.abs(number);
90
-
91
- const units = [
92
- "",
93
- "万",
94
- "亿",
95
- "兆",
96
- "京",
97
- "垓",
98
- "秭",
99
- "穰",
100
- "沟",
101
- "涧",
102
- "正",
103
- "载",
104
- "极",
105
- ];
106
- const digits = Math.floor(Math.log10(number) / 4); // 计算位数
107
-
108
- // 不超过万位的数字直接返回
109
- if (digits === 0) {
110
- if (integer) {
111
- return _join(number, suffix, plus);
112
- } else {
113
- return _join(number.toFixed(2), suffix, plus);
114
- }
115
- }
116
-
117
- const dividedNumber = number / Math.pow(10000, digits);
118
- const formattedNumber = dividedNumber.toFixed(2);
119
-
120
- return _join(formattedNumber, units[digits] + suffix, plus);
121
- }
122
-
123
47
  /**
124
48
  * 设置量词属性
125
49
  * @param {*} data 需修改对象
@@ -1,4 +1,4 @@
1
- import { ExtractParameters } from "./Index";
1
+ import { ExtractParameters } from "../Index";
2
2
 
3
3
  /**
4
4
  * 非null | undefined判断
@@ -20,19 +20,6 @@ export function _IsObject(value: any): boolean;
20
20
  */
21
21
  export function _ExecuteWhenIdle(callback: Function);
22
22
 
23
- /**
24
- * 转为百分比字符串
25
- * @param value 分子
26
- * @param totalValue 分母
27
- * @param decimalPlaces 保留小数位
28
- * @returns 10.00%
29
- */
30
- export function _ConvertToPercentage(
31
- value: number,
32
- totalValue: number,
33
- decimalPlaces?: number
34
- ): number;
35
-
36
23
  /**
37
24
  * 等待条件满足
38
25
  * @param conditionChecker 条件检查器
@@ -84,19 +71,6 @@ export function _TimeTransition(
84
71
  pad?: boolean
85
72
  ): string;
86
73
 
87
- /**
88
- * 误差范围
89
- * @param value 需要判断的数字
90
- * @param target 目标数字
91
- * @param errorMargin 正负误差范围
92
- * @returns 是否在误差内
93
- */
94
- export function _IsWithinErrorMargin(
95
- value: number,
96
- target: number,
97
- errorMargin: number
98
- ): boolean;
99
-
100
74
  /**
101
75
  * 读取文件
102
76
  * @param src 文件地址
@@ -121,23 +95,6 @@ export function _GetFrameRate(
121
95
  referenceNode: number
122
96
  ): void;
123
97
 
124
- /**
125
- * 进度
126
- * @param {(schedule)=>void} callback callback( 进度百分比 )
127
- * @param {Number} TIME 总时长
128
- */
129
- export function _Schedule(
130
- callback: (schedule: number) => void,
131
- TIME: number
132
- ): void;
133
-
134
- /**
135
- * 格式化数字,给数字加上千位分隔符。
136
- * @param {number} number - 要格式化的数字。
137
- * @returns {string} - 格式化后的字符串。
138
- */
139
- export function _FormatNumber(number: number): string;
140
-
141
98
  /**
142
99
  * 单位转换 12** -> **px
143
100
  * @param {string} width
@@ -234,3 +191,26 @@ export function _CopyToClipboard(text: string): Promise<void>;
234
191
  * @returns string
235
192
  */
236
193
  export function _FormatFileSize(size: number): string;
194
+
195
+ /**
196
+ * 根据路径获取目标对象
197
+ * 该函数用于在给定的模型中,通过路径字符串来获取深层嵌套的目标对象如果路径中的某一部分不存在,则会创建一个新的对象(除非已经是路径的最后一部分)
198
+ *
199
+ * @param {Object} model - 包含要查询的数据的模型对象
200
+ * @param {string} path - 用点分隔的路径字符串,表示要访问的对象属性路径
201
+ * @returns {Object|undefined} - 返回目标对象,如果路径不存在则返回undefined
202
+ */
203
+ export function _GetTargetByPath(model: any, path: string): any;
204
+
205
+ /**
206
+ * 根据路径更新目标值
207
+ *
208
+ * 该函数通过一个点分隔的路径来更新一个对象中的嵌套属性值
209
+ * 它使用了reduce方法来遍历路径数组,并在路径的终点设置新的值
210
+ *
211
+ * @param {Object} model - 包含要更新数据的模型对象
212
+ * @param {string} path - 点分隔的字符串路径,指示如何到达目标属性
213
+ * @param {*} value - 要设置的新值
214
+ * @returns {*} - 返回更新后的模型对象中的值
215
+ */
216
+ export function _UpdateTargetByPath(model: any, path: string, value: any): any;
@@ -31,31 +31,6 @@ export function _ExecuteWhenIdle(callback) {
31
31
  requestIdleCallback(loop);
32
32
  }
33
33
 
34
- /**
35
- * 转为百分比字符串
36
- * @param value 分子
37
- * @param totalValue 分母
38
- * @param decimalPlaces 保留小数位
39
- * @returns 10.00%
40
- */
41
- export function _ConvertToPercentage(value, totalValue, decimalPlaces = 2) {
42
- if (
43
- typeof value !== "number" ||
44
- typeof totalValue !== "number" ||
45
- typeof decimalPlaces !== "number" ||
46
- totalValue == 0
47
- ) {
48
- console.error("异常输入:", arguments);
49
- return "0.00%";
50
- }
51
- return (
52
- Number(
53
- parseInt((value / totalValue) * Math.pow(10, 2 + decimalPlaces)) /
54
- Math.pow(10, decimalPlaces)
55
- ) || 0
56
- );
57
- }
58
-
59
34
  /**
60
35
  * 等待条件满足
61
36
  * @param conditionChecker 条件检查器
@@ -184,17 +159,6 @@ export function _TimeTransition(time, template, pad = true) {
184
159
  return template;
185
160
  }
186
161
 
187
- /**
188
- * 误差范围
189
- * @param value 需要判断的数字
190
- * @param target 目标数字
191
- * @param errorMargin 正负误差范围
192
- * @returns 是否在误差内
193
- */
194
- export function _IsWithinErrorMargin(value, target, errorMargin) {
195
- return Math.abs(value - target) <= errorMargin;
196
- }
197
-
198
162
  /**
199
163
  * 读取文件
200
164
  * @param src 文件地址
@@ -251,31 +215,6 @@ export function _GetFrameRate(callback, referenceNode = 10) {
251
215
  });
252
216
  }
253
217
 
254
- /**
255
- * 进度
256
- * @param {(schedule)=>void} callback callback( 进度百分比 )
257
- * @param {Number} TIME 总时长
258
- */
259
- export function _Schedule(callback, TIME = 500) {
260
- let t;
261
- function loop(time) {
262
- if (!t) t = time;
263
- let percentage = Math.min((time - t) / TIME, 1);
264
- callback(percentage);
265
- if (time - t < TIME) requestAnimationFrame(loop);
266
- }
267
- requestAnimationFrame(loop);
268
- }
269
-
270
- /**
271
- * 格式化数字,给数字加上千位分隔符。
272
- * @param {number} number - 要格式化的数字。
273
- * @returns {string} - 格式化后的字符串。
274
- */
275
- export function _FormatNumber(number) {
276
- return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
277
- }
278
-
279
218
  /**
280
219
  * 单位转换 12** -> **px
281
220
  * @param {string} width
@@ -486,3 +425,37 @@ export function _FormatFileSize(size) {
486
425
  }
487
426
  return `${Math.round(size * 100) / 100} ${units[unitIndex]}`;
488
427
  }
428
+
429
+ /**
430
+ * 根据路径获取目标对象
431
+ * 该函数用于在给定的模型中,通过路径字符串来获取深层嵌套的目标对象如果路径中的某一部分不存在,则会创建一个新的对象(除非已经是路径的最后一部分)
432
+ *
433
+ * @param {Object} model - 包含要查询的数据的模型对象
434
+ * @param {string} path - 用点分隔的路径字符串,表示要访问的对象属性路径
435
+ * @returns {Object|undefined} - 返回目标对象,如果路径不存在则返回undefined
436
+ */
437
+ export function _GetTargetByPath(model, path) {
438
+ const arr = path.split(".");
439
+ return arr.reduce((prev, curr, index) => {
440
+ if (prev.hasOwnProperty(curr)) return prev[curr];
441
+ return (prev[curr] = index == arr.length - 1 ? undefined : {});
442
+ }, model.value);
443
+ }
444
+ /**
445
+ * 根据路径更新目标值
446
+ *
447
+ * 该函数通过一个点分隔的路径来更新一个对象中的嵌套属性值
448
+ * 它使用了reduce方法来遍历路径数组,并在路径的终点设置新的值
449
+ *
450
+ * @param {Object} model - 包含要更新数据的模型对象
451
+ * @param {string} path - 点分隔的字符串路径,指示如何到达目标属性
452
+ * @param {*} value - 要设置的新值
453
+ * @returns {*} - 返回更新后的模型对象中的值
454
+ */
455
+ export function _UpdateTargetByPath(model, path, value) {
456
+ const arr = path.split(".");
457
+ return arr.reduce((prev, curr, index) => {
458
+ if (index === arr.length - 1) prev[curr] = value;
459
+ return prev[curr];
460
+ }, model.value);
461
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nhanh-pure-function",
3
- "version": "1.2.8",
3
+ "version": "1.3.1",
4
4
  "description": "纯函数工具",
5
5
  "main": "lib/Index.js",
6
6
  "scripts": {
package/lib/test.ts DELETED
@@ -1 +0,0 @@
1
- /** 用于测试 js / ts 功能实现及类型是否正确 */