# disparity

> Colorized string diff ideal for text/code that spans through multiple lines

Latest version **3.2.0** (published 2021-01-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install disparity
pnpm add disparity
yarn add disparity
bun add disparity
```

Provides the command `disparity`.

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 3.2.0 |
| Published | 2021-01-20 |
| First published | 2015-04-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=8 |
| Dependencies | 2 |
| Unpacked size | 15.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 58 |
| Author | Miller Medeiros |
| Maintainers | ruyadorno, millermedeiros |
| Keywords | diff, char, unified, multiline, string, color, ansi, terminal, cli, tty |

## Links

- npm: https://www.npmjs.com/package/disparity
- Repository: https://github.com/millermedeiros/disparity
- Issues: https://github.com/millermedeiros/disparity/issues
- npm.io page: https://npm.io/package/disparity

## Dependencies (2)

- [diff](https://npm.io/package/diff.md) ^4.0.2
- [ansi-styles](https://npm.io/package/ansi-styles.md) ^4.2.1

## Alternatives

- [d3-force-3d](https://npm.io/package/d3-force-3d.md) — 1.0M weekly downloads
- [ng2-charts](https://npm.io/package/ng2-charts.md) — 486.8K weekly downloads
- [@arcgis/core](https://npm.io/package/@arcgis/core.md) — 257.8K weekly downloads
- [react-sparklines](https://npm.io/package/react-sparklines.md) — 249.3K weekly downloads
- [react-native-gifted-charts](https://npm.io/package/react-native-gifted-charts.md) — 182.3K weekly downloads

## Recent versions

- 3.2.0 (latest) — 2021-01-20
- 3.1.0 — 2020-05-19
- 3.0.0 — 2019-11-06
- 2.0.0 — 2015-04-04
- 1.3.1 — 2015-04-01
- 1.3.0 — 2015-04-01
- 1.2.0 — 2015-04-01
- 1.1.0 — 2015-04-01
- 1.0.0 — 2015-04-01

## README

# disparity

Colorized string diff ideal for text/code that spans through multiple lines.

This is basically just a wrapper around
[diff](https://www.npmjs.com/package/diff) and
[ansi-styles](https://www.npmjs.com/package/ansi-styles) + line numbers and
omitting lines that don't have changes and/or that wouldn't help user identify
the diff "context".

We also replace some *invisible* chars to make it easier to understand what
really changed from one file to another:

 - `\r` becomes `<CR>`
 - `\n` becomes `<LF>`
 - `\t` becomes `<tab>`

Created mainly to be used by
[esformatter](https://www.npmjs.com/package/esformatter) and other tools that
might need to display a nice looking diff of source files.


## API

```js
var disparity = require('disparity');
```

### chars(oldStr, newStr[, opts]):String

Diffs two blocks of text, comparing character by character and returns
a `String` with ansi color codes.

```js
var diff = disparity.chars(file1, file2);
console.log(diff);
```

Will return an empty string if `oldStr === newStr`;

```js
// default options
var opts = {
  // how many lines to display before/after a line that contains diffs
  context: 3,
  // file paths displayed just before the diff
  paths: [disparity.removed, disparity.added]
};
```

![screenshot char diff](https://raw.githubusercontent.com/millermedeiros/disparity/master/screenshots/chars.png)

### unified(oldStr, newStr[, opts]):String

Returns ansi colorized [unified
diff](http://en.wikipedia.org/wiki/Diff_utility#Unified_format).

Will return an empty string if `oldStr === newStr`;

```js
var diff = disparity.unified(file1, file2, {
  paths: ['test/file1.js', 'test/file2.js']
});
console.log(diff);
```

![screenshot unified diff](https://raw.githubusercontent.com/millermedeiros/disparity/master/screenshots/unified.png)

### unifiedNoColor(oldStr, newStr[, opts]):String

Returns [unified diff](http://en.wikipedia.org/wiki/Diff_utility#Unified_format).
Useful for terminals that [doesn't support colors](https://www.npmjs.com/package/supports-color).

Will return an empty string if `oldStr === newStr`;

```js
var diff = disparity.unifiedNoColor(file1, file2, {
  paths: ['test/file1.js', 'test/file2.js']
});
console.log(diff);
```

![screenshot unified diff no color](https://raw.githubusercontent.com/millermedeiros/disparity/master/screenshots/unified_no_color.png)

### removed:String

String used on the diff headers to say that chars/lines was removed.

```js
// default value
disparity.removed = 'removed';
```

### added:String

String used on the diff headers to say that chars/lines was added.

```js
// default value
disparity.added = 'added';
```

### colors:Object

Object containing references to all the colors used by disparity.

If you want a different output than `ansi` (eg. HTML) you can replace the color
values:

```js
// wrap blocks into custom tags
disparity.colors = {
  // chars diff
  charsRemoved: { open: '<bggreen>', close: '</bggreen>' },
  charsAdded: { open: '<bgred>', close: '</bgred>' },

  // unified diff
  removed: { open: '<red>', close: '</red>' },
  added: { open: '<green>', close: '</green>' },
  header: { open: '<yellow>', close: '</yellow>' },
  section: { open: '<magenta>', close: '</magenta>' }
};
```

## CLI

`disparity` also have a command line interface:

```
disparity [OPTIONS] <file_1> <file_2>

Options:
  -c, --chars           Output char diff (default mode).
  -u, --unified         Output unified diff.
  --unified-no-color    Don't output colors.
  -v, --version         Display current version.
  -h, --help            Display this help.
```

PS: cli can only compare 2 external files at the moment, no `stdin` support.

## License

Released under the MIT license.

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