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

ot-diff

v1.1.1

Published

A simple diff library for implementing operational transformations in JS

Downloads

28

Readme

ot-diff

A diffing tool for operational-transformations in JavaScript.

(note: this is not a diffing tool as you might expect. For git-style diffs, check out diff)

Purpose

When working with operational-transformations (OT), you need to know when a block of text is inserted, deleted, or replaced. You're only working with contiguous changes in strings (contiguous meaning that all changes are together. There's never more than one group of changes).

Here's an example:

# contiguous changes (what OT handles)
string  -> strings # insertion
strings -> string # deletion
string  -> strong # replacement

# non-contiguous changes (what OT can't handle)
string  -> things

This library exists to give you OT-friendly diffs between two strings.

Usage

Install with

npm install ot-diff --save
# Or
yarn add ot-diff

Include with

import OtDiff from 'ot-diff';
// Or
const OtDiff = require('ot-diff');

Use with

OtDiff.diff('old string', 'new string', raw);
// raw is an optional boolean that will give you more information about the diff.  Default: false

Examples

For more examples, see src/test/.

Insertion

OtDiff.diff('strin', 'string')

// returns:
// {
//   action: 'insert',
//   payload: 'g',
//   start: 5 <- character which to insert after (0-indexed)
// }

Deletion

OtDiff.diff('string', 'strin')

// returns:
// {
//   action: 'delete',
//   start: 5, <- character at which deletion starts (0-indexed)
// Deletion includes this character.
//   remove: 1 <- number of characters removed
// }

Replacement


OtDiff.diff('string', 'strong')

// returns:
// {
//   action: 'replace',
//   start: 3,
//   remove: 1,
//   payload: 'o'
// }

No changes

OtDiff.diff('string', 'string')

// returns:
// {
//   action: 'noop'
// }

Transform tools

There are four helper tools for applying transformations to a string.

They all take the string you're manipulating and the transform returned by OtDiff.diff. They are:

let transform = OtDiff.diff(...);

OtDiff.insert('string', transform);
OtDiff.delete('string', transform);
OtDiff.replace('string', transform);
OtDiff.noop('string', transform);

Or, if you want the transform to be applied based on the value of diff.action, you can use this:

let transform = OtDiff.diff(...);

OtDiff.transform('string', transform); // Automatically applies the correct transformation based on transform

Contributing

Changing code

Edit what you need in src/ot-diff.js then run npm run compile before submittig a PR.

Ensure that all tests are passing and you add any appropriate new tests before submitting!

Running tests

npm run test will compile and run the compiled tests.

Benchmarking

Please benchmark before and after contributing with npm run benchmark. If your additions cause poor performance, they may be rejected.

Other

Bug reports and pull requests are welcome on GitHub at https://github.com/kieraneglin/ot-diff/. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The package is available as open source under the terms of the MIT License.