All files / json-crdt/__bench__/util/editors json-joy.ts

79.16% Statements 19/24
100% Branches 0/0
61.53% Functions 8/13
79.16% Lines 19/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 462x 2x 2x     2x     7x 7x 7x   403663x 403663x     105602x   7x             2x     7x 7x 7x 7x   403663x     105602x   7x              
import {Timestamp} from '../../../../json-crdt-patch/clock';
import {StrNode} from '../../../nodes';
import {Model} from '../../../model';
import type {SequentialTraceEditor} from '../types';
 
export const editorStrNode: SequentialTraceEditor = {
  name: 'StrNode (json-joy)',
  factory: () => {
    let time = 0;
    const rga = new StrNode(new Timestamp(1, time++));
    return {
      ins: (pos: number, insert: string) => {
        rga.insAt(pos, new Timestamp(1, time), insert);
        time += insert.length;
      },
      del: (pos: number, len: number) => {
        rga.delete(rga.findInterval(pos, len));
      },
      get: () => rga.view(),
      len: () => rga.length(),
      chunks: () => rga.size(),
    };
  },
};
 
export const editorJsonJoy: SequentialTraceEditor = {
  name: 'json-joy',
  factory: () => {
    const model = Model.withLogicalClock();
    model.api.root('');
    const str = model.api.str([]);
    return {
      ins: (pos: number, insert: string) => {
        str.ins(pos, insert);
      },
      del: (pos: number, len: number) => {
        str.del(pos, len);
      },
      get: () => str.view(),
      len: () => str.view().length,
      chunks: () => str.node.size(),
      toBlob: () => model.toBinary(),
    };
  },
};