# just-diff-apply

> Apply a diff to an object. Optionally supports jsonPatch protocol

Latest version **5.5.0** (published 2022-12-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install just-diff-apply
pnpm add just-diff-apply
yarn add just-diff-apply
bun add just-diff-apply
```

## Health

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

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

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 5.5.0 |
| Published | 2022-12-17 |
| First published | 2017-08-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 15.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6200 |
| Author | Angus Croll |
| Maintainers | angus-c |
| Keywords | object, diff, apply, jsonPatch, no-dependencies, just |

## Links

- npm: https://www.npmjs.com/package/just-diff-apply
- Repository: https://github.com/angus-c/just
- Homepage: https://github.com/angus-c/just#readme
- Issues: https://github.com/angus-c/just/issues
- npm.io page: https://npm.io/package/just-diff-apply

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 5.5.0 (latest) — 2022-12-17
- 5.4.1 — 2022-08-08
- 5.4.0 — 2022-08-07
- 5.3.1 — 2022-05-21
- 5.2.0 — 2022-03-13
- 5.1.1 — 2022-01-24
- 5.1.0 — 2022-01-22
- 5.0.1 — 2022-01-09
- 5.0.0 — 2021-12-13
- 4.0.1 — 2021-11-13
- 4.0.0 — 2021-11-09
- 3.1.2 — 2021-11-01
- 3.1.0 — 2021-10-31
- 3.0.0 — 2019-06-25
- 2.0.0 — 2019-02-15
- … 3 more at https://npm.io/package/just-diff-apply/versions

## README

<!-- DO NOT EDIT THIS FILE! THIS FILE WAS AUTOGENERATED BY TEMPLATE-MATE -->
<!-- SEE https://github.com/angus-c/just/blob/master/CONTRIBUTING.md#readme-template -->

## just-diff-apply

Part of a [library](https://anguscroll.com/just) of zero-dependency npm modules that do just do one thing.
Guilt-free utilities for every occasion.

[`🍦 Try it`](https://anguscroll.com/just/just-diff-apply)

```shell
npm install just-diff-apply
```
```shell
yarn add just-diff-apply
```

Apply a diff object to an object. Pass converter to apply a http://jsonpatch.com standard patch

```js
  import {diffApply} from 'just-diff-apply';

  const obj1 = {a: 3, b: 5};
  diffApply(obj1,
    [
      { "op": "remove", "path": ['b'] },
      { "op": "replace", "path": ['a'], "value": 4 },
      { "op": "add", "path": ['c'], "value": 5 }
    ]
  );
  obj1; // {a: 4, c: 5}

  const obj2 = {a: 3, b: 5};
  diffApply(obj2,
    [
      { "op": "move", "from": ['a'], "path": ['c']},
    ]
  );
  obj2; // {b: 5, c: 3}

  // using converter to apply jsPatch standard paths
  // see http://jsonpatch.com
  import {diffApply, jsonPatchPathConverter} from 'just-diff-apply'
  const obj3 = {a: 3, b: 5};
  diffApply(obj3, [
    { "op": "remove", "path": '/b' },
    { "op": "replace", "path": '/a', "value": 4 }
    { "op": "add", "path": '/c', "value": 5 }
  ], jsonPatchPathConverter);
  obj3; // {a: 4, c: 5}

  // arrays (array key can be string or numeric)
  const obj4 = {a: 4, b: [1, 2, 3]};
  diffApply(obj4, [
    { "op": "replace", "path": ['a'], "value": 3 }
    { "op": "replace", "path": ['b', 2], "value": 4 }
    { "op": "add", "path": ['b', 3], "value": 9 }
  ]);
  obj4; // {a: 3, b: [1, 2, 4, 9]}

  // nested paths
  const obj5 = {a: 4, b: {c: 3}};
  diffApply(obj5, [
    { "op": "replace", "path": ['a'], "value": 5 }
    { "op": "remove", "path": ['b', 'c']}
    { "op": "add", "path": ['b', 'd'], "value": 4 }
  ]);
  obj5; // {a: 5, b: {d: 4}}
```

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