https://github.com/yeojz/diff-immutability-helper
:fork_and_knife: Creates a diff between 2 javascript objects / variables, in a format that is compatible with immutability-helper
https://github.com/yeojz/diff-immutability-helper
diff immutability-helper immutable object react
Last synced: about 2 months ago
JSON representation
:fork_and_knife: Creates a diff between 2 javascript objects / variables, in a format that is compatible with immutability-helper
- Host: GitHub
- URL: https://github.com/yeojz/diff-immutability-helper
- Owner: yeojz
- License: mit
- Created: 2017-06-16T14:44:50.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-08-26T06:47:34.000Z (almost 7 years ago)
- Last Synced: 2025-03-24T00:11:14.756Z (2 months ago)
- Topics: diff, immutability-helper, immutable, object, react
- Language: JavaScript
- Homepage:
- Size: 107 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# diff-immutability-helper
> Creates a diff between 2 JavaScript objects, allowing you to mutate one object to another using immutability-helper
[![npm package][npm-badge]][npm-link]
[![Build Status][build-badge]][build-link]
[![Coverage Status][codecov-badge]][codecov-link]## Overview
`diff-immutability-helper` creates an [immutability-helper](https://www.npmjs.com/package/immutability-helper) compatible diff object between 2 JavaScript variables.
This diff object would then allow you to mutate the base object to the target object.
## Installation
```
$> npm install diff-immutability-helper
```## Example
Given:
```js
import diff from 'diff-immutability-helper';const base = {
a: [1, 2, { b: 1 }, 4, 5, 6],
b: 'test',
c: 'prev'
};const target = {
a: [1, 2, { b: 2 }, 4, 6],
b: 'test 2',
d: 'new'
};const change = diff(base, target);
```will give a result of:
```js
const change = {
a: {
$splice: [[4, 1], [2, 1, { b: 2 }]]
},
b: {$set: 'test 2'},
$apply: (v) => omit(v, ['c']),
$merge: {
d: 'new'
}
}
```thus, we can then do:
```js
import update from 'immutability-helper';
update(base, change); // to match target
```## Notes
- Array diffing uses [LCS](https://en.wikipedia.org/wiki/Longest_common_subsequence_problem)
## License
`diff-immutability-helper` is [MIT licensed](./LICENSE)
[npm-badge]: https://img.shields.io/npm/v/diff-immutability-helper.svg?style=flat-square
[npm-link]: https://www.npmjs.com/package/diff-immutability-helper[build-badge]: https://img.shields.io/circleci/project/github/yeojz/diff-immutability-helper/master.svg?style=flat-square
[build-link]: https://circleci.com/gh/yeojz/diff-immutability-helper.svg[codecov-badge]: https://img.shields.io/codecov/c/github/yeojz/diff-immutability-helper/master.svg?style=flat-square
[codecov-link]: https://codecov.io/gh/yeojz/diff-immutability-helper