Skip to content

Commit 0971655

Browse files
committed
feat: add support for move operations
1 parent 0711abf commit 0971655

File tree

3 files changed

+138
-88
lines changed

3 files changed

+138
-88
lines changed

src/__tests__/new.test.ts

Lines changed: 86 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,13 @@ describe('JSONPatchOT', () => {
1212
{op: OpType.replace, path: '/removed/array/3', value: 86},
1313
];
1414

15-
expect(JSONPatchOT(acceptedOps, proposedOps)).toEqual([
16-
{op: OpType.replace, path: '/some/other', value: 3},
17-
]);
15+
expect(JSONPatchOT(acceptedOps, proposedOps)).toEqual([{op: OpType.replace, path: '/some/other', value: 3}]);
1816
});
1917

2018
it('should handle types without a translation function', () => {
21-
const acceptedOps: Operation[] = [
22-
{op: OpType.test, path: '/test', value: 'test'},
23-
];
19+
const acceptedOps: Operation[] = [{op: OpType.test, path: '/test', value: 'test'}];
2420

25-
const proposedOps: Operation[] = [
26-
{op: OpType.replace, path: '/test', value: 'change name'},
27-
];
21+
const proposedOps: Operation[] = [{op: OpType.replace, path: '/test', value: 'change name'}];
2822

2923
expect(JSONPatchOT(acceptedOps, proposedOps)).toEqual(proposedOps);
3024
});
@@ -59,49 +53,32 @@ describe('JSONPatchOT', () => {
5953
});
6054

6155
it('should handle path without slash', () => {
62-
const acceptedOps: Operation[] = [
63-
{op: OpType.remove, path: ''},
64-
];
56+
const acceptedOps: Operation[] = [{op: OpType.remove, path: ''}];
6557

66-
const proposedOps: Operation[] = [
67-
{op: OpType.replace, path: '', value: 'change name'},
68-
];
58+
const proposedOps: Operation[] = [{op: OpType.replace, path: '', value: 'change name'}];
6959

7060
expect(JSONPatchOT(acceptedOps, proposedOps)).toEqual([]);
7161
});
7262

7363
it('should handle add to the same path', () => {
74-
const acceptedOps: Operation[] = [
75-
{op: OpType.add, path: '/title', value: 'Hello!'},
76-
];
64+
const acceptedOps: Operation[] = [{op: OpType.add, path: '/title', value: 'Hello!'}];
7765

78-
const proposedOps: Operation[] = [
79-
{op: OpType.add, path: '/title', value: 'Hi World!'},
80-
];
66+
const proposedOps: Operation[] = [{op: OpType.add, path: '/title', value: 'Hi World!'}];
8167

82-
expect(JSONPatchOT(acceptedOps, proposedOps)).toEqual([
83-
{op: OpType.add, path: '/title', value: 'Hi World!'},
84-
]);
68+
expect(JSONPatchOT(acceptedOps, proposedOps)).toEqual([{op: OpType.add, path: '/title', value: 'Hi World!'}]);
8569
});
8670

8771
it('should handle add to the same path with options', () => {
8872
const options = {acceptedWinsOnEqualPath: true};
89-
const acceptedOps: Operation[] = [
90-
{op: OpType.add, path: '/title', value: 'Hello!'},
91-
];
73+
const acceptedOps: Operation[] = [{op: OpType.add, path: '/title', value: 'Hello!'}];
9274

93-
const proposedOps: Operation[] = [
94-
{op: OpType.add, path: '/title', value: 'Hi World!'},
95-
];
75+
const proposedOps: Operation[] = [{op: OpType.add, path: '/title', value: 'Hi World!'}];
9676

9777
expect(JSONPatchOT(acceptedOps, proposedOps, options)).toEqual([]);
9878
});
9979

10080
it('should handle mutliple array index changes', () => {
101-
const acceptedOps: Operation[] = [
102-
{op: OpType.remove, path: '/array/3'},
103-
{op: OpType.remove, path: '/array/5'},
104-
];
81+
const acceptedOps: Operation[] = [{op: OpType.remove, path: '/array/3'}, {op: OpType.remove, path: '/array/5'}];
10582

10683
const proposedOps: Operation[] = [
10784
{op: OpType.replace, path: '/array/4', value: 'change name'},
@@ -160,26 +137,61 @@ describe('JSONPatchOT', () => {
160137
]);
161138
});
162139

163-
// it('should handle array index changes with accepted moves to and out of an array', () => {
164-
// // [0, 1, 2, 3, 4, 5, 6]; <- Starting array
165-
// const acceptedOps: Operation[] = [
166-
// // {op: OpType.move, path: '/array/3', from: '/someval'}, //acts like an add
167-
// {op: OpType.move, path: '/someval', from: '/array/5'}, // acts like a remove
168-
// ];
140+
it('should handle array index changes with accepted moves out of array', () => {
141+
// [0, 1, 2, 3, 4, 5, 6]; <- Starting array
142+
const acceptedOps: Operation[] = [
143+
{op: OpType.move, path: '/someval', from: '/array/5'}, // acts like a remove
144+
];
145+
146+
// [0, 1, 2, 3, 4, 6]; <- Array after accepted copies
147+
148+
// Actions to double some specific value
149+
const proposedOps: Operation[] = [
150+
{op: OpType.replace, path: '/array/5', value: 10}, // 5 -> 10
151+
];
169152

170-
// // [0, 1, 2, val, 3, 5, 6]; <- Array after accepted copies
153+
expect(JSONPatchOT(acceptedOps, proposedOps)).toEqual([]);
154+
});
171155

172-
// // Actions to double some specific values
173-
// const proposedOps: Operation[] = [
174-
// {op: OpType.replace, path: '/array/5', value: 6}, // 3 -> 6
175-
// ];
156+
it('should handle array index changes with accepted moves into array', () => {
157+
// [0, 1, 2, 3, 4, 5, 6]; <- Starting array
158+
const acceptedOps: Operation[] = [
159+
{op: OpType.move, path: '/array/3', from: '/someval'}, //acts like an add
160+
];
176161

177-
// expect(JSONPatchOT(acceptedOps, proposedOps)).toEqual([
178-
// {op: OpType.replace, path: '/array/4', value: 6},
179-
// //{op: OpType.replace, path: '/array/6', value: 8}, <- removed
180-
// {op: OpType.replace, path: '/array/8', value: 12},
181-
// ]);
182-
// });
162+
// [0, 1, 2, val, 3, 5, 6]; <- Array after accepted copies
163+
164+
// Actions to double some specific values
165+
const proposedOps: Operation[] = [
166+
{op: OpType.replace, path: '/array/3', value: 6}, // 3 -> 6
167+
];
168+
169+
expect(JSONPatchOT(acceptedOps, proposedOps)).toEqual([{op: OpType.replace, path: '/array/4', value: 6}]);
170+
});
171+
172+
it('should handle array index changes with accepted moves within an array', () => {
173+
// [0, 1, 2, 3, 4, 5, 6]; <- Starting array
174+
const acceptedOps: Operation[] = [
175+
{op: OpType.move, path: '/array/3', from: '/array/5'}, //acts like an add
176+
];
177+
178+
// [0, 1, 2, val, 3, 5, 6]; <- Array after accepted copies
179+
180+
// Actions to double some specific values
181+
const proposedOps: Operation[] = [
182+
{op: OpType.replace, path: '/array/3', value: 6}, // 3 -> 6
183+
{op: OpType.replace, path: '/array/4', value: 8}, // 4 -> 8
184+
{op: OpType.replace, path: '/array/5', value: 10}, // 5 -> 10
185+
{op: OpType.replace, path: '/array/6', value: 12}, // 6 -> 12
186+
];
187+
188+
expect(JSONPatchOT(acceptedOps, proposedOps)).toEqual([
189+
{op: OpType.replace, path: '/array/4', value: 6},
190+
{op: OpType.replace, path: '/array/5', value: 8},
191+
// {op: OpType.replace, path: '/array/5', value: 10}, <- removed
192+
{op: OpType.replace, path: '/array/6', value: 12}, // <- unchanged
193+
]);
194+
});
183195

184196
it('should handle array index changes with accepted adds and removes', () => {
185197
// [0, 1, 2, 3, 4, 5, 6]; <- Starting array
@@ -203,4 +215,27 @@ describe('JSONPatchOT', () => {
203215
{op: OpType.replace, path: '/array/5', value: 10},
204216
]);
205217
});
218+
219+
it('should handle array index changes with accepted adds and removes in different order', () => {
220+
// [0, 1, 2, 3, 4, 5, 6]; <- Starting array
221+
const acceptedOps: Operation[] = [
222+
{op: OpType.add, path: '/array/1', value: 30},
223+
{op: OpType.remove, path: '/array/2'},
224+
];
225+
226+
// [0, 30, 2, 3, 4, 5, 6]; <- Array after accepted add and remove
227+
228+
// Actions to double some specific values
229+
const proposedOps: Operation[] = [
230+
{op: OpType.replace, path: '/array/2', value: 4}, // 2 -> 4
231+
{op: OpType.replace, path: '/array/1', value: 2}, // 1 -> 2
232+
{op: OpType.replace, path: '/array/5', value: 10}, // 5 -> 10
233+
];
234+
235+
expect(JSONPatchOT(acceptedOps, proposedOps)).toEqual([
236+
{op: OpType.replace, path: '/array/2', value: 4},
237+
// {op: OpType.replace, path: '/array/1', value: 2}, <- removed
238+
{op: OpType.replace, path: '/array/5', value: 10},
239+
]);
240+
});
206241
});

0 commit comments

Comments
 (0)