lyb-js 1.6.33 → 1.6.36
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/Base/index.d.ts +4 -0
- package/Base/index.js +4 -0
- package/Browser/index.d.ts +10 -0
- package/Browser/index.js +10 -0
- package/Data/index.d.ts +8 -0
- package/Data/index.js +8 -0
- package/Data/libJsPickUnique.js +1 -1
- package/File/index.d.ts +3 -0
- package/File/index.js +3 -0
- package/Formatter/LibJsNumComma.d.ts +1 -6
- package/Formatter/LibJsNumComma.js +2 -8
- package/Formatter/LibJsNumberUnit.d.ts +1 -7
- package/Formatter/LibJsNumberUnit.js +5 -13
- package/Formatter/LibJsSecondsFormatterChinese.d.ts +1 -6
- package/Formatter/LibJsSecondsFormatterChinese.js +9 -14
- package/Formatter/index.d.ts +5 -0
- package/Formatter/index.js +5 -0
- package/Math/LibJsCalculateExpression.d.ts +1 -6
- package/Math/LibJsCalculateExpression.js +48 -65
- package/Math/index.d.ts +7 -0
- package/Math/index.js +7 -0
- package/Misc/LibJsClassObservable.js +1 -1
- package/Misc/LibJsDiffObject.d.ts +8 -0
- package/Misc/LibJsDiffObject.js +48 -0
- package/Misc/LibJsPruneEmpty.js +5 -2
- package/Misc/LibJsPullUpLoad.d.ts +1 -1
- package/Misc/LibJsPullUpLoad.js +1 -1
- package/Misc/index.d.ts +10 -0
- package/Misc/index.js +10 -0
- package/README.md +27 -2
- package/Random/index.d.ts +4 -0
- package/Random/index.js +4 -0
- package/Time/LibJsCountdown.d.ts +1 -1
- package/Time/LibJsCountdown.js +9 -12
- package/Time/index.d.ts +4 -0
- package/Time/index.js +4 -0
- package/index.d.ts +30 -0
- package/index.js +30 -0
- package/package.json +30 -7
- package/Base/LibIsNull.d.ts +0 -2
- package/Base/LibIsNull.js +0 -2
package/Base/index.d.ts
ADDED
package/Base/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from "./LibJsColorConsole.js";
|
|
2
|
+
export * from "./LibJsCopy.js";
|
|
3
|
+
export * from "./LibJsGetRowValue.js";
|
|
4
|
+
export * from "./LibJsIsMobile.js";
|
|
5
|
+
export * from "./LibJsIsPad.js";
|
|
6
|
+
export * from "./LibJsObjToUrlParams.js";
|
|
7
|
+
export * from "./libJsParseQueryString.js";
|
|
8
|
+
export * from "./LibJsPathParams.js";
|
|
9
|
+
export * from "./LibJsSetTitleIcon.js";
|
|
10
|
+
export * from "./LibJsTagTitleTip.js";
|
package/Browser/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from "./LibJsColorConsole.js";
|
|
2
|
+
export * from "./LibJsCopy.js";
|
|
3
|
+
export * from "./LibJsGetRowValue.js";
|
|
4
|
+
export * from "./LibJsIsMobile.js";
|
|
5
|
+
export * from "./LibJsIsPad.js";
|
|
6
|
+
export * from "./LibJsObjToUrlParams.js";
|
|
7
|
+
export * from "./libJsParseQueryString.js";
|
|
8
|
+
export * from "./LibJsPathParams.js";
|
|
9
|
+
export * from "./LibJsSetTitleIcon.js";
|
|
10
|
+
export * from "./LibJsTagTitleTip.js";
|
package/Data/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from "./LibJsChunkArray.js";
|
|
2
|
+
export * from "./LibJsDeepJSONParse.js";
|
|
3
|
+
export * from "./LibJsGroupArrayByKey.js";
|
|
4
|
+
export * from "./LibJsMatchEmail.js";
|
|
5
|
+
export * from "./libJsPickUnique.js";
|
|
6
|
+
export * from "./LibJsShuffleArray.js";
|
|
7
|
+
export * from "./LibJsStepArray.js";
|
|
8
|
+
export * from "./LibReverseArrayFromIndex.js";
|
package/Data/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from "./LibJsChunkArray.js";
|
|
2
|
+
export * from "./LibJsDeepJSONParse.js";
|
|
3
|
+
export * from "./LibJsGroupArrayByKey.js";
|
|
4
|
+
export * from "./LibJsMatchEmail.js";
|
|
5
|
+
export * from "./libJsPickUnique.js";
|
|
6
|
+
export * from "./LibJsShuffleArray.js";
|
|
7
|
+
export * from "./LibJsStepArray.js";
|
|
8
|
+
export * from "./LibReverseArrayFromIndex.js";
|
package/Data/libJsPickUnique.js
CHANGED
package/File/index.d.ts
ADDED
package/File/index.js
ADDED
|
@@ -1,7 +1,2 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @description 数字每三位添加逗号(不四舍五入)
|
|
3
|
-
* @param num 需要格式化的数字
|
|
4
|
-
* @param reserve 保留小数位数
|
|
5
|
-
* @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsNumComma-数字逗号
|
|
6
|
-
*/
|
|
1
|
+
/** @description 数字每三位添加逗号,不四舍五入 */
|
|
7
2
|
export declare const libJsNumComma: (num: number, reserve?: number) => string;
|
|
@@ -1,13 +1,7 @@
|
|
|
1
|
-
import Decimal from "decimal.js";
|
|
2
|
-
/**
|
|
3
|
-
* @description 数字每三位添加逗号(不四舍五入)
|
|
4
|
-
* @param num 需要格式化的数字
|
|
5
|
-
* @param reserve 保留小数位数
|
|
6
|
-
* @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsNumComma-数字逗号
|
|
7
|
-
*/
|
|
1
|
+
import { Decimal } from "decimal.js";
|
|
2
|
+
/** @description 数字每三位添加逗号,不四舍五入 */
|
|
8
3
|
export const libJsNumComma = (num, reserve = 2) => {
|
|
9
4
|
const decimal = new Decimal(num);
|
|
10
|
-
// 截断小数,不四舍五入
|
|
11
5
|
const str = decimal.toFixed(reserve, Decimal.ROUND_DOWN);
|
|
12
6
|
const reg = str.includes(".") ? /(\d)(?=(\d{3})+\.)/g : /(\d)(?=(?:\d{3})+$)/g;
|
|
13
7
|
return str.replace(reg, "$1,");
|
|
@@ -1,8 +1,2 @@
|
|
|
1
|
-
/** @description
|
|
2
|
-
* @param num 数字
|
|
3
|
-
* @param units 单位组,key为单位,value为格式化阈值
|
|
4
|
-
* @param retain 保留几位小数
|
|
5
|
-
* @returns 数字+单位
|
|
6
|
-
* @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsNumberUnit-数字单位
|
|
7
|
-
*/
|
|
1
|
+
/** @description 将大于或等于单位组属性值的数值,格式化为对应单位 */
|
|
8
2
|
export declare const libJsNumberUnit: (num: number, units: Record<string, number>, retain?: number) => string;
|
|
@@ -1,12 +1,6 @@
|
|
|
1
|
-
import Decimal from "decimal.js";
|
|
2
|
-
import { libJsNumComma } from
|
|
3
|
-
/** @description
|
|
4
|
-
* @param num 数字
|
|
5
|
-
* @param units 单位组,key为单位,value为格式化阈值
|
|
6
|
-
* @param retain 保留几位小数
|
|
7
|
-
* @returns 数字+单位
|
|
8
|
-
* @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsNumberUnit-数字单位
|
|
9
|
-
*/
|
|
1
|
+
import { Decimal } from "decimal.js";
|
|
2
|
+
import { libJsNumComma } from "./LibJsNumComma.js";
|
|
3
|
+
/** @description 将大于或等于单位组属性值的数值,格式化为对应单位 */
|
|
10
4
|
export const libJsNumberUnit = (num, units, retain = 0) => {
|
|
11
5
|
const decimalValue = new Decimal(num);
|
|
12
6
|
const sortedUnits = Object.entries(units).sort(([, a], [, b]) => b - a);
|
|
@@ -14,10 +8,8 @@ export const libJsNumberUnit = (num, units, retain = 0) => {
|
|
|
14
8
|
const decimalThreshold = new Decimal(threshold);
|
|
15
9
|
if (decimalValue.greaterThanOrEqualTo(decimalThreshold)) {
|
|
16
10
|
const formattedValue = decimalValue.dividedBy(decimalThreshold);
|
|
17
|
-
|
|
18
|
-
return v + unit;
|
|
11
|
+
return `${libJsNumComma(Number(formattedValue), retain)}${unit}`;
|
|
19
12
|
}
|
|
20
13
|
}
|
|
21
|
-
|
|
22
|
-
return v;
|
|
14
|
+
return libJsNumComma(Number(decimalValue), retain);
|
|
23
15
|
};
|
|
@@ -1,7 +1,2 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @description 将秒数格式化为中文时间描述,支持扩展到年
|
|
3
|
-
* @param seconds 秒数
|
|
4
|
-
* @returns 格式化后的中文时间
|
|
5
|
-
* @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsSecondsFormatterChinese-中文时间
|
|
6
|
-
*/
|
|
1
|
+
/** @description 将秒数格式化为中文时间描述 */
|
|
7
2
|
export declare const libJsSecondsFormatterChinese: (seconds: number) => string;
|
|
@@ -1,20 +1,15 @@
|
|
|
1
1
|
import dayjs from "dayjs";
|
|
2
|
-
import duration from "dayjs/plugin/duration";
|
|
2
|
+
import duration from "dayjs/plugin/duration.js";
|
|
3
3
|
dayjs.extend(duration);
|
|
4
|
-
/**
|
|
5
|
-
* @description 将秒数格式化为中文时间描述,支持扩展到年
|
|
6
|
-
* @param seconds 秒数
|
|
7
|
-
* @returns 格式化后的中文时间
|
|
8
|
-
* @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsSecondsFormatterChinese-中文时间
|
|
9
|
-
*/
|
|
4
|
+
/** @description 将秒数格式化为中文时间描述 */
|
|
10
5
|
export const libJsSecondsFormatterChinese = (seconds) => {
|
|
11
|
-
const
|
|
12
|
-
const years = Math.floor(
|
|
13
|
-
const months = Math.floor(
|
|
14
|
-
const days = Math.floor(
|
|
15
|
-
const hours =
|
|
16
|
-
const minutes =
|
|
17
|
-
const remainingSeconds =
|
|
6
|
+
const currentDuration = dayjs.duration(seconds, "seconds");
|
|
7
|
+
const years = Math.floor(currentDuration.asYears());
|
|
8
|
+
const months = Math.floor(currentDuration.asMonths() % 12);
|
|
9
|
+
const days = Math.floor(currentDuration.asDays() % 30);
|
|
10
|
+
const hours = currentDuration.hours();
|
|
11
|
+
const minutes = currentDuration.minutes();
|
|
12
|
+
const remainingSeconds = currentDuration.seconds();
|
|
18
13
|
const timeParts = [];
|
|
19
14
|
if (years > 0) {
|
|
20
15
|
timeParts.push(`${years}年`);
|
|
@@ -1,7 +1,2 @@
|
|
|
1
|
-
/** @description 计算表达式字符串
|
|
2
|
-
* @param expression 表达式字符串
|
|
3
|
-
* @param point 小数点精度
|
|
4
|
-
* @returns 计算结果
|
|
5
|
-
* @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsCalculateExpression-表达式字符串
|
|
6
|
-
*/
|
|
1
|
+
/** @description 计算表达式字符串 */
|
|
7
2
|
export declare const libJsCalculateExpression: (expression: string, point?: number) => number;
|
|
@@ -1,107 +1,90 @@
|
|
|
1
|
-
import Decimal from "decimal.js";
|
|
2
|
-
/** @description 计算表达式字符串
|
|
3
|
-
* @param expression 表达式字符串
|
|
4
|
-
* @param point 小数点精度
|
|
5
|
-
* @returns 计算结果
|
|
6
|
-
* @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsCalculateExpression-表达式字符串
|
|
7
|
-
*/
|
|
1
|
+
import { Decimal } from "decimal.js";
|
|
2
|
+
/** @description 计算表达式字符串 */
|
|
8
3
|
export const libJsCalculateExpression = (expression, point = 2) => {
|
|
9
|
-
//清除所有空格
|
|
10
4
|
expression = expression.replace(/\s+/g, "");
|
|
11
|
-
//支持的运算符和优先级
|
|
12
5
|
const operators = {
|
|
13
6
|
"+": 1,
|
|
14
7
|
"-": 1,
|
|
15
8
|
"*": 2,
|
|
16
9
|
"/": 2,
|
|
17
10
|
};
|
|
18
|
-
//支持的小数点精度
|
|
19
11
|
const toDecimal = (value) => new Decimal(value);
|
|
20
|
-
//判断字符是否是运算符
|
|
21
12
|
const isOperator = (char) => ["+", "-", "*", "/"].includes(char);
|
|
22
|
-
//判断字符是否是数字(包括小数点)
|
|
23
13
|
const isNumber = (char) => /[0-9.]/.test(char);
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const char = expression[i];
|
|
14
|
+
const evaluate = (inputExpression) => {
|
|
15
|
+
const outputQueue = [];
|
|
16
|
+
const operatorStack = [];
|
|
17
|
+
let index = 0;
|
|
18
|
+
while (index < inputExpression.length) {
|
|
19
|
+
const char = inputExpression[index];
|
|
31
20
|
if (isNumber(char)) {
|
|
32
21
|
let numStr = "";
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
i++;
|
|
22
|
+
while (index < inputExpression.length && isNumber(inputExpression[index])) {
|
|
23
|
+
numStr += inputExpression[index];
|
|
24
|
+
index++;
|
|
37
25
|
}
|
|
38
26
|
outputQueue.push(toDecimal(numStr));
|
|
27
|
+
continue;
|
|
39
28
|
}
|
|
40
|
-
|
|
29
|
+
if (char === "(") {
|
|
41
30
|
operatorStack.push(char);
|
|
42
|
-
|
|
31
|
+
index++;
|
|
32
|
+
continue;
|
|
43
33
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
while (operatorStack.length > 0 &&
|
|
47
|
-
operatorStack[operatorStack.length - 1] !== "(") {
|
|
34
|
+
if (char === ")") {
|
|
35
|
+
while (operatorStack.length > 0 && operatorStack[operatorStack.length - 1] !== "(") {
|
|
48
36
|
outputQueue.push(operatorStack.pop());
|
|
49
37
|
}
|
|
50
|
-
operatorStack.pop();
|
|
51
|
-
|
|
38
|
+
operatorStack.pop();
|
|
39
|
+
index++;
|
|
40
|
+
continue;
|
|
52
41
|
}
|
|
53
|
-
|
|
54
|
-
//运算符
|
|
42
|
+
if (isOperator(char)) {
|
|
55
43
|
while (operatorStack.length > 0 &&
|
|
56
44
|
operators[operatorStack[operatorStack.length - 1]] >= operators[char]) {
|
|
57
45
|
outputQueue.push(operatorStack.pop());
|
|
58
46
|
}
|
|
59
47
|
operatorStack.push(char);
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
else {
|
|
63
|
-
throw new Error(`无效字符: ${char}`);
|
|
48
|
+
index++;
|
|
49
|
+
continue;
|
|
64
50
|
}
|
|
51
|
+
throw new Error(`无效字符: ${char}`);
|
|
65
52
|
}
|
|
66
|
-
//把所有剩余的操作符添加到输出队列
|
|
67
53
|
while (operatorStack.length > 0) {
|
|
68
54
|
outputQueue.push(operatorStack.pop());
|
|
69
55
|
}
|
|
70
|
-
//执行运算
|
|
71
56
|
const calcStack = [];
|
|
72
|
-
for (
|
|
73
|
-
if (typeof token
|
|
74
|
-
const b = calcStack.pop();
|
|
75
|
-
const a = calcStack.pop();
|
|
76
|
-
switch (token) {
|
|
77
|
-
case "+":
|
|
78
|
-
calcStack.push(a.add(b));
|
|
79
|
-
break;
|
|
80
|
-
case "-":
|
|
81
|
-
calcStack.push(a.sub(b));
|
|
82
|
-
break;
|
|
83
|
-
case "*":
|
|
84
|
-
calcStack.push(a.mul(b));
|
|
85
|
-
break;
|
|
86
|
-
case "/":
|
|
87
|
-
if (b.eq(0))
|
|
88
|
-
throw new Error("除数不能为零");
|
|
89
|
-
calcStack.push(a.div(b));
|
|
90
|
-
break;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
57
|
+
for (const token of outputQueue) {
|
|
58
|
+
if (typeof token !== "string") {
|
|
94
59
|
calcStack.push(token);
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
const b = calcStack.pop();
|
|
63
|
+
const a = calcStack.pop();
|
|
64
|
+
switch (token) {
|
|
65
|
+
case "+":
|
|
66
|
+
calcStack.push(a.add(b));
|
|
67
|
+
break;
|
|
68
|
+
case "-":
|
|
69
|
+
calcStack.push(a.sub(b));
|
|
70
|
+
break;
|
|
71
|
+
case "*":
|
|
72
|
+
calcStack.push(a.mul(b));
|
|
73
|
+
break;
|
|
74
|
+
case "/":
|
|
75
|
+
if (b.eq(0)) {
|
|
76
|
+
throw new Error("除数不能为零");
|
|
77
|
+
}
|
|
78
|
+
calcStack.push(a.div(b));
|
|
79
|
+
break;
|
|
95
80
|
}
|
|
96
81
|
}
|
|
97
82
|
return calcStack.pop();
|
|
98
83
|
};
|
|
99
84
|
try {
|
|
100
|
-
|
|
101
|
-
const result = evaluate(expression);
|
|
102
|
-
return Number(result.toFixed(point, Decimal.ROUND_DOWN)); //保留指定的小数位数
|
|
85
|
+
return Number(evaluate(expression).toFixed(point, Decimal.ROUND_DOWN));
|
|
103
86
|
}
|
|
104
87
|
catch (error) {
|
|
105
|
-
throw new Error(
|
|
88
|
+
throw new Error(`表达式计算失败:${error.message}`);
|
|
106
89
|
}
|
|
107
90
|
};
|
package/Math/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from "./LibJsCalculateExpression.js";
|
|
2
|
+
export * from "./LibJsConvertAngle.js";
|
|
3
|
+
export * from "./LibJsCoordsAngle.js";
|
|
4
|
+
export * from "./LibJsCoordsDistance.js";
|
|
5
|
+
export * from "./LibJsDecimal.js";
|
|
6
|
+
export * from "./LibJsLerp.js";
|
|
7
|
+
export * from "./LibJsNormalizeInRange.js";
|
package/Math/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from "./LibJsCalculateExpression.js";
|
|
2
|
+
export * from "./LibJsConvertAngle.js";
|
|
3
|
+
export * from "./LibJsCoordsAngle.js";
|
|
4
|
+
export * from "./LibJsCoordsDistance.js";
|
|
5
|
+
export * from "./LibJsDecimal.js";
|
|
6
|
+
export * from "./LibJsLerp.js";
|
|
7
|
+
export * from "./LibJsNormalizeInRange.js";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** @description 筛出已修改的值 */
|
|
2
|
+
/**
|
|
3
|
+
* @description 深度对比对象差异(只返回新对象中发生变更的字段)
|
|
4
|
+
* - 基础类型:值不同则返回新值
|
|
5
|
+
* - 对象:递归 diff
|
|
6
|
+
* - 数组:不做 diff,值不同直接返回新数组
|
|
7
|
+
*/
|
|
8
|
+
export declare const libDiffObject: <T extends Record<string, any>>(oldObj: T, newObj: T) => Partial<Record<keyof T, any>>;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/** @description 筛出已修改的值 */
|
|
2
|
+
/**
|
|
3
|
+
* @description 深度对比对象差异(只返回新对象中发生变更的字段)
|
|
4
|
+
* - 基础类型:值不同则返回新值
|
|
5
|
+
* - 对象:递归 diff
|
|
6
|
+
* - 数组:不做 diff,值不同直接返回新数组
|
|
7
|
+
*/
|
|
8
|
+
export const libDiffObject = (oldObj, newObj) => {
|
|
9
|
+
const result = {};
|
|
10
|
+
const isObject = (val) => val !== null && typeof val === "object" && !Array.isArray(val);
|
|
11
|
+
Object.keys(newObj).forEach((key) => {
|
|
12
|
+
const oldVal = oldObj?.[key];
|
|
13
|
+
const newVal = newObj[key];
|
|
14
|
+
// 数组:不递归,值不同直接返回
|
|
15
|
+
if (Array.isArray(newVal)) {
|
|
16
|
+
if (!Array.isArray(oldVal) || oldVal.length !== newVal.length) {
|
|
17
|
+
result[key] = newVal;
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const changed = newVal.some((item, index) => {
|
|
21
|
+
const oldItem = oldVal[index];
|
|
22
|
+
// 对象递归
|
|
23
|
+
if (item && typeof item === "object" && !Array.isArray(item)) {
|
|
24
|
+
return Object.keys(libDiffObject(oldItem || {}, item)).length > 0;
|
|
25
|
+
}
|
|
26
|
+
// 基础类型
|
|
27
|
+
return oldItem !== item;
|
|
28
|
+
});
|
|
29
|
+
if (changed) {
|
|
30
|
+
result[key] = newVal;
|
|
31
|
+
}
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
// 对象:递归 diff
|
|
35
|
+
if (isObject(newVal)) {
|
|
36
|
+
const childDiff = libDiffObject(oldVal || {}, newVal);
|
|
37
|
+
if (Object.keys(childDiff).length > 0) {
|
|
38
|
+
result[key] = childDiff;
|
|
39
|
+
}
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
// 基础类型
|
|
43
|
+
if (oldVal !== newVal) {
|
|
44
|
+
result[key] = newVal;
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
return result;
|
|
48
|
+
};
|
package/Misc/LibJsPruneEmpty.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { libJsIsNull } from "../Base/LibJsIsNull";
|
|
1
|
+
import { libJsIsNull } from "../Base/LibJsIsNull.js";
|
|
2
2
|
/** @description 去掉对象内的空值 */
|
|
3
3
|
export const libJsPruneEmpty = (obj) => {
|
|
4
4
|
const result = Object.entries(obj).reduce((acc, [key, value]) => {
|
|
@@ -7,7 +7,10 @@ export const libJsPruneEmpty = (obj) => {
|
|
|
7
7
|
acc[key] = value;
|
|
8
8
|
return acc;
|
|
9
9
|
}
|
|
10
|
-
if (value &&
|
|
10
|
+
if (value &&
|
|
11
|
+
typeof value === "object" &&
|
|
12
|
+
!Array.isArray(value) &&
|
|
13
|
+
!(value instanceof Date)) {
|
|
11
14
|
const next = libJsPruneEmpty(value);
|
|
12
15
|
if (next)
|
|
13
16
|
acc[key] = next;
|
package/Misc/LibJsPullUpLoad.js
CHANGED
package/Misc/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from "./LibJsClassObservable.js";
|
|
2
|
+
export * from "./LibJsDiffObject.js";
|
|
3
|
+
export * from "./LibJsEmitter.js";
|
|
4
|
+
export * from "./LibJsEmitterClose.js";
|
|
5
|
+
export * from "./LibJsHorizontal.js";
|
|
6
|
+
export * from "./LibJsNumberStepper.js";
|
|
7
|
+
export * from "./LibJsPruneEmpty.js";
|
|
8
|
+
export * from "./LibJsPullUpLoad.js";
|
|
9
|
+
export * from "./LibJsRegFormValidate.js";
|
|
10
|
+
export * from "./LibJsRetryRequest.js";
|
package/Misc/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from "./LibJsClassObservable.js";
|
|
2
|
+
export * from "./LibJsDiffObject.js";
|
|
3
|
+
export * from "./LibJsEmitter.js";
|
|
4
|
+
export * from "./LibJsEmitterClose.js";
|
|
5
|
+
export * from "./LibJsHorizontal.js";
|
|
6
|
+
export * from "./LibJsNumberStepper.js";
|
|
7
|
+
export * from "./LibJsPruneEmpty.js";
|
|
8
|
+
export * from "./LibJsPullUpLoad.js";
|
|
9
|
+
export * from "./LibJsRegFormValidate.js";
|
|
10
|
+
export * from "./LibJsRetryRequest.js";
|
package/README.md
CHANGED
|
@@ -155,6 +155,7 @@ console.log(libJsGetRowValue({ user: { name: "Tom" } }, "user.name")); // "Tom"
|
|
|
155
155
|
- [LibJsEmitterClose-一次性关闭监听](#libjsemitterclose-一次性关闭监听)
|
|
156
156
|
- [LibJsHorizontal-游戏横版状态](#libjshorizontal-游戏横版状态)
|
|
157
157
|
- [LibJsNumberStepper-数字步进器](#libjsnumberstepper-数字步进器)
|
|
158
|
+
- [LibJsDiffObject-筛出已修改的值](#libjsdiffobject-筛出已修改的值)
|
|
158
159
|
- [LibJsPruneEmpty-对象属性去空值](#libjspruneempty-对象属性去空值)
|
|
159
160
|
- [LibJsPullUpLoad-上拉加载](#libjspullupload-上拉加载)
|
|
160
161
|
- [LibJsRegFormValidate-表单验证](#libjsregformvalidate-表单验证)
|
|
@@ -764,15 +765,39 @@ addButton.addEventListener("pointerdown", () => stepper.down("add"));
|
|
|
764
765
|
subButton.addEventListener("pointerdown", () => stepper.down("sub"));
|
|
765
766
|
```
|
|
766
767
|
|
|
768
|
+
### LibJsDiffObject-筛出已修改的值
|
|
769
|
+
|
|
770
|
+
深度对比对象差异,只返回新对象中发生变更的字段。
|
|
771
|
+
|
|
772
|
+
```ts
|
|
773
|
+
import { libDiffObject } from "lyb-js/Misc/LibJsDiffObject";
|
|
774
|
+
|
|
775
|
+
const oldObj = {
|
|
776
|
+
name: "Tom",
|
|
777
|
+
profile: { city: "Shanghai", age: 18 },
|
|
778
|
+
tags: [{ id: 1 }, { id: 2 }],
|
|
779
|
+
};
|
|
780
|
+
|
|
781
|
+
const newObj = {
|
|
782
|
+
name: "Jerry",
|
|
783
|
+
profile: { city: "Shanghai", age: 20 },
|
|
784
|
+
tags: [{ id: 1 }, { id: 3 }],
|
|
785
|
+
};
|
|
786
|
+
|
|
787
|
+
console.log(libDiffObject(oldObj, newObj));
|
|
788
|
+
// { name: "Jerry", profile: { age: 20 }, tags: [{ id: 1 }, { id: 3 }] }
|
|
789
|
+
```
|
|
790
|
+
|
|
767
791
|
### LibJsPruneEmpty-对象属性去空值
|
|
768
792
|
|
|
769
|
-
递归移除对象中的空字符串、`null`、`undefined`
|
|
793
|
+
递归移除对象中的空字符串、`null`、`undefined` 和空对象;空数组也会被去掉。`Date` 会保留原值,不会被递归处理。
|
|
770
794
|
|
|
771
795
|
```ts
|
|
772
796
|
import { libJsPruneEmpty } from "lyb-js/Misc/LibJsPruneEmpty";
|
|
773
797
|
|
|
774
798
|
const result = libJsPruneEmpty({
|
|
775
799
|
name: "Tom",
|
|
800
|
+
createdAt: new Date("2026-05-07T00:00:00.000Z"),
|
|
776
801
|
empty: "",
|
|
777
802
|
profile: {
|
|
778
803
|
age: undefined,
|
|
@@ -782,7 +807,7 @@ const result = libJsPruneEmpty({
|
|
|
782
807
|
});
|
|
783
808
|
|
|
784
809
|
console.log(result);
|
|
785
|
-
// { name: "Tom", profile: { city: "Shanghai" } }
|
|
810
|
+
// { name: "Tom", createdAt: 2026-05-07T00:00:00.000Z, profile: { city: "Shanghai" } }
|
|
786
811
|
```
|
|
787
812
|
|
|
788
813
|
### LibJsPullUpLoad-上拉加载
|
package/Random/index.js
ADDED
package/Time/LibJsCountdown.d.ts
CHANGED
package/Time/LibJsCountdown.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import dayjs from "dayjs";
|
|
2
|
-
import duration from "dayjs/plugin/duration";
|
|
2
|
+
import duration from "dayjs/plugin/duration.js";
|
|
3
3
|
dayjs.extend(duration);
|
|
4
|
-
/** @description 倒计时,keepUnit
|
|
4
|
+
/** @description 倒计时,keepUnit 为保留单位时不再使用更高单位进位 */
|
|
5
5
|
export const libJsCountdown = (endTime, keepUnit) => {
|
|
6
6
|
const startTime = dayjs();
|
|
7
7
|
const diff = dayjs(endTime).diff(startTime);
|
|
8
8
|
const time = dayjs.duration(diff);
|
|
9
|
-
const pad = (
|
|
9
|
+
const pad = (num) => num.toString().padStart(2, "0");
|
|
10
10
|
if (diff <= 0) {
|
|
11
11
|
return {
|
|
12
12
|
years: "00",
|
|
@@ -18,24 +18,21 @@ export const libJsCountdown = (endTime, keepUnit) => {
|
|
|
18
18
|
ended: true,
|
|
19
19
|
};
|
|
20
20
|
}
|
|
21
|
-
// 先获取原始值
|
|
22
21
|
const years = time.years();
|
|
23
22
|
const months = time.months();
|
|
24
23
|
const days = time.days();
|
|
25
24
|
let hours = time.hours();
|
|
26
25
|
let minutes = time.minutes();
|
|
27
26
|
let seconds = time.seconds();
|
|
28
|
-
|
|
29
|
-
const totalSec = time.asSeconds();
|
|
30
|
-
// 根据保留单位展开
|
|
27
|
+
const totalSeconds = time.asSeconds();
|
|
31
28
|
if (keepUnit === "hour") {
|
|
32
|
-
hours = Math.floor(
|
|
33
|
-
minutes = Math.floor((
|
|
34
|
-
seconds = Math.floor(
|
|
29
|
+
hours = Math.floor(totalSeconds / 3600);
|
|
30
|
+
minutes = Math.floor((totalSeconds % 3600) / 60);
|
|
31
|
+
seconds = Math.floor(totalSeconds % 60);
|
|
35
32
|
}
|
|
36
33
|
else if (keepUnit === "minute") {
|
|
37
|
-
minutes = Math.floor(
|
|
38
|
-
seconds = Math.floor(
|
|
34
|
+
minutes = Math.floor(totalSeconds / 60);
|
|
35
|
+
seconds = Math.floor(totalSeconds % 60);
|
|
39
36
|
}
|
|
40
37
|
return {
|
|
41
38
|
years: pad(years),
|
package/Time/index.d.ts
ADDED
package/Time/index.js
ADDED
package/index.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import * as Base from "./Base/index.js";
|
|
2
|
+
import * as Browser from "./Browser/index.js";
|
|
3
|
+
import * as Data from "./Data/index.js";
|
|
4
|
+
import * as File from "./File/index.js";
|
|
5
|
+
import * as Formatter from "./Formatter/index.js";
|
|
6
|
+
import * as Math from "./Math/index.js";
|
|
7
|
+
import * as Misc from "./Misc/index.js";
|
|
8
|
+
import * as Random from "./Random/index.js";
|
|
9
|
+
import * as Time from "./Time/index.js";
|
|
10
|
+
export * from "./Base/index.js";
|
|
11
|
+
export * from "./Browser/index.js";
|
|
12
|
+
export * from "./Data/index.js";
|
|
13
|
+
export * from "./File/index.js";
|
|
14
|
+
export * from "./Formatter/index.js";
|
|
15
|
+
export * from "./Math/index.js";
|
|
16
|
+
export * from "./Misc/index.js";
|
|
17
|
+
export * from "./Random/index.js";
|
|
18
|
+
export * from "./Time/index.js";
|
|
19
|
+
export { Base, Browser, Data, File, Formatter, Math, Misc, Random, Time };
|
|
20
|
+
export declare const LibJs: {
|
|
21
|
+
Base: typeof Base;
|
|
22
|
+
Browser: typeof Browser;
|
|
23
|
+
Data: typeof Data;
|
|
24
|
+
File: typeof File;
|
|
25
|
+
Formatter: typeof Formatter;
|
|
26
|
+
Math: typeof Math;
|
|
27
|
+
Misc: typeof Misc;
|
|
28
|
+
Random: typeof Random;
|
|
29
|
+
Time: typeof Time;
|
|
30
|
+
};
|
package/index.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import * as Base from "./Base/index.js";
|
|
2
|
+
import * as Browser from "./Browser/index.js";
|
|
3
|
+
import * as Data from "./Data/index.js";
|
|
4
|
+
import * as File from "./File/index.js";
|
|
5
|
+
import * as Formatter from "./Formatter/index.js";
|
|
6
|
+
import * as Math from "./Math/index.js";
|
|
7
|
+
import * as Misc from "./Misc/index.js";
|
|
8
|
+
import * as Random from "./Random/index.js";
|
|
9
|
+
import * as Time from "./Time/index.js";
|
|
10
|
+
export * from "./Base/index.js";
|
|
11
|
+
export * from "./Browser/index.js";
|
|
12
|
+
export * from "./Data/index.js";
|
|
13
|
+
export * from "./File/index.js";
|
|
14
|
+
export * from "./Formatter/index.js";
|
|
15
|
+
export * from "./Math/index.js";
|
|
16
|
+
export * from "./Misc/index.js";
|
|
17
|
+
export * from "./Random/index.js";
|
|
18
|
+
export * from "./Time/index.js";
|
|
19
|
+
export { Base, Browser, Data, File, Formatter, Math, Misc, Random, Time };
|
|
20
|
+
export const LibJs = {
|
|
21
|
+
Base,
|
|
22
|
+
Browser,
|
|
23
|
+
Data,
|
|
24
|
+
File,
|
|
25
|
+
Formatter,
|
|
26
|
+
Math,
|
|
27
|
+
Misc,
|
|
28
|
+
Random,
|
|
29
|
+
Time,
|
|
30
|
+
};
|
package/package.json
CHANGED
|
@@ -1,25 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lyb-js",
|
|
3
|
-
"version": "1.6.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.6.36",
|
|
4
|
+
"description": "JavaScript utility library",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"types": "./index.d.ts",
|
|
8
8
|
"main": "./index.js",
|
|
9
|
+
"module": "./index.js",
|
|
10
|
+
"files": [
|
|
11
|
+
"*.js",
|
|
12
|
+
"*.d.ts",
|
|
13
|
+
"Base",
|
|
14
|
+
"Browser",
|
|
15
|
+
"Data",
|
|
16
|
+
"File",
|
|
17
|
+
"Formatter",
|
|
18
|
+
"Math",
|
|
19
|
+
"Misc",
|
|
20
|
+
"Random",
|
|
21
|
+
"Time",
|
|
22
|
+
"README.md"
|
|
23
|
+
],
|
|
9
24
|
"exports": {
|
|
10
25
|
".": "./index.js",
|
|
26
|
+
"./Base": "./Base/index.js",
|
|
11
27
|
"./Base/*": "./Base/*",
|
|
28
|
+
"./Browser": "./Browser/index.js",
|
|
12
29
|
"./Browser/*": "./Browser/*",
|
|
30
|
+
"./Data": "./Data/index.js",
|
|
13
31
|
"./Data/*": "./Data/*",
|
|
32
|
+
"./File": "./File/index.js",
|
|
14
33
|
"./File/*": "./File/*",
|
|
34
|
+
"./Formatter": "./Formatter/index.js",
|
|
15
35
|
"./Formatter/*": "./Formatter/*",
|
|
36
|
+
"./Math": "./Math/index.js",
|
|
16
37
|
"./Math/*": "./Math/*",
|
|
38
|
+
"./Misc": "./Misc/index.js",
|
|
17
39
|
"./Misc/*": "./Misc/*",
|
|
40
|
+
"./Random": "./Random/index.js",
|
|
18
41
|
"./Random/*": "./Random/*",
|
|
42
|
+
"./Time": "./Time/index.js",
|
|
19
43
|
"./Time/*": "./Time/*"
|
|
20
44
|
},
|
|
21
45
|
"author": {
|
|
22
|
-
"name": "
|
|
46
|
+
"name": "lengyibai",
|
|
23
47
|
"email": "1329670984@qq.com"
|
|
24
48
|
},
|
|
25
49
|
"repository": {
|
|
@@ -27,14 +51,13 @@
|
|
|
27
51
|
"url": "git+https://github.com/lengyibai/lyb-js.git"
|
|
28
52
|
},
|
|
29
53
|
"keywords": [
|
|
30
|
-
"冷弋白",
|
|
31
54
|
"lengyibai",
|
|
32
55
|
"lyb",
|
|
33
|
-
"lyb-js"
|
|
56
|
+
"lyb-js",
|
|
57
|
+
"javascript"
|
|
34
58
|
],
|
|
35
59
|
"dependencies": {
|
|
36
60
|
"dayjs": "^1.11.13",
|
|
37
|
-
"decimal.js": "^10.4.3"
|
|
38
|
-
"vite": "^4.5.5"
|
|
61
|
+
"decimal.js": "^10.4.3"
|
|
39
62
|
}
|
|
40
63
|
}
|
package/Base/LibIsNull.d.ts
DELETED
package/Base/LibIsNull.js
DELETED