@kizahasi/ot-string
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 });
const upOperation = toUpOperation(twoWayOperation);
const nextState2 = apply({ prevState, upOperation });
console.log(nextState2.isError, nextState2.value);
const downOperation = toDownOperation(twoWayOperation);
const prevState2 = applyBack({ nextState, downOperation });
console.log(prevState2.isError, prevState2.value);
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);
const state3a = apply({ prevState: state2_june2, upOperation: transformed.value.secondPrime });
const state3b = apply({ prevState: state2_july1, upOperation: transformed.value.firstPrime });
console.log(state3a.isError);
console.log(state3b.isError);
console.log(state3a.value === 'July 2');
console.log(state3a.value === state3b.value);
Serialization and deserialization
import {
diff,
toDownOperation,
toUpOperation,
serializeUpOperation,
serializeDownOperation,
serializeTwoWayOperation,
deserializeUpOperation,
deserializeDownOperation,
deserializeTwoWayOperation,
} from '@kizahasi/ot-string';
import { dequal } from 'dequal';
const twoWayOperation = diff({ prevState: 'hour', nextState: 'ours' });
const upOperation = toUpOperation(twoWayOperation);
const downOperation = toDownOperation(twoWayOperation);
const serializedUpOperation = serializeUpOperation(upOperation);
console.log(serializedUpOperation);
const serializedDownOperation = serializeDownOperation(downOperation);
console.log(serializedDownOperation);
const serializedTwoWayOperation = serializeTwoWayOperation(twoWayOperation);
console.log(serializedTwoWayOperation);
const deserializedUpOperation = deserializeUpOperation(serializedUpOperation);
const deserializedDownOperation = deserializeDownOperation(serializedDownOperation);
const deserializedTwoWayOperation = deserializeTwoWayOperation(serializedTwoWayOperation);
console.log(dequal(downOperation, deserializedDownOperation));
console.log(dequal(upOperation, deserializedUpOperation));
console.log(dequal(twoWayOperation, deserializedTwoWayOperation));
Issues
- Some functions are not implemented (e.g. transformDownOperation).
License
MIT