|
| 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 | +To install the Python3 version of diff-patch, run: |
| 29 | + |
| 30 | +```bash |
| 31 | +pip install fsoft-diff-patch |
| 32 | +``` |
| 33 | + |
| 34 | +Then, in your Python code: |
| 35 | + |
| 36 | +```python |
| 37 | + |
| 38 | +from fsoft_diff_patch import diff, patch |
| 39 | + |
| 40 | +obj1 = { |
| 41 | + 'a': 1, |
| 42 | + 'b': 2, |
| 43 | + 'c': { |
| 44 | + 'd': 3, |
| 45 | + 'e': 4, |
| 46 | + }, |
| 47 | +} |
| 48 | + |
| 49 | +obj2 = { |
| 50 | + 'a': 1, |
| 51 | + 'b': 2, |
| 52 | + 'c': { |
| 53 | + 'd': 3, |
| 54 | + 'e': 5, # changed |
| 55 | + }, |
| 56 | +} |
| 57 | + |
| 58 | +# create the diff data |
| 59 | +diff_data = diff(obj1, obj2) |
| 60 | + |
| 61 | +# show the diff data for debugging |
| 62 | +print("diff_data: ", diff_data) |
| 63 | + |
| 64 | +# apply the diff data to obj1 to get an object that is the same as obj2 |
| 65 | +obj3 = patch(obj1, diff_data) |
| 66 | + |
| 67 | +# show the result for debugging |
| 68 | +print("obj3: ", obj3) |
| 69 | +``` |
| 70 | + |
| 71 | +### Hint |
| 72 | + |
| 73 | +The `diff` function can be used to see if two objects are the same, by comparing the result of `diff` to an empty object. |
| 74 | + |
| 75 | +```python |
| 76 | +from diff_patch import diff |
| 77 | + |
| 78 | +obj1 = { |
| 79 | + 'a': 1, |
| 80 | + 'b': 2, |
| 81 | + 'c': { |
| 82 | + 'd': 3, |
| 83 | + 'e': 4, |
| 84 | + }, |
| 85 | +} |
| 86 | + |
| 87 | +obj2 = { |
| 88 | + 'a': 1, |
| 89 | + 'b': 2, |
| 90 | + 'c': { |
| 91 | + 'd': 3, |
| 92 | + 'e': 4, |
| 93 | + }, |
| 94 | +} |
| 95 | + |
| 96 | +diff_data = diff(obj1, obj2) |
| 97 | + |
| 98 | +# if the objects are the same, diff_data will be an empty object |
| 99 | +if not diff_data: |
| 100 | + print("The objects are the same") |
| 101 | +``` |
| 102 | + |
| 103 | +## Contributors |
| 104 | + |
| 105 | +This library was created by [Fabio Rotondo](https://github.com/fsoft72). |
| 106 | + |
| 107 | +New language implementations are more than welcome.\ |
| 108 | +Please open an issue or a pull request if you want to contribute. |
| 109 | + |
| 110 | +**Collaborators**: |
| 111 | + |
| 112 | + * [Nikola Gluhovic](https://github.com/nini-os) |
| 113 | + |
| 114 | + |
| 115 | +The official repository for this library is [here](https://github.com/fsoft72/diff-patch). |
| 116 | + |
| 117 | +## License |
| 118 | + |
| 119 | +This library is licensed under the MIT License.\ |
| 120 | +See the [LICENSE](LICENSE) file for details. |
0 commit comments