Skip to content

Commit 65b2c19

Browse files
committed
feat: rename comparator back to objectHash and add example to readme
1 parent ef80451 commit 65b2c19

File tree

5 files changed

+96
-69
lines changed

5 files changed

+96
-69
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
22
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs
33

4-
name: Node.js CI
4+
name: CI Tests
55

66
on:
77
push:

README.md

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,68 @@
11
# generate-json-patch
22

3-
4-
Creates [RFC 6902](https://datatracker.ietf.org/doc/html/rfc6902/) compliant JSON Patch objects based on two given JSON objects.
3+
Create [RFC 6902](https://datatracker.ietf.org/doc/html/rfc6902/) compliant JSON Patch objects based on two given [JSON](https://www.ecma-international.org/publications-and-standards/standards/ecma-404/) objects with a configurable interface.
54

65
[![Version](https://img.shields.io/npm/v/generate-json-patch.svg)](https://npmjs.org/package/generate-json-patch)
76
[![Downloads/week](https://img.shields.io/npm/dw/generate-json-patch.svg)](https://npmjs.org/package/generate-json-patch)
7+
[![Tests](https://github.com/marcolink/generate-json-patch/workflows/CI%20Tests/badge.svg?branch=main)](https://github.com/marcolink/generate-json-patch/actions/workflows/test.yml)
88
[![License](https://img.shields.io/npm/l/generate-json-patch.svg)](https://github.com/marcoxlink/generate-json-patch/blob/main/package.json)
9+
[![TypeScript](https://img.shields.io/badge/%3C%2F%3E-TypeScript-%230074c1.svg)](http://www.typescriptlang.org/)
910

11+
# TL;DR
12+
- Can diff any two [JSON](https://www.ecma-international.org/publications-and-standards/standards/ecma-404/) compliant objects - returns differences as [JSON Patch](http://jsonpatch.com/).
13+
- Elegant array diffing by providing an `objectHash` to match array elements
14+
- Ignore specific keys by providing a `propertyFilter`
15+
- `move` operations are ALWAYS **appended at the end**, therefore, they can be ignored (if wanted) when the patch gets applied.
16+
- :paw_prints: ***Is it small?*** Zero dependencies - it's ~**7 KB** (uncompressed).
17+
- :crystal_ball: ***Is it fast?*** I haven't done any performance comparison yet.
18+
- :hatched_chick: ***Is it stable?*** Test coverage is high, but it's still in its early days - bugs are expected.
19+
- The interface is inspired by [jsondiffpatch](https://github.com/benjamine/jsondiffpatch)
20+
- **100%** Typescript
1021

1122
# Installation
23+
Works on node and browser environments.
1224
```bash
1325
npm install generate-json-patch
1426
```
1527

1628
# Usage
1729

1830
```typescript
19-
import {generateJSONPatch} from 'generate-json-patch';
31+
import { generateJSONPatch } from 'generate-json-patch';
2032

2133
const before = { name: "Berta", manufacturer: "Ford", type: "Granada", year: 1972 };
2234
const after = { name: "Berta", manufacturer: "Ford", type: "Granada", year: 1974 };
2335

24-
const patch = generateJSONPatch(before, after):
36+
const patch = generateJSONPatch(before, after);
2537

2638
console.log(patch) // => [{op: 'replace', path: '/year', value: 1974}]
2739
```
28-
2940

30-
> This project is inspired by https://github.com/benjamine/jsondiffpatch
41+
## Configuration
42+
43+
```typescript
44+
import { generateJSONPatch, JsonPatchConfig, JSONValue } from 'generate-json-patch';
45+
46+
generateJSONPatch({/*...*/}, {/*...*/}, {
47+
// called when comparing array elements
48+
objectHash: function(value: JSONValue, context: JsonPatchConfig) {
49+
return value.name
50+
},
51+
// called for every property on objects. Can be used to ignore sensitive or irrelevant
52+
// properties when comparing data.
53+
propertyFilter: function (propertyName: string, context: JsonPatchConfig) {
54+
return !['sensitiveProperty'].includes(propertyName);
55+
},
56+
array: {
57+
// When true, no move operations will be created.
58+
// The rersulting patch will not lead to identical objects,
59+
// as postions of array elements can be different!
60+
ignoreMove: true
61+
}
62+
});
63+
```
64+
65+
66+
> For more examples, check out the [tests](./src/index.spec.ts)
67+
68+

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "generate-json-patch",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"author": "Marco Link <[email protected]>",
55
"private": false,
66
"repository": "marcolink/generate-json-patch",
@@ -23,7 +23,10 @@
2323
"keywords": [
2424
"json",
2525
"diff",
26-
"patch"
26+
"patch",
27+
"compare",
28+
"generate",
29+
"create"
2730
],
2831
"scripts": {
2932
"build": "tsup src/index.ts",

src/index.spec.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ describe('a generate json patch function', () => {
127127
})
128128

129129
describe('with an array comparator', () => {
130-
it("throws when comparator is not a function", () => {
130+
it("throws when objectHash is not a function", () => {
131131
const before = [
132132
{id: 1, paramOne: "before"},
133133
]
@@ -137,7 +137,7 @@ describe('a generate json patch function', () => {
137137

138138
assert.throws(() => generateJSONPatch(before, after, {
139139
// @ts-ignore
140-
comparator: 'not-a-function'
140+
objectHash: 'not-a-function'
141141
}))
142142

143143
})
@@ -154,7 +154,7 @@ describe('a generate json patch function', () => {
154154
]
155155

156156
const patch = generateJSONPatch(before, after, {
157-
comparator: function (obj: any) {
157+
objectHash: function (obj: any) {
158158
return `${obj.id}`;
159159
}
160160
})
@@ -167,7 +167,7 @@ describe('a generate json patch function', () => {
167167
]);
168168
})
169169

170-
it("handles changes with custom comparator based on direction param", () => {
170+
it("handles changes with custom objectHash based on direction param", () => {
171171
const before = [
172172
{
173173
id: 1, value: 'before'
@@ -186,7 +186,7 @@ describe('a generate json patch function', () => {
186186
]
187187

188188
const patch = generateJSONPatch(before, after, {
189-
comparator: function (obj: any, context) {
189+
objectHash: function (obj: any, context) {
190190
if (obj.id === 1 && context.side === 'right') {
191191
return '4'
192192
}
@@ -249,7 +249,7 @@ describe('a generate json patch function', () => {
249249
]
250250

251251
const patch = generateJSONPatch(before, after, {
252-
comparator: function (obj: any) {
252+
objectHash: function (obj: any) {
253253
return `${obj.id}`;
254254
},
255255
})
@@ -281,7 +281,7 @@ describe('a generate json patch function', () => {
281281
]
282282

283283
const patch = generateJSONPatch(before, after, {
284-
comparator: function (obj: any) {
284+
objectHash: function (obj: any) {
285285
return `${obj.id}`;
286286
},
287287
})
@@ -316,7 +316,7 @@ describe('a generate json patch function', () => {
316316
]
317317

318318
const patch = generateJSONPatch(before, after, {
319-
comparator: function (obj: any) {
319+
objectHash: function (obj: any) {
320320
return `${obj.id}`;
321321
},
322322
})
@@ -351,7 +351,7 @@ describe('a generate json patch function', () => {
351351
]
352352

353353
const patch = generateJSONPatch(before, after, {
354-
comparator: function (obj: any) {
354+
objectHash: function (obj: any) {
355355
return `${obj.id}`;
356356
},
357357
array:{ignoreMove: true}

0 commit comments

Comments
 (0)