npm

@kizahasi/ot-string
TypeScript icon, indicating that this package has built-in type declarations

0.7.0 • Public • Published

@kizahasi/ot-string

License npm version minified size CI publish

Operational Transfomation library.

Installation

Run npm install @kizahasi/ot-string or yarn add @kizahasi/ot-string.

To use it in a browser directly, you can use Skypack.

Usage

Diff two texts

import { diff } from '@kizahasi/ot-string';

const prevState = 'January';
const nextState = 'February';

const twoWayOperation = diff({ prevState, nextState });

Apply operations

import { diff, toUpOperation, apply, toDownOperation, applyBack } from '@kizahasi/ot-string';

const prevState = 'January';
const nextState = 'February';
const twoWayOperation = diff({ prevState, nextState });

// `toUpOperation` drops some redundant data from twoWayOperation to reduce its object size.
const upOperation = toUpOperation(twoWayOperation);
// `apply` function accepts upOperation, but cannot accept downOperation.
const nextState2 = apply({ prevState, upOperation });
console.log(nextState2.isError, nextState2.value);
// => false February

// `toDownOperation` drops some redundant data from twoWayOperation to reduce its object size.
const downOperation = toDownOperation(twoWayOperation);
// `applyBack` function accepts downOperation, but cannot accept upOperation.
const prevState2 = applyBack({ nextState, downOperation });
console.log(prevState2.isError, prevState2.value);
// => false January

Operational transformation

import { toUpOperation, diff, transformUpOperation, apply } from '@kizahasi/ot-string';

const state1 = 'June 1';
const state2_june2 = 'June 2';
const state2_july1 = 'July 1';

const first = toUpOperation(diff({ prevState: state1, nextState: state2_june2 }));
const second = toUpOperation(diff({ prevState: state1, nextState: state2_july1 }));

const transformed = transformUpOperation({ first, second });
console.log(transformed.isError);
// => false

// state1 + first + secondPrime
const state3a = apply({ prevState: state2_june2, upOperation: transformed.value.secondPrime });
// state1 + second + firstPrime
const state3b = apply({ prevState: state2_july1, upOperation: transformed.value.firstPrime });

console.log(state3a.isError);
// => false
console.log(state3b.isError);
// => false
console.log(state3a.value === 'July 2');
// => true

// state1 + first + secondPrime = state1 + second + firstPrime
console.log(state3a.value === state3b.value);
// => true

Serialization and deserialization

import {
    diff,
    toDownOperation,
    toUpOperation,
    serializeUpOperation,
    serializeDownOperation,
    serializeTwoWayOperation,
    deserializeUpOperation,
    deserializeDownOperation,
    deserializeTwoWayOperation,
} from '@kizahasi/ot-string';
import { dequal } from 'dequal'; // package to check for deep equality

const twoWayOperation = diff({ prevState: 'hour', nextState: 'ours' });
const upOperation = toUpOperation(twoWayOperation);
const downOperation = toDownOperation(twoWayOperation);

// Serialize UpOperation.
const serializedUpOperation = serializeUpOperation(upOperation);
console.log(serializedUpOperation);
// => [ { t: 'd', d: 1 }, { t: 'r', r: 3 }, { t: 'i', i: 's' } ]
// (t = type, r = retain, i = insert, d = delete. Above object indicates "Delete 1 character, then retain 3 characters, finally insert 's'.")

// Serialize DownOperation.
const serializedDownOperation = serializeDownOperation(downOperation);
console.log(serializedDownOperation);
// => [ { t: 'd', d: 'h' }, { t: 'r', r: 3 }, { t: 'i', i: 1 } ]

// Serialize TwoWayOperation.
const serializedTwoWayOperation = serializeTwoWayOperation(twoWayOperation);
console.log(serializedTwoWayOperation);
// => [ { t: 'd', d: 'h' }, { t: 'r', r: 3 }, { t: 'i', i: 's' } ]

// Deserialize.
const deserializedUpOperation = deserializeUpOperation(serializedUpOperation);
const deserializedDownOperation = deserializeDownOperation(serializedDownOperation);
const deserializedTwoWayOperation = deserializeTwoWayOperation(serializedTwoWayOperation);
console.log(dequal(downOperation, deserializedDownOperation)); // => true
console.log(dequal(upOperation, deserializedUpOperation)); // => true
console.log(dequal(twoWayOperation, deserializedTwoWayOperation)); // => true

Issues

  • Some functions are not implemented (e.g. transformDownOperation).

License

MIT

Package Sidebar

Install

npm i @kizahasi/ot-string

Weekly Downloads

415

Version

0.7.0

License

MIT

Unpacked Size

76.6 kB

Total Files

33

Last publish

Collaborators

  • kizahasi