list-diff.js

0.1.0 • Public • Published

list-diff

Introduction

Diff two lists/strings in time O(n*m). The algorithm finding the minimal amount of moves(patches) is Levenshtein distance.

Install

$ npm install list-diff.js --save

Usage

var diff = require("list-diff.js")
var oldList = [{id: "a"}, {id: "b"}, {id: "c"}, {id: "d"}, {id: "e"}]
var newList = [{id: "c"}, {id: "a"}, {id: "b"}, {id: "e"}, {id: "f"}]
 
var patches = diff(oldList, newList, "id")
 
patches.forEach(function (patch) {
  if (patch.type === diff.DELETION) {
    oldList.splice(patch.index, 1)
  } else if (patch.type === diff.INSERTION) {
    oldList.splice(patch.index, 0, patch.item)
  } else if (patch.type === diff.SUBSTITUTION) {
    oldList.splice(patch.index, 1, patch.item)
  }
})
 
// now `oldList` is equal to `newList`
// [{id: "c"}, {id: "a"}, {id: "b"}, {id: "e"}, {id: "f"}]
console.log(oldList)

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i list-diff.js

Weekly Downloads

45

Version

0.1.0

License

MIT

Last publish

Collaborators

  • toplan