json-diff-ts 3.0.0-beta.1 → 3.0.0-beta.2

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.
@@ -1,12 +1,12 @@
1
1
  type FunctionKey = (obj: any, shouldReturnKeyName?: boolean) => any;
2
- export type EmbeddedObjKeysType = Record<string, string | FunctionKey>;
3
- export type EmbeddedObjKeysMapType = Map<string | RegExp, string | FunctionKey>;
4
- export declare enum Operation {
2
+ type EmbeddedObjKeysType = Record<string, string | FunctionKey>;
3
+ type EmbeddedObjKeysMapType = Map<string | RegExp, string | FunctionKey>;
4
+ declare enum Operation {
5
5
  REMOVE = "REMOVE",
6
6
  ADD = "ADD",
7
7
  UPDATE = "UPDATE"
8
8
  }
9
- export interface IChange {
9
+ interface IChange {
10
10
  type: Operation;
11
11
  key: string;
12
12
  embeddedKey?: string | FunctionKey;
@@ -14,8 +14,8 @@ export interface IChange {
14
14
  oldValue?: any;
15
15
  changes?: IChange[];
16
16
  }
17
- export type Changeset = IChange[];
18
- export interface IFlatChange {
17
+ type Changeset = IChange[];
18
+ interface IFlatChange {
19
19
  type: Operation;
20
20
  key: string;
21
21
  path: string;
@@ -31,7 +31,7 @@ export interface IFlatChange {
31
31
  * @param {EmbeddedObjKeysType | EmbeddedObjKeysMapType} embeddedObjKeys - An optional parameter specifying keys of embedded objects.
32
32
  * @returns {IChange[]} - An array of changes that transform the old object into the new object.
33
33
  */
34
- export declare function diff(oldObj: any, newObj: any, embeddedObjKeys?: EmbeddedObjKeysType | EmbeddedObjKeysMapType, keysToSkip?: string[]): IChange[];
34
+ declare function diff(oldObj: any, newObj: any, embeddedObjKeys?: EmbeddedObjKeysType | EmbeddedObjKeysMapType, keysToSkip?: string[]): IChange[];
35
35
  /**
36
36
  * Applies all changes in the changeset to the object.
37
37
  *
@@ -43,7 +43,7 @@ export declare function diff(oldObj: any, newObj: any, embeddedObjKeys?: Embedde
43
43
  * If the change value is not null or undefined, or if the change type is REMOVE, it applies the change to the object directly.
44
44
  * Otherwise, it applies the change to the corresponding branch of the object.
45
45
  */
46
- export declare const applyChangeset: (obj: any, changeset: Changeset) => any;
46
+ declare const applyChangeset: (obj: any, changeset: Changeset) => any;
47
47
  /**
48
48
  * Reverts the changes made to an object based on a given changeset.
49
49
  *
@@ -55,7 +55,7 @@ export declare const applyChangeset: (obj: any, changeset: Changeset) => any;
55
55
  * It then iterates over each change in the changeset. If the change does not have any nested changes, it reverts the change on the object directly.
56
56
  * If the change does have nested changes, it reverts the changes on the corresponding branch of the object.
57
57
  */
58
- export declare const revertChangeset: (obj: any, changeset: Changeset) => any;
58
+ declare const revertChangeset: (obj: any, changeset: Changeset) => any;
59
59
  /**
60
60
  * Flattens a changeset into an array of flat changes.
61
61
  *
@@ -69,7 +69,7 @@ export declare const revertChangeset: (obj: any, changeset: Changeset) => any;
69
69
  * If so, it updates the path and recursively flattens the nested changes or the embedded object.
70
70
  * If the change does not have nested changes or an embedded key, it creates a flat change and returns it in an array.
71
71
  */
72
- export declare const flattenChangeset: (obj: Changeset | IChange, path?: string, embeddedKey?: string | FunctionKey) => IFlatChange[];
72
+ declare const flattenChangeset: (obj: Changeset | IChange, path?: string, embeddedKey?: string | FunctionKey) => IFlatChange[];
73
73
  /**
74
74
  * Transforms a flat changeset into a nested changeset.
75
75
  *
@@ -83,7 +83,7 @@ export declare const flattenChangeset: (obj: Changeset | IChange, path?: string,
83
83
  * If it represents a leaf node, it sets the key, type, value, and oldValue of the current change object.
84
84
  * Finally, it pushes the unflattened change object into the changes array.
85
85
  */
86
- export declare const unflattenChanges: (changes: IFlatChange | IFlatChange[]) => IChange[];
86
+ declare const unflattenChanges: (changes: IFlatChange | IFlatChange[]) => IChange[];
87
87
  /**
88
88
  * Determines the type of a given object.
89
89
  *
@@ -94,5 +94,22 @@ export declare const unflattenChanges: (changes: IFlatChange | IFlatChange[]) =>
94
94
  * If the object is neither undefined nor null, it uses Object.prototype.toString to get the object's type.
95
95
  * The type is extracted from the string returned by Object.prototype.toString using a regular expression.
96
96
  */
97
- export declare const getTypeOfObj: (obj: any) => string;
98
- export {};
97
+ declare const getTypeOfObj: (obj: any) => string;
98
+
99
+ declare enum CompareOperation {
100
+ CONTAINER = "CONTAINER",
101
+ UNCHANGED = "UNCHANGED"
102
+ }
103
+ interface IComparisonEnrichedNode {
104
+ type: Operation | CompareOperation;
105
+ value: IComparisonEnrichedNode | IComparisonEnrichedNode[] | any | any[];
106
+ oldValue?: any;
107
+ }
108
+ declare const createValue: (value: any) => IComparisonEnrichedNode;
109
+ declare const createContainer: (value: object | [
110
+ ]) => IComparisonEnrichedNode;
111
+ declare const enrich: (object: any) => IComparisonEnrichedNode;
112
+ declare const applyChangelist: (object: IComparisonEnrichedNode, changelist: IFlatChange[]) => IComparisonEnrichedNode;
113
+ declare const compare: (oldObject: any, newObject: any) => IComparisonEnrichedNode;
114
+
115
+ export { type Changeset, CompareOperation, type EmbeddedObjKeysMapType, type EmbeddedObjKeysType, type IChange, type IComparisonEnrichedNode, type IFlatChange, Operation, applyChangelist, applyChangeset, compare, createContainer, createValue, diff, enrich, flattenChangeset, getTypeOfObj, revertChangeset, unflattenChanges };
@@ -0,0 +1,115 @@
1
+ type FunctionKey = (obj: any, shouldReturnKeyName?: boolean) => any;
2
+ type EmbeddedObjKeysType = Record<string, string | FunctionKey>;
3
+ type EmbeddedObjKeysMapType = Map<string | RegExp, string | FunctionKey>;
4
+ declare enum Operation {
5
+ REMOVE = "REMOVE",
6
+ ADD = "ADD",
7
+ UPDATE = "UPDATE"
8
+ }
9
+ interface IChange {
10
+ type: Operation;
11
+ key: string;
12
+ embeddedKey?: string | FunctionKey;
13
+ value?: any | any[];
14
+ oldValue?: any;
15
+ changes?: IChange[];
16
+ }
17
+ type Changeset = IChange[];
18
+ interface IFlatChange {
19
+ type: Operation;
20
+ key: string;
21
+ path: string;
22
+ valueType: string | null;
23
+ value?: any;
24
+ oldValue?: any;
25
+ }
26
+ /**
27
+ * Computes the difference between two objects.
28
+ *
29
+ * @param {any} oldObj - The original object.
30
+ * @param {any} newObj - The updated object.
31
+ * @param {EmbeddedObjKeysType | EmbeddedObjKeysMapType} embeddedObjKeys - An optional parameter specifying keys of embedded objects.
32
+ * @returns {IChange[]} - An array of changes that transform the old object into the new object.
33
+ */
34
+ declare function diff(oldObj: any, newObj: any, embeddedObjKeys?: EmbeddedObjKeysType | EmbeddedObjKeysMapType, keysToSkip?: string[]): IChange[];
35
+ /**
36
+ * Applies all changes in the changeset to the object.
37
+ *
38
+ * @param {any} obj - The object to apply changes to.
39
+ * @param {Changeset} changeset - The changeset to apply.
40
+ * @returns {any} - The object after the changes from the changeset have been applied.
41
+ *
42
+ * The function first checks if a changeset is provided. If so, it iterates over each change in the changeset.
43
+ * If the change value is not null or undefined, or if the change type is REMOVE, it applies the change to the object directly.
44
+ * Otherwise, it applies the change to the corresponding branch of the object.
45
+ */
46
+ declare const applyChangeset: (obj: any, changeset: Changeset) => any;
47
+ /**
48
+ * Reverts the changes made to an object based on a given changeset.
49
+ *
50
+ * @param {any} obj - The object on which to revert changes.
51
+ * @param {Changeset} changeset - The changeset to revert.
52
+ * @returns {any} - The object after the changes from the changeset have been reverted.
53
+ *
54
+ * The function first checks if a changeset is provided. If so, it reverses the changeset to start reverting from the last change.
55
+ * It then iterates over each change in the changeset. If the change does not have any nested changes, it reverts the change on the object directly.
56
+ * If the change does have nested changes, it reverts the changes on the corresponding branch of the object.
57
+ */
58
+ declare const revertChangeset: (obj: any, changeset: Changeset) => any;
59
+ /**
60
+ * Flattens a changeset into an array of flat changes.
61
+ *
62
+ * @param {Changeset | IChange} obj - The changeset or change to flatten.
63
+ * @param {string} [path='$'] - The current path in the changeset.
64
+ * @param {string | FunctionKey} [embeddedKey] - The key to use for embedded objects.
65
+ * @returns {IFlatChange[]} - An array of flat changes.
66
+ *
67
+ * The function first checks if the input is an array. If so, it recursively flattens each change in the array.
68
+ * If the input is not an array, it checks if the change has nested changes or an embedded key.
69
+ * If so, it updates the path and recursively flattens the nested changes or the embedded object.
70
+ * If the change does not have nested changes or an embedded key, it creates a flat change and returns it in an array.
71
+ */
72
+ declare const flattenChangeset: (obj: Changeset | IChange, path?: string, embeddedKey?: string | FunctionKey) => IFlatChange[];
73
+ /**
74
+ * Transforms a flat changeset into a nested changeset.
75
+ *
76
+ * @param {IFlatChange | IFlatChange[]} changes - The flat changeset to unflatten.
77
+ * @returns {IChange[]} - The unflattened changeset.
78
+ *
79
+ * The function first checks if the input is a single change or an array of changes.
80
+ * It then iterates over each change and splits its path into segments.
81
+ * For each segment, it checks if it represents an array or a leaf node.
82
+ * If it represents an array, it creates a new change object and updates the pointer to this new object.
83
+ * If it represents a leaf node, it sets the key, type, value, and oldValue of the current change object.
84
+ * Finally, it pushes the unflattened change object into the changes array.
85
+ */
86
+ declare const unflattenChanges: (changes: IFlatChange | IFlatChange[]) => IChange[];
87
+ /**
88
+ * Determines the type of a given object.
89
+ *
90
+ * @param {any} obj - The object whose type is to be determined.
91
+ * @returns {string | null} - The type of the object, or null if the object is null.
92
+ *
93
+ * This function first checks if the object is undefined or null, and returns 'undefined' or null respectively.
94
+ * If the object is neither undefined nor null, it uses Object.prototype.toString to get the object's type.
95
+ * The type is extracted from the string returned by Object.prototype.toString using a regular expression.
96
+ */
97
+ declare const getTypeOfObj: (obj: any) => string;
98
+
99
+ declare enum CompareOperation {
100
+ CONTAINER = "CONTAINER",
101
+ UNCHANGED = "UNCHANGED"
102
+ }
103
+ interface IComparisonEnrichedNode {
104
+ type: Operation | CompareOperation;
105
+ value: IComparisonEnrichedNode | IComparisonEnrichedNode[] | any | any[];
106
+ oldValue?: any;
107
+ }
108
+ declare const createValue: (value: any) => IComparisonEnrichedNode;
109
+ declare const createContainer: (value: object | [
110
+ ]) => IComparisonEnrichedNode;
111
+ declare const enrich: (object: any) => IComparisonEnrichedNode;
112
+ declare const applyChangelist: (object: IComparisonEnrichedNode, changelist: IFlatChange[]) => IComparisonEnrichedNode;
113
+ declare const compare: (oldObject: any, newObject: any) => IComparisonEnrichedNode;
114
+
115
+ export { type Changeset, CompareOperation, type EmbeddedObjKeysMapType, type EmbeddedObjKeysType, type IChange, type IComparisonEnrichedNode, type IFlatChange, Operation, applyChangelist, applyChangeset, compare, createContainer, createValue, diff, enrich, flattenChangeset, getTypeOfObj, revertChangeset, unflattenChanges };