lyb-js 1.6.32 → 1.6.34
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/Browser/LibJsGetRowValue.d.ts +6 -0
- package/Browser/LibJsGetRowValue.js +10 -0
- package/Misc/LibJsDiffObject.d.ts +8 -0
- package/Misc/LibJsDiffObject.js +48 -0
- package/Misc/LibJsPruneEmpty.js +4 -1
- package/README.md +989 -806
- package/package.json +1 -1
- package/Base/LibIsNull.d.ts +0 -2
- package/Base/LibIsNull.js +0 -2
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** @description 按点路径获取对象值
|
|
2
|
+
* @param row 数据对象
|
|
3
|
+
* @param prop 点路径属性
|
|
4
|
+
* @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsGetRowValue-按点路径获取对象值
|
|
5
|
+
*/
|
|
6
|
+
export const libJsGetRowValue = (row, prop) => {
|
|
7
|
+
return prop
|
|
8
|
+
.split(".")
|
|
9
|
+
.reduce((currentValue, key) => currentValue?.[key], row);
|
|
10
|
+
};
|
|
@@ -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
|
@@ -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;
|