• Stars
    star
    146
  • Rank 252,769 (Top 5 %)
  • Language
    JavaScript
  • License
    Apache License 2.0
  • Created over 9 years ago
  • Updated over 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Visual JavaScript text diff library. CommonJS module created from https://code.google.com/p/google-diff-match-patch/.

TextDiff

JavaScript diff library with support for visual, HTML-formatted output

This repository contains the diff functionality of the google-diff-match-patch library by Neil Fraser, turned into a node module which is suitable for requireing into projects.

Demo

Example usage

var Diff = require('text-diff');

var diff = new Diff(); // options may be passed to constructor; see below
var textDiff = diff.main('text1', 'text2'); // produces diff array
diff.prettyHtml(textDiff); // produces a formatted HTML string

Initialization options

Arguments may be passed into the Diff constructor in the form of an object:

  • timeout: Number of seconds to map a diff before giving up (0 for infinity).
  • editCost: Cost of an empty edit operation in terms of edit characters.

Example initialization with arguments: var diff = new Diff({ timeout: 2, editCost: 6 });

Documentation

The API documentation below has been modified from the original API documentation.

Initialization

The first step is to create a new diff object (see example above). This object contains various properties which set the behaviour of the algorithms, as well as the following methods/functions:

main(text1, text2) => diffs

An array of differences is computed which describe the transformation of text1 into text2. Each difference is an array. The first element specifies if it is an insertion (1), a deletion (-1) or an equality (0). The second element specifies the affected text.

main("Good dog", "Bad dog") => [(-1, "Goo"), (1, "Ba"), (0, "d dog")]

Despite the large number of optimisations used in this function, diff can take a while to compute. The timeout setting is available to set how many seconds any diff's exploration phase may take (see "Initialization options" section above). The default value is 1.0. A value of 0 disables the timeout and lets diff run until completion. Should diff time out, the return value will still be a valid difference, though probably non-optimal.

cleanupSemantic(diffs) => null

A diff of two unrelated texts can be filled with coincidental matches. For example, the diff of "mouse" and "sofas" is [(-1, "m"), (1, "s"), (0, "o"), (-1, "u"), (1, "fa"), (0, "s"), (-1, "e")]. While this is the optimum diff, it is difficult for humans to understand. Semantic cleanup rewrites the diff, expanding it into a more intelligible format. The above example would become: [(-1, "mouse"), (1, "sofas")]. If a diff is to be human-readable, it should be passed to cleanupSemantic.

cleanupEfficiency(diffs) => null

This function is similar to cleanupSemantic, except that instead of optimising a diff to be human-readable, it optimises the diff to be efficient for machine processing. The results of both cleanup types are often the same.

The efficiency cleanup is based on the observation that a diff made up of large numbers of small diffs edits may take longer to process (in downstream applications) or take more capacity to store or transmit than a smaller number of larger diffs. The diff.EditCost property sets what the cost of handling a new edit is in terms of handling extra characters in an existing edit. The default value is 4, which means if expanding the length of a diff by three characters can eliminate one edit, then that optimisation will reduce the total costs.

levenshtein(diffs) => int

Given a diff, measure its Levenshtein distance in terms of the number of inserted, deleted or substituted characters. The minimum distance is 0 which means equality, the maximum distance is the length of the longer string.

prettyHtml(diffs) => html

Takes a diff array and returns a string of pretty HTML. Deletions are wrapped in <del></del> tags, and insertions are wrapped in <ins></ins> tags. Use CSS to apply styling to these tags.

Tests

Tests have not been ported to this library fork, however tests are available in the original library. If you would like to port tests over, you will need to do some function call renaming (viz. the diff_ prefix has been removed from functions in the fork) and remove tests specific to the "patch" and "match" functionalities of the original library.

More Repositories

1

google-sheet-s3

Google Apps Script that publishes a Google Sheet to Amazon S3 as a JSON file. Auto-updates on edit & maintains data types. Creates an array of objects keyed by column header.
JavaScript
130
star
2

stream-editor

A web tool for interactively using and chaining command-line text manipulation utilities like sed, grep, and awk.
HTML
25
star
3

slack-meetups

Slack bot that randomly pairs users in a Slack channel for 1:1 meetups. Meet new, interesting people in your company, club, or group!
Python
18
star
4

skry

Extendable CMS for small news organizations following decoupled CMS design paradigm. Built on Django Rest Framework. Dynamic image resizing, revision history, fully exposable models API.
Python
16
star
5

driving-time-heatmap

Input an origin point, generate a heatmap of driving times to surrounding areas at different times of day. πŸš— ⏱ πŸ”₯ πŸ—Ί
JavaScript
10
star
6

harmontown-search

Search all transcripts from the Harmontown podcast. Transcription powered by OpenAI's Whisper model. Search powered by Typesense.
TypeScript
7
star
7

google-diff-match-patch

Automatically exported from code.google.com/p/google-diff-match-patch
Python
4
star
8

type-emoji

The no-nonsense emoji search keyboard. Search for and copy emoji quickly.
JavaScript
4
star
9

flight-status-dashboard

Simple command line UI to display the progress of your flight.
JavaScript
3
star
10

sepalnote

Hierarchical note taking webapp. Django/PostgreSQL + AngularJS. (under development)
Python
3
star
11

monitor404

Easy-to-configure service for logging and notification of HTTP 404 errors.
HTML
3
star
12

google-sheets-json

NPM package to easily get data from public Google Sheets and format the messy API response in a pretty, usable way.
JavaScript
3
star
13

react-kofi-overlay

A React component for accepting donations though Ko-fi without leaving your website
TypeScript
3
star
14

spectacle

Web viewer for circular videos – such as those produced by Snap Inc.'s Spectacles smartglasses – using gyroscope data from DeviceOrientation events. This library is not affiliated with or endorsed by Snap, Inc.
JavaScript
2
star
15

simple-video-chat

[WIP] Dead simple video chat using WebRTC and the SimpleWebRTC library. Idea is to make it simple enough for anyone to use easily – no extraneous user interface stuff that every video chat app seems to have.
JavaScript
2
star
16

smarthome

Collection of my smarthome-related scripts
JavaScript
2
star
17

mailpdf

Online service to mail a PDF document anywhere in the U.S. under 60 seconds.
JavaScript
2
star
18

console.console

A method on `console` for when you need consolation that you're doing ok. `console.console()`
JavaScript
2
star
19

spekti

Type the name of a TV show at the command line and start watching the latest episode within 10 seconds. πŸ“Ί Uses torrent search scraping, TVDB API, and WebTorrent CLI.
JavaScript
1
star
20

bionic-ears

Experimenting with using the web audio API to give you super hearing while filtering out transient noise.
JavaScript
1
star
21

gifdatabase

The internet's finest animated GIF search. (until Giphy came around... someone give me $150 million in venture capital?)
Python
1
star
22

my-commute-dashboard

Visualize when the trains are arriving along my personal commute and when to leave to catch them. πŸš†β³
JavaScript
1
star
23

cs35l

UCLA Winter 2015 CS 35L (Software Construction Laboratory) labs and homework.
Assembly
1
star
24

bottom-nav-visibility-change

Event listener to detect when the visibility of the Mobile Safari bottom navigation bar has changed.
HTML
1
star
25

kikl

The link shortener for easy to type and remember URLs. E.g., kikl.co/goofy-lemur instead of bit.ly/1QyEygk
Python
1
star
26

yagsa

Yet Another Google Spreadsheet API. Get the contents of a Google Sheet in properly-typed JSON, cache the result.
JavaScript
1
star
27

react-live-blog

**Not yet production-ready** React live blog component using good ol' Ajax polling that supports arbitrary embeds and markdown formatting. Works great with a data backend using https://github.com/liddiard/google-sheet-s3
JavaScript
1
star