The Wayback Machine - https://web.archive.org/web/20201201095845/https://github.com/cudr/scto
Skip to content
master
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

scto

Text diff compare to OP (CRDT Operation model)

Build Status img

This package is similar to jsdiff, but generates CRDT-friendly operations. Useful for compare strings to OP data-transfer model. x30 faster then diff-match-patch and x625 faster for strings compare than jsdiff!

image

Install

yarn add scto

Example

Text compare:

import { stringDiffToOps, applyOps } from 'scto'

const origin = 'Yet so far hath discretion fought'
const modifyed = 'Yet get far discretion fought!'

const operations = stringDiffToOps(origin, modifyed)

/*
  operations [
    { type: 'replace', offset: 4, data: 'get', shift: 2 },
    { type: 'drop', offset: 12, shift: 5 },
    { type: 'insert', offset: 29, data: '!' }
  ] 
*/

const applyed = applyOps(origin, operations) // Yet get far discretion fought!

console.log(applyed === modifyed) // true

Array compare:

import { collateDiffToOps, applyOps } from 'scto'

const origin = ['foo', 'bar', 'baz']
const modifyed = ['abc', 'foo', 'baz']

const operations = collateDiffToOps(origin, modifyed)

/*
  operations [
    { type: 'insert', offset: 0, data: ['abc'] },
    { type: 'drop', offset: 2, shift: 1 }
  ] 
*/

const applyed = applyOps(origin, operations)

/* ['abc', 'foo', 'baz'] */

Possible operations:

Replace:

export interface Replace {
  type: "replace";
  offset: number;
  shift: number;
  data: Collate;
}

Drop:

export interface Drop {
  type: "drop";
  offset: number;
  shift: number;
}

Insert:

export interface Insert {
  type: "insert";
  offset: number;
  data: Collate;
}

About

Text diff compare to OP (CRDT Operation model)

Topics

Resources

Releases

No releases published

Packages

No packages published
You can’t perform that action at this time.