Skip to content

Commit 79bdcdb

Browse files
docs: update readme
1 parent 03ba9e0 commit 79bdcdb

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

packages/obj-diff/README.md

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
- Patching
2424

25-
- Supports comparing more object types
25+
- Supports comparing custom object types
2626

2727
- TypeScript Support
2828

@@ -215,6 +215,66 @@ const out = patch(a, diff(a, b));
215215
assert.deepStrictEqual(out, b); // ok
216216
```
217217

218+
## Comparing Custom Types
219+
220+
By default, the `diff` function cannot compare every object types other than the list above.
221+
222+
You can extend the default `diff` function using the `diffWith` function.
223+
224+
Now you can compare any object types of your own.
225+
226+
### Usage - diffWith()
227+
```js
228+
diff(
229+
obj1: object,
230+
obj2: object,
231+
fn: (a: object, b: object) => boolean | undefined
232+
): Array<DiffResult>
233+
```
234+
235+
### Examples
236+
237+
Let us compare the `MongoDB` bson `ObjectId` objects.
238+
239+
```js
240+
import { ObjectId } from "bson";
241+
import { diffWith } from "../src";
242+
243+
const record1 = {
244+
_id: new ObjectId(),
245+
title: "Article 1",
246+
desc: "The article description.",
247+
};
248+
249+
const record2 = {
250+
_id: new ObjectId(),
251+
title: "Article 1",
252+
desc: "The new article description.",
253+
};
254+
255+
const result = diffWith(record1, record2, (a, b) => {
256+
if (a instanceof ObjectId && b instanceof ObjectId) {
257+
return a.toString() !== b.toString();
258+
}
259+
});
260+
261+
console.log(result);
262+
/*
263+
[
264+
{
265+
t: 2,
266+
p: [ "_id" ],
267+
v: new ObjectId('663088b877dd3c9aaec482d4'),
268+
},
269+
{
270+
t: 2,
271+
p: [ "desc" ],
272+
v: "The new article description.",
273+
}
274+
]
275+
*/
276+
```
277+
218278
## Benchmark
219279

220280
```diff

0 commit comments

Comments
 (0)