# list-diff2

> Diff two list

Latest version **0.1.4** (published 2015-11-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install list-diff2
pnpm add list-diff2
yarn add list-diff2
bun add list-diff2
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.4 |
| Published | 2015-11-21 |
| First published | 2015-11-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 184 |
| Author | Livoras |
| Maintainers | livoras |
| Keywords | diff |

## Links

- npm: https://www.npmjs.com/package/list-diff2
- Repository: https://github.com/livoras/list-diff
- Issues: https://github.com/livoras/list-diff/issues
- npm.io page: https://npm.io/package/list-diff2

## Recent versions

- 0.1.4 (latest) — 2015-11-21
- 0.1.3 — 2015-11-20
- 0.1.2 — 2015-11-19
- 0.1.1 — 2015-11-19
- 0.1.0 — 2015-11-19

## README

list-diff 
=================

[![build](https://circleci.com/gh/livoras/list-diff/tree/master.png?style=shield)](https://circleci.com/gh/livoras/list-diff) [![codecov.io](https://codecov.io/github/livoras/list-diff/coverage.svg?branch=master)](https://codecov.io/github/livoras/list-diff?branch=master) [![npm version](https://badge.fury.io/js/list-diff2.svg)](https://badge.fury.io/js/list-diff2) [![Dependency Status](https://david-dm.org/livoras/list-diff.svg)](https://david-dm.org/livoras/list-diff) 

[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)

## Introduction

Diff two lists in time O(n). 
I
The algorithm finding the minimal amount of moves is [Levenshtein distance](https://en.wikipedia.org/wiki/Levenshtein_distance) which is O(n*m). This algorithm is not the best but is enougth for front-end DOM list manipulation. 

This project is mostly influenced by [virtual-dom](https://github.com/Matt-Esch/virtual-dom/blob/master/vtree/diff.js) algorithm.

## Install

    $ npm install list-diff2 --save

## Usage

```javascript
var diff = require("list-diff2")
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 moves = diff(oldList, newList, "id")
// `moves` is a sequence of actions (remove or insert): 
// type 0 is removing, type 1 is inserting
// moves: [
//   {index: 3, type: 0},
//   {index: 0, type: 1, item: {id: "c"}}, 
//   {index: 0, type: 0}, 
//   {index: 4, type: 1, item: {id: "f"}}
//  ]

moves.forEach(function(move) {
  if (move.type === 0) {
    oldList.splice(move.index, 1) // type 0 is removing
  } else {
    oldList.splice(move.index, 0, move.item) // type 1 is inserting
  }
})

// now `oldList` is equal to `newList`
// [{id: "c"}, {id: "a"}, {id: "b"}, {id: "e"}, {id: "f"}]
console.log(oldList) 
```

## License 
MIT

---
_Source: https://npm.io/package/list-diff2 · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
