diff --git a/packages/@ot-doc/document/src/lib/array-document.spec.ts b/packages/@ot-doc/document/src/lib/array-document.spec.ts index 669c027..7d020b0 100644 --- a/packages/@ot-doc/document/src/lib/array-document.spec.ts +++ b/packages/@ot-doc/document/src/lib/array-document.spec.ts @@ -1,56 +1,42 @@ import { describeDocumentMeta, DocumentTestCases } from "../util/test-utility"; -import { arrayDocument, Oplet } from "./array-document"; +import { arrayDocument, deleteAt, insertAt, Oplet } from "./array-document"; const arrNum = arrayDocument(); describeDocumentMeta('arrayDocument', arrNum, { singleton: [ [], - [{ typ: 'ins', idx: 0, val: 1 }], - [{ typ: 'del', idx: 0, val: 1 }], - [ - { typ: 'del', idx: 0, val: 1 }, - { typ: 'ins', idx: 1, val: 2 }, - ], - [ - { typ: 'ins', idx: 1, val: 2 }, - { typ: 'del', idx: 0, val: 1 }, - ], + insertAt(0, 1), + deleteAt(0, 1), + [...deleteAt(0, 1), ...insertAt(1, 2)], + [...insertAt(1, 2), ...deleteAt(0, 1)], ], composable3: [ [ [], - [ - { typ: 'ins', idx: 1, val: 2 }, - { typ: 'del', idx: 0, val: 1 }, - ], - [ - { typ: 'del', idx: 0, val: 2 }, - { typ: 'ins', idx: 1, val: 2 }, - ], + [...insertAt(1, 2), ...deleteAt(0, 1)], + [...deleteAt(0, 2), ...insertAt(1, 2)], ], [ - [ - { typ: 'ins', idx: 1, val: 2 }, - { typ: 'del', idx: 0, val: 1 }, - ], + [...insertAt(1, 2), ...deleteAt(0, 1)], [], - [ - { typ: 'del', idx: 0, val: 2 }, - { typ: 'ins', idx: 1, val: 2 }, - ], + [...deleteAt(0, 2), ...insertAt(1, 2)], ], [ - [ - { typ: 'ins', idx: 1, val: 2 }, - { typ: 'del', idx: 0, val: 1 }, - ], - [ - { typ: 'del', idx: 0, val: 2 }, - { typ: 'ins', idx: 1, val: 2 }, - ], + [...insertAt(1, 2), ...deleteAt(0, 1)], + [...deleteAt(0, 2), ...insertAt(1, 2)], [], ], + [ + insertAt(0, 1, 2, 3), + insertAt(1, 4, 5), + insertAt(3, 6), + ], + [ + insertAt(0, 0, 1, 2, 3), + deleteAt(2, 2, 3), + deleteAt(0, 0), + ], ], others({ comp, inv }) { // const a: Oplet[] = [{ typ: 'ins', idx: 1, val: 2}, { typ: 'del', idx: 0, val: 1 }]; diff --git a/packages/@ot-doc/document/src/lib/array-document.ts b/packages/@ot-doc/document/src/lib/array-document.ts index a8ad7d4..d2a8373 100644 --- a/packages/@ot-doc/document/src/lib/array-document.ts +++ b/packages/@ot-doc/document/src/lib/array-document.ts @@ -75,7 +75,7 @@ export const arrayDocument = ({ const update = (...ops: Oplet[]): void => { if (r === a) r = [...a]; r.splice(i, 2, ...ops); - i = i + ops.length - 1; + i = Math.max(i + ops.length - 1, 0); updated = true; }; @@ -83,7 +83,8 @@ export const arrayDocument = ({ updated = false; i = 0; while (i < r.length - 1) { - const [eA, eB] = r.slice(i, i + 2); + const eA = r[i]; + const eB = r[i + 1]; if (eA.typ === 'ins') { if (eB.typ === 'ins') { if (eA.idx >= eB.idx) { @@ -142,4 +143,10 @@ export const arrayDocument = ({ return true; }, }; -}; \ No newline at end of file +}; + +export const insertAt = (i: number, ...arr: T[]): Oplet[] => + arr.map((val, j) => ({ typ: 'ins', val, idx: i + j })); + +export const deleteAt = (i: number, ...arr: T[]): Oplet[] => + arr.map((val, j): Oplet => ({ typ: 'del', val, idx: i + j })).reverse(); \ No newline at end of file