@kizahasi/ot-string
Operational Transfomation library for string.
Installation
Run npm install @kizahasi/ot-string
or yarn add @kizahasi/ot-string
.
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({ prevState, upOperation });
console.log(nextState2.isError, nextState2.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,
deserializeUpOperation,
deserializeDownOperation,
deserializeTwoWayOperation,
} from '../src';
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(downOperation);
console.log(serializedDownOperation);
const deserializedUpOperation = deserializeUpOperation(serializedUpOperation);
const deserializedDownOperation = deserializeDownOperation(serializedDownOperation);
const deserializedTwoWayOperation = deserializeTwoWayOperation(serializedTwoWayOperation);
Issues
- Some functions are not implemented (e.g. transformDownOperation).