0.1.0 • Published 8 years ago

list-diff.js v0.1.0

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

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