Skip to content

mlcheng/js-diff

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

js-diff

Easily find the difference between two strings using Levenshtein distance.

A demo is available on my playground.

Usage

To find the diff between two strings, specify the source and the final string to find the transformation.

const source = 'pineapple';
const destination = 'apple';

iqwerty.diff.diff(source, destination);

A DiffObject is returned. It contains the following properties:

.from

The source string - in this case: 'pineapple'.

.to

The destination string - in this case: 'apple'.

.levenshteinDistance

The edit distance, or the amount of changes needed to transform the source to destination string.

.changes

An array containing the changes needed to transform the source string. You may parse this yourself if you wish.

.toString()

Call this method to print the default string with changes made. In this case, .toString() will output an object:

{
	plainText: '(-pine)apple',
	richText: '<span class="iqwerty-diff-remove">(-pine)</span>apple',
}

The iqwerty-diff-remove and iqwerty-diff-add classes can be styled however you like.

About

String differences using LCS and Levenshtein distance

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published