Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@sanity/diff-match-patch

Package Overview
Dependencies
Maintainers
33
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@sanity/diff-match-patch

Robust diff, match and patch algorithms to perform operations required for synchronizing plain text

  • 0.0.4
  • npm
  • Socket score

Version published
Weekly downloads
103K
decreased by-4.28%
Maintainers
33
Weekly downloads
 
Created
Source

@sanity/diff-match-patch

A TypeScript fork of the JavaScript version of google/diff-match-patch, that includes a few long-standing pull requests, fixing certain bugs and with an API more familiar to the JavaScript ecosystem.

What is it?

The Diff Match and Patch libraries offer robust algorithms to perform the operations required for synchronizing plain text.

  1. Diff:
    • Compare two blocks of plain text and efficiently return a list of differences.
    • Diff Demo
  2. Match:
    • Given a search string, find its best fuzzy match in a block of plain text. Weighted for both accuracy and location.
    • Match Demo
  3. Patch:
    • Apply a list of patches onto plain text. Use best-effort to apply patch even when the underlying text doesn't match.
    • Patch Demo

Originally built in 2006 to power Google Docs, this library is now available in C++, C#, Dart, Java, JavaScript, Lua, Objective C, and Python.

API

Creating and applying patches
import {
  makePatches,
  applyPatches,
  stringifyPatches,
} from '@sanity/diff-match-patch'

// Make array of diffs in internal array format, eg tuples of `[DiffType, string]`
const patches = makePatches('from this', 'to this')

// Make unidiff-formatted (string) patch
const patch = stringifyPatches(patches)

// Apply the patch (either the unidiff-formatted or the internal array representation)
const newValue = applyPatches('from this', patches)
Creating diffs
import { makeDiff } from '@sanity/diff-match-patch'

// Make an array of diff tuples, eg `[DiffType, string]`
const diff = makeDiff('from this', 'to this')
Matching text
import { match } from '@sanity/diff-match-patch'

// Find position in text for the given pattern, at the approximate location given
const position = match('some text to match against', 'match', 9)

Algorithms

This library implements Myer's diff algorithm which is generally considered to be the best general-purpose diff. A layer of pre-diff speedups and post-diff cleanups surround the diff algorithm, improving both performance and output quality.

This library also implements a Bitap matching algorithm at the heart of a flexible matching and patching strategy.

Significant changes

This fork has a few modifications to the original:

  • API has changed - individual methods are exposed as importable functions instead of being attached to a DiffMatchPatch prototype
  • Includes a fix for surrogate pair handling, by Dennis Snell
  • Uses modern tooling for code compilation and testing

Keywords

FAQs

Package last updated on 22 Jul 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc