项目作者: kieraneglin

项目描述 :
Operational-transformation diffing tool for JavaScript
高级语言: JavaScript
项目地址: git://github.com/kieraneglin/ot-diff.git
创建时间: 2017-05-29T19:56:01Z
项目社区:https://github.com/kieraneglin/ot-diff

开源协议:

下载


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:

  1. # contiguous changes (what OT handles)
  2. string -> strings # insertion
  3. strings -> string # deletion
  4. string -> strong # replacement
  5. # non-contiguous changes (what OT can't handle)
  6. string -> things

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

Usage

Install with

  1. npm install ot-diff --save
  2. # Or
  3. yarn add ot-diff

Include with

  1. import OtDiff from 'ot-diff';
  2. // Or
  3. const OtDiff = require('ot-diff');

Use with

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

Examples

For more examples, see src/test/.

Insertion

  1. OtDiff.diff('strin', 'string')
  2. // returns:
  3. // {
  4. // action: 'insert',
  5. // payload: 'g',
  6. // start: 5 <- character which to insert after (0-indexed)
  7. // }

Deletion

  1. OtDiff.diff('string', 'strin')
  2. // returns:
  3. // {
  4. // action: 'delete',
  5. // start: 5, <- character at which deletion starts (0-indexed)
  6. // Deletion includes this character.
  7. // remove: 1 <- number of characters removed
  8. // }

Replacement

  1. OtDiff.diff('string', 'strong')
  2. // returns:
  3. // {
  4. // action: 'replace',
  5. // start: 3,
  6. // remove: 1,
  7. // payload: 'o'
  8. // }

No changes

  1. OtDiff.diff('string', 'string')
  2. // returns:
  3. // {
  4. // action: 'noop'
  5. // }

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:

  1. let transform = OtDiff.diff(...);
  2. OtDiff.insert('string', transform);
  3. OtDiff.delete('string', transform);
  4. OtDiff.replace('string', transform);
  5. OtDiff.noop('string', transform);

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

  1. let transform = OtDiff.diff(...);
  2. 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.