npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@kizahasi/ot-string

v0.7.0

Published

Operational Transfomation library for string

Downloads

2,576

Readme

@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