Skip to content

Fix a couple of bugs when str_ins+str_del are used with transform #900

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/json-patch-ot/__tests__/scenarios.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,50 @@ const groups: ScenarioGroup[] = [
},
],
},
{
name: 'str_ins x str_del (same position)',
scenarios: [
{
name: 'Deletes at same position as insert.',
docStart: {a: 'abcde'},
user1: [{op: 'str_ins', path: '/a', pos: 0, str: 'X'}],
user2: [{op: 'str_del', path: '/a', pos: 0, str: 'abc'}],
docEnd: {a: 'Xde'},
},
{
name: 'Deletes at same non-zero position as insert.',
docStart: {a: '12abcde'},
user1: [{op: 'str_ins', path: '/a', pos: 2, str: 'X'}],
user2: [{op: 'str_del', path: '/a', pos: 2, str: 'abc'}],
docEnd: {a: '12Xde'},
},
{
name: 'Deletes at higher position than insert.',
docStart: {a: 'hello world'},
user1: [{op: 'str_ins', path: '/a', pos: 2, str: 'XX'}],
user2: [{op: 'str_del', path: '/a', pos: 8, str: 'rld'}],
docEnd: {a: 'heXXllo wo'},
},
],
},
{
name: 'str_ins x str_del (different paths)',
scenarios: [
{
name: 'Operations on different paths should not interfere.',
docStart: {
a: 'hello',
b: 'world',
},
user1: [{op: 'str_ins', path: '/a', pos: 0, str: 'X'}],
user2: [{op: 'str_del', path: '/b', pos: 0, str: 'wor'}],
docEnd: {
a: 'Xhello',
b: 'ld',
},
},
],
},
/*
{
name: 'move x replace',
Expand Down
5 changes: 2 additions & 3 deletions src/json-patch-ot/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import {xforms} from './transforms';
* already accepted patches.
*
* @param accepted Array of already accepted operations.
* @param proposed Array of proposed operations. Proposed operations are mutated inline.
* @param acceptedWins Whether accepted operation should win on when paths match exactly.
* @param proposed Array of proposed operations.
*
* @returns Array of transformed changes
* @returns Array of transformed changes.
*/
export const transform = (accepted: readonly Op[], proposed: readonly Op[]): readonly Op[] => {
const length = accepted.length;
Expand Down
3 changes: 3 additions & 0 deletions src/json-patch-ot/transforms/xStrDel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {type Op, OpStrDel, OpStrIns} from '../../json-patch/op';
import {isPathEqual} from '@jsonjoy.com/json-pointer';

export const xStrDel = (del: OpStrDel, op: Op): null | Op | Op[] => {
if (op instanceof OpStrIns) {
if (!isPathEqual(del.path, op.path)) return op;
const ins = op;
if (ins.pos > del.pos) {
const deleteLength = del.deleteLength();
Expand All @@ -11,6 +13,7 @@ export const xStrDel = (del: OpStrDel, op: Op): null | Op | Op[] => {
}

if (op instanceof OpStrDel) {
if (!isPathEqual(del.path, op.path)) return op;
const opLen = op.deleteLength();
const delLen = del.deleteLength();
const overlapLen1 = del.pos + delLen - op.pos;
Expand Down
5 changes: 4 additions & 1 deletion src/json-patch-ot/transforms/xStrIns.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import {operationToOp} from '../../json-patch/codec/json';
import {type Op, OpStrDel, OpStrIns} from '../../json-patch/op';
import {isPathEqual} from '@jsonjoy.com/json-pointer';

export const xStrIns = (ins: OpStrIns, op: Op): null | Op | Op[] => {
if (op instanceof OpStrIns) {
if (!isPathEqual(ins.path, op.path)) return op;
if (ins.pos > op.pos) return op;
return operationToOp({...op.toJson(), pos: op.pos + ins.str.length}, {});
} else if (op instanceof OpStrDel) {
if (!isPathEqual(ins.path, op.path)) return op;
const del = op;
if (del.pos < ins.pos) {
const deleteLength: number = typeof del.str === 'string' ? del.str.length : del.len!;
Expand All @@ -24,7 +27,7 @@ export const xStrIns = (ins: OpStrIns, op: Op): null | Op | Op[] => {
}
}
}
if (ins.pos < del.pos) return operationToOp({...op.toJson(), pos: op.pos + ins.str.length}, {});
if (ins.pos <= del.pos) return operationToOp({...op.toJson(), pos: op.pos + ins.str.length}, {});
return op;
}

Expand Down