Skip to content

Commit d31e60a

Browse files
committed
renamings of comparison service and prep input utils functions
1 parent 0edc115 commit d31e60a

2 files changed

Lines changed: 470 additions & 0 deletions

File tree

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
import * as jsondiffpatch from "jsondiffpatch";
2+
import { Delta } from "jsondiffpatch";
3+
4+
import { JSONValue } from "../utils/jsonHelper.js";
5+
import { InputModels } from "../interfaces/inputmodels.js";
6+
import { DifferenceState, DiffModel } from "../interfaces/diffmodel.js";
7+
import * as customJuuFormatter from "../utils/customFormatter.js";
8+
import { CustomOp } from "../interfaces/util.js";
9+
import defaultHashMatching from "../customisable/defaultHashMatching.js";
10+
import { algoVariation, CONFIG } from "../config.js";
11+
import { prepareDiffMap } from "../utils/prepInput.js";
12+
import { runtimeImprovedMapImplementations } from "./algo.js";
13+
14+
15+
export function createDiff(inputModels: InputModels): DiffModel {
16+
if (inputModels.original === undefined) {
17+
console.log("2-Way Comparison");
18+
return createDiffModelFrom2WayDiff(
19+
createDiff2Way(inputModels.left, inputModels.right),
20+
);
21+
} else {
22+
console.log("3-Way Comparison");
23+
return createDiff3Way(
24+
inputModels.original,
25+
inputModels.left,
26+
inputModels.right,
27+
);
28+
}
29+
}
30+
31+
export function createDiffModelFrom2WayDiff(operations: CustomOp[]): DiffModel {
32+
const diffModel: DiffModel = {
33+
threeWay: false,
34+
differencesL: [],
35+
differencesR: [],
36+
conflicts: [],
37+
};
38+
39+
operations.forEach((op, index) => {
40+
diffModel.differencesL.push({
41+
id: index, // TODO: what id to take? more sophisticated?
42+
kind: op.op,
43+
state: DifferenceState.UNRESOLVED,
44+
path: op.path,
45+
});
46+
});
47+
48+
return diffModel;
49+
}
50+
51+
export function createDiff2Way(left: JSONValue, right: JSONValue): CustomOp[] {
52+
const delta = jsondiffpatch
53+
.create({
54+
objectHash: (obj, index) => {
55+
return defaultHashMatching(obj as Record<string, undefined>, index);
56+
},
57+
})
58+
.diff(left, right);
59+
60+
const operations = customJuuFormatter.format(delta);
61+
62+
return operations;
63+
}
64+
65+
export function createDiff3Way(
66+
original: JSONValue,
67+
left: JSONValue,
68+
right: JSONValue,
69+
): DiffModel {
70+
let diffModel: DiffModel = {
71+
threeWay: true,
72+
differencesL: [],
73+
differencesR: [],
74+
conflicts: [],
75+
};
76+
77+
const diffsLeft = createDiff2Way(original, left);
78+
79+
const diffsRight = createDiff2Way(original, right);
80+
81+
if (diffsLeft == undefined || diffsRight == undefined) {
82+
// left or right is the same as orginal, so no conflicts but differences
83+
84+
if (diffsLeft === undefined) {
85+
for (const [i, diff] of diffsRight.entries()) {
86+
diffModel.differencesR.push({
87+
id: i, // TODO: maybe other id instead of index?
88+
kind: diff.op,
89+
path: diff.path,
90+
state: DifferenceState.UNRESOLVED,
91+
});
92+
}
93+
} else {
94+
for (const [i, diff] of diffsLeft.entries()) {
95+
diffModel.differencesL.push({
96+
id: i, // TODO: maybe other id instead of index?
97+
kind: diff.op,
98+
path: diff.path,
99+
state: DifferenceState.UNRESOLVED,
100+
});
101+
}
102+
}
103+
return diffModel;
104+
}
105+
106+
console.log("jsonpatch result left", JSON.stringify(diffsLeft));
107+
console.log("jsonpatch result right", JSON.stringify(diffsRight));
108+
109+
/*
110+
if (algoVariation.nested) {
111+
const diffsWithUsedFlagL = addUsedFlag(diffsLeft);
112+
const diffsWithUsedFlagR = addUsedFlag(diffsRight);
113+
114+
// Populate differencesL and differencesR
115+
diffModel.differencesL = diffsWithUsedFlagL.map((diff, index) => ({
116+
id: index, // TODO: maybe other id instead of index?
117+
kind: diff.opInfo.op,
118+
state: DifferenceState.UNRESOLVED,
119+
path: diff.opInfo.path,
120+
}));
121+
122+
diffModel.differencesR = diffsWithUsedFlagR.map((diff, index) => ({
123+
id: index, // TODO: maybe other id instead of index?
124+
kind: diff.opInfo.op,
125+
state: DifferenceState.UNRESOLVED,
126+
path: diff.opInfo.path,
127+
}));
128+
129+
diffModel = nestedForLoopWorstImplementation(
130+
diffsWithUsedFlagL,
131+
diffsWithUsedFlagR,
132+
diffModel,
133+
);
134+
}
135+
*/
136+
137+
if (algoVariation.eficient) {
138+
const diffMapL = prepareDiffMap(diffsLeft);
139+
const diffMapR = prepareDiffMap(diffsRight);
140+
141+
const diffListL = [
142+
...diffMapL.delete.values(),
143+
...diffMapL.add.values(),
144+
...diffMapL.update.values(),
145+
...diffMapL.move.values(),
146+
];
147+
const diffListR = [
148+
...diffMapR.delete.values(),
149+
...diffMapR.add.values(),
150+
...diffMapR.update.values(),
151+
...diffMapR.move.values(),
152+
];
153+
154+
// console.log("maps of left and right diffs");
155+
// console.log(diffListL);
156+
// console.log(diffListR);
157+
158+
// Populate differencesL and differencesR
159+
diffModel.differencesL = diffListL.map((diff, index) => ({
160+
id: index, // TODO: maybe other id instead of index?
161+
kind: diff.opInfo.op,
162+
state: DifferenceState.UNRESOLVED,
163+
path: diff.opInfo.path,
164+
}));
165+
166+
diffModel.differencesR = diffListR.map((diff, index) => ({
167+
id: index, // TODO: maybe other id instead of index?
168+
kind: diff.opInfo.op,
169+
state: DifferenceState.UNRESOLVED,
170+
path: diff.opInfo.path,
171+
}));
172+
173+
diffModel = runtimeImprovedMapImplementations(
174+
diffMapL,
175+
diffMapR,
176+
diffModel,
177+
);
178+
}
179+
180+
return diffModel;
181+
}
182+
183+
export function applyDiffDoPatch(original: unknown, delta: Delta) {
184+
const withHash = jsondiffpatch.create({
185+
objectHash: (obj) => {
186+
const objRecord = obj as Record<string, undefined>;
187+
console.log("objeect - any type");
188+
console.log(objRecord);
189+
return objRecord[CONFIG.IDENTIFIER];
190+
},
191+
});
192+
193+
withHash.patch(original, delta);
194+
195+
console.log(JSON.stringify(original));
196+
return original;
197+
}
198+
199+
/*
200+
function improvedTextDiffTry() {
201+
const customDiffPatch = jsondiffpatch.create({
202+
// textDiff: {
203+
// diffMatchPatch: {},
204+
// minLength: 60, // default value
205+
// },
206+
});
207+
}
208+
*/

0 commit comments

Comments
 (0)