lyb-js 1.6.33 → 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.
@@ -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
+ };
@@ -7,7 +7,10 @@ export const libJsPruneEmpty = (obj) => {
7
7
  acc[key] = value;
8
8
  return acc;
9
9
  }
10
- if (value && typeof value === "object" && !Array.isArray(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/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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lyb-js",
3
- "version": "1.6.33",
3
+ "version": "1.6.34",
4
4
  "description": "自用JS方法库",
5
5
  "license": "ISC",
6
6
  "type": "module",
@@ -1,2 +0,0 @@
1
- /** @description 判断是否为空值 */
2
- export declare const libIsNull: (value: any) => boolean;
package/Base/LibIsNull.js DELETED
@@ -1,2 +0,0 @@
1
- /** @description 判断是否为空值 */
2
- export const libIsNull = (value) => value === null || value === undefined || value === "";