https://github.com/yeojz/flat-diff
diff algorithm based on the unique key path of each value in an Object / JSON
https://github.com/yeojz/flat-diff
diff json keypath object
Last synced: 5 months ago
JSON representation
diff algorithm based on the unique key path of each value in an Object / JSON
- Host: GitHub
- URL: https://github.com/yeojz/flat-diff
- Owner: yeojz
- License: mit
- Created: 2017-06-20T14:56:23.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-06-25T15:45:23.000Z (over 8 years ago)
- Last Synced: 2025-01-20T09:40:50.816Z (9 months ago)
- Topics: diff, json, keypath, object
- Language: JavaScript
- Homepage:
- Size: 38.1 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# flat-diff
> A naive diff algorithm based on the unique key path of each value in an Object / JSON
[![npm package][npm-badge]][npm-link]
[![Build Status][build-badge]][build-link]
[![Coverage Status][codecov-badge]][codecov-link]## Installation
```
$ npm install flat-diff --save
```## Example
Given:
```js
import diff from 'flat-diff';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 metadata = diff(base, target);
```will give a result of:
```js
const metadata = [
{ path: 'a.0', type: 'same', value: 1 },
{ path: 'a.1', type: 'same', value: 2 },
{ path: 'a.2.b', type: 'put', prev: 1, value: 2 },
{ path: 'a.3', type: 'same', value: 4 },
{ path: 'a.4', type: 'put', prev: 5, value: 6 },
{ path: 'a.5', type: 'del', value: 6 },
{ path: 'b', type: 'put', prev: 'test', value: 'test 2' },
{ path: 'c', type: 'del', value: 'prev' },
{ path: 'd', type: 'post', value: 'new' }
];// Note: path keys will be sorted.
```## Caveats
Due to how keys are generated, if you have an array and object with number keys in the same path,
there will be a conflict.i.e.
```js
// object
{
a: {
'0': 'b',
'1': 'c'
}
}// array
{
a: ['b', 'c']
}// both formats above will result in similar paths:
[
{path: 'a.0', value: 'b'},
{path: 'a.1', value: 'c'}
]
```## License
`flat-diff` is [MIT licensed](./LICENSE)
[npm-badge]: https://img.shields.io/npm/v/flat-diff.svg?style=flat-square
[npm-link]: https://www.npmjs.com/package/flat-diff[build-badge]: https://img.shields.io/circleci/project/github/yeojz/flat-diff/master.svg?style=flat-square
[build-link]: https://circleci.com/gh/yeojz/flat-diff.svg[codecov-badge]: https://img.shields.io/codecov/c/github/yeojz/flat-diff/master.svg?style=flat-square
[codecov-link]: https://codecov.io/gh/yeojz/flat-diff