|
| 1 | +# diff-patch |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +diff-patch is a library that exposes two functions, `diff` and `patch`. |
| 6 | + |
| 7 | +The library does not have any dependencies. |
| 8 | + |
| 9 | +The `diff` function takes two objects (`obj1` and `obj2`) and deep compares them, returning an object that represents the difference between the two (the `diffData`). |
| 10 | +`diffData` is an object that can be used to apply the diff to `obj1` to get an object that is the same as `obj2`. |
| 11 | + |
| 12 | +The `patch` function takes an `obj` and a `diffData` object and applies the diff to the object, returning a new object. |
| 13 | + |
| 14 | +The `diff` and `patch` functions are implemented in the following languages: |
| 15 | + |
| 16 | + * TypeScript |
| 17 | + * Python3 |
| 18 | + |
| 19 | +The `diffData` object **can be used interchangeably between the languages**.\ |
| 20 | +This makes this library very useful for networked applications, where the client and server are written in different languages. |
| 21 | + |
| 22 | +`diffData` can be serialized to JSON and deserialized back to an object. It aims to be as small as possible, and is designed to be used in a networked environment. |
| 23 | + |
| 24 | +More languages will be added in the future, contributions are welcome. |
| 25 | + |
| 26 | +## Usage |
| 27 | + |
| 28 | +### TypeScript |
| 29 | + |
| 30 | +To install the TypeScript version of diff-patch, run: |
| 31 | + |
| 32 | +```bash |
| 33 | +npm install @fsoft/diff-patch |
| 34 | +``` |
| 35 | + |
| 36 | +Then, in your TypeScript code: |
| 37 | + |
| 38 | +```typescript |
| 39 | +import { diff, patch } from '@fsoft/diff-patch'; |
| 40 | + |
| 41 | +const obj1 = { |
| 42 | + a: 1, |
| 43 | + b: 2, |
| 44 | + c: { |
| 45 | + d: 3, |
| 46 | + e: 4, |
| 47 | + }, |
| 48 | +}; |
| 49 | + |
| 50 | +const obj2 = { |
| 51 | + a: 1, |
| 52 | + b: 2, |
| 53 | + c: { |
| 54 | + d: 3, |
| 55 | + e: 5, // changed |
| 56 | + }, |
| 57 | +}; |
| 58 | + |
| 59 | +// create the diff data |
| 60 | +const diffData = diff(obj1, obj2); |
| 61 | + |
| 62 | +// show the diff data for debugging |
| 63 | +console.log( "diffData: ", diffData); |
| 64 | + |
| 65 | +// apply the diff data to obj1 to get an object that is the same as obj2 |
| 66 | +const obj3 = patch(obj1, diffData); |
| 67 | + |
| 68 | +// show the result for debugging |
| 69 | +console.log( "obj3: ", obj3); |
| 70 | +``` |
| 71 | + |
| 72 | +## Contributors |
| 73 | + |
| 74 | +This library was created by [Fabio Rotondo](https://github.com/fsoft72). |
| 75 | + |
| 76 | +New language implementations are more than welcome.\ |
| 77 | +Please open an issue or a pull request if you want to contribute. |
| 78 | + |
| 79 | +**Collaborators**: |
| 80 | + |
| 81 | + * [Nikola Gluhovic](https://github.com/nini-os) |
| 82 | + |
| 83 | + |
| 84 | +The official repository for this library is [here](https://github.com/fsoft72/diff-patch). |
| 85 | + |
| 86 | +## License |
| 87 | + |
| 88 | +This library is licensed under the MIT License.\ |
| 89 | +See the [LICENSE](LICENSE) file for details. |
0 commit comments