# generic-diff

> Diff any array-like object, including strings

Latest version **1.0.1** (published 2015-12-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install generic-diff
pnpm add generic-diff
yarn add generic-diff
bun add generic-diff
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2015-12-20 |
| First published | 2015-04-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 10 |
| Author | Luc Thevenard |
| Maintainers | lucthev |
| Keywords | array, diff, string, difference |

## Links

- npm: https://www.npmjs.com/package/generic-diff
- Repository: https://github.com/lucthev/generic-diff
- Issues: https://github.com/lucthev/generic-diff/issues
- npm.io page: https://npm.io/package/generic-diff

## Dependencies (1)

- [object-assign](https://npm.io/package/object-assign.md) ^2.0.0

## Alternatives

- [@mce/gif](https://npm.io/package/@mce/gif.md) — 2.6K weekly downloads
- [cleanse](https://npm.io/package/cleanse.md) — 173 weekly downloads
- [str](https://npm.io/package/str.md) — 127 weekly downloads
- [naming](https://npm.io/package/naming.md) — 95 weekly downloads
- [tap-telco-api](https://npm.io/package/tap-telco-api.md) — 19 weekly downloads

## Recent versions

- 1.0.1 (latest) — 2015-12-20
- 1.0.0 — 2015-04-03

## README

# generic-diff

Diff arrays or array-like objects, such as strings. Based on ["An O(ND) Difference Algorithm and its Variations" (Myers, 1986)][diff].

## diff( a, b [, eql] )

Diffs the array-like objects `a` and `b`, returning a summary of edits required to turn `a` into `b`. Defaults to using strict equality (`===`) to compare items in `a` and `b`. A comparison function, `eql`, can optionally be used for more nuanced comparisons; the signature of this function is `(item from a, item from b) => Boolean`.

The “summary of changes” is an array of objects with three properties: `items`, an array of one or more items from `a` or `b`, and boolean properties `added` and `removed`, indicating whether the item(s) should be added or removed from `a`, respectively. For example, if we’re diffing the strings `abc` and `abd`, the summary of changes would look like:

```js
[{
  items: ['a', 'b'],
  added: false,
  removed: false
}, {
  items: ['c'],
  added: false,
  removed: true
}, {
  items: ['d'],
  added: true,
  removed: false
}]
```

## Example

Diff two strings, creating an HTML representation of their differences:

```js
var diff = require('generic-diff')

var changes = diff('falafel', 'fallacy')
changes = changes.map(function (edit) {
  if (edit.added) {
    return '<ins>' + edit.items.join('') + '</ins>'
  } else if (edit.removed) {
    return '<del>' + edit.items.join('') + '</del>'
  } else {
    return edit.items.join('')
  }
}).join('')

console.log(changes)
// 'fal<del>afe</del>l<ins>acy</ins>'
```

For a slightly more involved example, [this gist][file-diff] demonstrates how to diff two files and produce an output similar to the UNIX `diff` command.

[file-diff]: https://gist.github.com/lucthev/f7096f85442ec448cb64
[diff]: http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.4.6927

## LICENSE

MIT

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