Skip to content

Commit ef4bf6a

Browse files
committed
update readme
1 parent 6ac571c commit ef4bf6a

File tree

2 files changed

+109
-42
lines changed

2 files changed

+109
-42
lines changed

README.md

Lines changed: 108 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<p align="center">
22
<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>
44
<p align="center">Works in Node.js and in the browser.</p>
55
<p align="center">
66
<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,21 +11,22 @@
1111
<h3 align="center"><a href="https://alexandermac.github.io/o2diff">Demo</a></h3>
1212
</p>
1313

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)
2021

21-
### Install
22+
# Install
2223
```bash
2324
$ pnpm i o2diff
2425
```
2526

26-
### Usage
27+
# Usage
2728
```js
28-
const o2diff = require('o2diff')
29+
import { diff, diffPaths, diffValues } from 'o2diff'
2930

3031
const original = {
3132
firstName: 'John',
@@ -53,54 +54,120 @@ const current = {
5354
}
5455
}
5556

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+
*/
59123
```
60124

61-
### API
125+
# API
62126

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`.
65129

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.
69133

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`.
72136

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.
76140

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`.
79143

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.
83147

84-
##### revert(dest, src, customizer)
148+
### function revert(dest: Input, src: Input, customizer: (d: unknown, s: unknown) => unknown): RecordUnknown | ArrayUnknown;
85149
Reverts `dest` object to `src`, calls `customizer` for each `dest.path`.
86150

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.
90155

91-
##### getPaths(obj)
92-
Returns all the paths of the object.
156+
### function getPaths(obj: Input): string[]
157+
Returns all paths of the object.
93158

94-
- `obj` - the object.
159+
- `obj: Input` - the source object.
160+
- returns the list of paths.
95161

96-
##### omitPaths(obj, excludedPaths)
162+
### function omitPaths(obj: Input, excludedPaths: string[]): RecordUnknown | ArrayUnknown
97163
Returns the object without `excludedPaths`.
98164

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.
101168

102-
### License
169+
# License
103170
Licensed under the MIT license.
104171

105-
### Author
172+
# Author
106173
Alexander Mac

docs-src/src/app/app.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class AppComponent implements AfterViewInit {
2424
inputSuccess = ''
2525
inputError = ''
2626
diffType: DiffType | undefined
27-
libVersion = (o2diff as any).getLibVersion()
27+
libVersion = o2diff.getLibVersion()
2828

2929
@ViewChild('original', { static: false }) original: any
3030
@ViewChild('current', { static: false }) current: any

0 commit comments

Comments
 (0)