|
1 | 1 | <p align="center">
|
2 | 2 | <h1 align="center">o2diff</h1>
|
3 |
| - <p align="center">Compares two objects and returns the differences between them if different formats: changed values, changed paths, differences.</p> |
| 3 | + <p align="center">Compares two objects and returns the differences between them in different formats: changed values, changed paths, differences.</p> |
4 | 4 | <p align="center">Works in Node.js and in the browser.</p>
|
5 | 5 | <p align="center">
|
6 | 6 | <a href="https://github.com/alexandermac/o2diff/actions/workflows/ci.yml?query=branch%3Amaster"><img src="https://github.com/alexandermac/o2diff/actions/workflows/ci.yml/badge.svg" alt="Build Status"></a>
|
|
11 | 11 | <h3 align="center"><a href="https://alexandermac.github.io/o2diff">Demo</a></h3>
|
12 | 12 | </p>
|
13 | 13 |
|
14 |
| -### Features |
15 |
| -- Provides three outputs: |
16 |
| - - `diff`: `{ left, right }` the object differences. |
17 |
| - - `values`: `{ changed, added, deleted }`, the changed values. |
18 |
| - - `paths`: `{ changed, added, deleted }`, the changed paths. |
19 |
| -- Revert function, to revert the destination object to the source object. |
| 14 | +# Contents |
| 15 | +- [Contents](#contents) |
| 16 | +- [Install](#install) |
| 17 | +- [Usage](#usage) |
| 18 | +- [API](#api) |
| 19 | +- [References](#references) |
| 20 | +- [License](#license) |
20 | 21 |
|
21 |
| -### Install |
| 22 | +# Install |
22 | 23 | ```bash
|
23 | 24 | $ pnpm i o2diff
|
24 | 25 | ```
|
25 | 26 |
|
26 |
| -### Usage |
| 27 | +# Usage |
27 | 28 | ```js
|
28 |
| -const o2diff = require('o2diff') |
| 29 | +import { diff, diffPaths, diffValues } from 'o2diff' |
29 | 30 |
|
30 | 31 | const original = {
|
31 | 32 | firstName: 'John',
|
@@ -53,54 +54,120 @@ const current = {
|
53 | 54 | }
|
54 | 55 | }
|
55 | 56 |
|
56 |
| -o2diff.diff(original, current) // returns { left, right } with objects diff |
57 |
| -o2diff.diffValues(original, current) // returns { changed, added, deleted } with values diff |
58 |
| -o2diff.diffPaths(original, current) // returns { changed, added, deleted } with paths diff |
| 57 | +// objects diff |
| 58 | +diff(original, current) |
| 59 | +/* |
| 60 | +{ |
| 61 | + left: { |
| 62 | + firstName: 'John', |
| 63 | + lastName: 'Smith', |
| 64 | + |
| 65 | + phones: [{ type: 'home', value: '+12222' }] |
| 66 | + }, |
| 67 | + right: { |
| 68 | + firstName: 'Michael', |
| 69 | + age: 25, |
| 70 | + |
| 71 | + phones: [{ type: 'work', value: '+13333' }], |
| 72 | + address: { |
| 73 | + city: 'New York', |
| 74 | + location: { latitude: 40.730610, longitude: -73.935242 } |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | +*/ |
| 79 | + |
| 80 | +// values diff |
| 81 | +diffValues(original, current) |
| 82 | +/* |
| 83 | +{ |
| 84 | + changed: { |
| 85 | + firstName: 'Michael', |
| 86 | + |
| 87 | + phones: [{ type: 'home', value: '+12222' }] |
| 88 | + }, |
| 89 | + added: { |
| 90 | + age: 25, |
| 91 | + address: { |
| 92 | + city: 'New York', |
| 93 | + location: { latitude: 40.730610, longitude: -73.935242 } |
| 94 | + } |
| 95 | + }, |
| 96 | + deleted: { |
| 97 | + lastName: 'Smith' |
| 98 | + } |
| 99 | +} |
| 100 | +*/ |
| 101 | + |
| 102 | +// paths diff |
| 103 | +diffPaths(original, current) |
| 104 | +/* |
| 105 | +{ |
| 106 | + changed: [ |
| 107 | + 'firstName', |
| 108 | + 'email', |
| 109 | + 'phones[0].type', |
| 110 | + 'phones[0].value' |
| 111 | + ], |
| 112 | + added: [ |
| 113 | + 'age', |
| 114 | + 'address.city', |
| 115 | + 'address.location.latitude', |
| 116 | + 'address.location.longitude' |
| 117 | + ], |
| 118 | + deleted: [ |
| 119 | + 'lastName' |
| 120 | + ] |
| 121 | +} |
| 122 | +*/ |
59 | 123 | ```
|
60 | 124 |
|
61 |
| -### API |
| 125 | +# API |
62 | 126 |
|
63 |
| -##### diff(original, current) |
64 |
| -Returns the differences between `original` and `current`. |
| 127 | +### function diff(original: Input, current: Input): DiffResult |
| 128 | +Returns the difference between `original` and `current`. |
65 | 129 |
|
66 |
| - - `original` - the original object. |
67 |
| - - `current` - the current (actual) object. |
68 |
| - - returns `{ left, right }` object. |
| 130 | +- `original: Input` - the original object. |
| 131 | +- `current: Input` - the current (actual) object. |
| 132 | +- returns `{ left, right }: DiffResult` object. |
69 | 133 |
|
70 |
| -##### diffValues(original, current) |
71 |
| -Returns the added, changed and deleted values between `original` and `current`. |
| 134 | +### function diffValues(original: Input, current: Input): DiffValuesResult |
| 135 | +Returns added, changed and deleted values between `original` and `current`. |
72 | 136 |
|
73 |
| - - `original` - the original object. |
74 |
| - - `current` - the current (actual) object. |
75 |
| - - returns `{ changed, added, deleted }` object. |
| 137 | +- `original: Input` - the original object. |
| 138 | +- `current: Input` - the current (actual) object. |
| 139 | +- returns `{ changed, added, deleted }: DiffValuesResult` object. |
76 | 140 |
|
77 |
| -##### diffPaths(original, current) |
78 |
| -Returns the added, changed and deleted paths between `original` and `current`. |
| 141 | +### function diffPaths(original: Input, current: Input): DiffPathsResult |
| 142 | +Returns added, changed and deleted paths between `original` and `current`. |
79 | 143 |
|
80 |
| - - `original` - the original object. |
81 |
| - - `current` - the current (actual) object. |
82 |
| - - returns `{ changed, added, deleted }` object. |
| 144 | +- `original: Input` - the original object. |
| 145 | +- `current: Input` - the current (actual) object. |
| 146 | +- returns `{ changed, added, deleted }: DiffPathsResult` object. |
83 | 147 |
|
84 |
| -##### revert(dest, src, customizer) |
| 148 | +### function revert(dest: Input, src: Input, customizer: (d: unknown, s: unknown) => unknown): RecordUnknown | ArrayUnknown; |
85 | 149 | Reverts `dest` object to `src`, calls `customizer` for each `dest.path`.
|
86 | 150 |
|
87 |
| - - `dest` - the destination object. |
88 |
| - - `src` - the source object. |
89 |
| - - `customizer` - the function that is called for each `dest.path`. |
| 151 | +- `dest: Input` - the destination object. |
| 152 | +- `src: Input` - the source object. |
| 153 | +- `customizer: (d: unknown, s: unknown) => unknown)` - the function that is called for each `dest.path`. |
| 154 | +- returns a record or an array. |
90 | 155 |
|
91 |
| -##### getPaths(obj) |
92 |
| -Returns all the paths of the object. |
| 156 | +### function getPaths(obj: Input): string[] |
| 157 | +Returns all paths of the object. |
93 | 158 |
|
94 |
| - - `obj` - the object. |
| 159 | +- `obj: Input` - the source object. |
| 160 | +- returns the list of paths. |
95 | 161 |
|
96 |
| -##### omitPaths(obj, excludedPaths) |
| 162 | +### function omitPaths(obj: Input, excludedPaths: string[]): RecordUnknown | ArrayUnknown |
97 | 163 | Returns the object without `excludedPaths`.
|
98 | 164 |
|
99 |
| - - `obj` - the object. |
100 |
| - - `excludedPaths` - the array of paths to exclude. The path can be with mask: `*.name` or `name.*` to exclude only path started or ended with the name. |
| 165 | +- `obj: Input` - the source object. |
| 166 | +- `excludedPaths` - the array of paths to exclude. The path can be with mask: `*.name` or `name.*` to exclude only path started or ended with the name. |
| 167 | +- returns a record or an array. |
101 | 168 |
|
102 |
| -### License |
| 169 | +# License |
103 | 170 | Licensed under the MIT license.
|
104 | 171 |
|
105 |
| -### Author |
| 172 | +# Author |
106 | 173 | Alexander Mac
|
0 commit comments