Skip to content

Commit f0a3e0f

Browse files
feat: add patch function
1 parent 3f7a7f7 commit f0a3e0f

File tree

11 files changed

+121
-8
lines changed

11 files changed

+121
-8
lines changed

.changeset/little-jokes-jam.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opentf/obj-diff": minor
3+
---
4+
5+
Added patch function.

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@
1212

1313
</div>
1414

15-
> The Fast, Accurate, JavaScript Objects Diffing Library.
15+
> The Fast, Accurate, JavaScript Objects Diffing & Patching Library.
1616
1717
**[LIVE DEMO](https://obj-diff.pages.dev/)**
1818

1919
## Features
2020

2121
- Deep Objects Diffing
2222

23-
- Supports More Object Types, eg: `Map`, `Set`
23+
- Patching
24+
25+
- Supports comparing more object types
2426

2527
- TypeScript Support
2628

@@ -198,6 +200,21 @@ diff(a, b);
198200
*/
199201
```
200202

203+
## Patching
204+
205+
You can apply the diff result onto the original object to get the modified object.
206+
207+
```js
208+
import { diff, patch } from '@opentf/obj-diff';
209+
210+
const a = {a: 1, b: 2};
211+
const b = {a: 2, c: 3};
212+
213+
const out = patch(a, diff(a, b));
214+
215+
assert.deepStrictEqual(out, b); // ok
216+
```
217+
201218
## Benchmark
202219

203220
```diff

apps/demo/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ function App() {
142142
<Box sx={{ display: "flex", justifyContent: "center", mt: 2 }}>
143143
<Card size="sm">
144144
<Typography level="body-lg">
145-
🚀 The Fast, Accurate, JavaScript Objects Diffing Library.
145+
🚀 The Fast, Accurate, JavaScript Objects Diffing & Patching Library.
146146
</Typography>
147147
</Card>
148148
</Box>

demo.png

-36 Bytes
Loading

packages/obj-diff/README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@
1212

1313
</div>
1414

15-
> The Fast, Accurate, JavaScript Objects Diffing Library.
15+
> The Fast, Accurate, JavaScript Objects Diffing & Patching Library.
1616
1717
**[LIVE DEMO](https://obj-diff.pages.dev/)**
1818

1919
## Features
2020

2121
- Deep Objects Diffing
2222

23-
- Supports More Object Types, eg: `Map`, `Set`
23+
- Patching
24+
25+
- Supports comparing more object types
2426

2527
- TypeScript Support
2628

@@ -198,6 +200,21 @@ diff(a, b);
198200
*/
199201
```
200202

203+
## Patching
204+
205+
You can apply the diff result onto the original object to get the modified object.
206+
207+
```js
208+
import { diff, patch } from '@opentf/obj-diff';
209+
210+
const a = {a: 1, b: 2};
211+
const b = {a: 2, c: 3};
212+
213+
const out = patch(a, diff(a, b));
214+
215+
assert.deepStrictEqual(out, b); // ok
216+
```
217+
201218
## Benchmark
202219

203220
```diff
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { diff, patch } from "../src";
2+
3+
describe("patch", () => {
4+
test("simple objects", () => {
5+
const a = { a: 1, b: 2 };
6+
const b = { a: 2, c: 3 };
7+
expect(patch(a, diff(a, b))).toEqual(b);
8+
});
9+
10+
test("simple arrays", () => {
11+
const a = [1, 2, 3, 4, 5];
12+
const b = [1, 3, 5];
13+
expect(patch(a, diff(a, b))).toEqual(b);
14+
});
15+
16+
test("deep objects", () => {
17+
const a = {
18+
foo: {
19+
bar: {
20+
a: ["a", "b"],
21+
b: 2,
22+
c: ["x", "y"],
23+
e: 100,
24+
},
25+
},
26+
buzz: "world",
27+
};
28+
29+
const b = {
30+
foo: {
31+
bar: {
32+
a: ["a"],
33+
b: 2,
34+
c: ["x", "y", "z"],
35+
d: "Hello, world!",
36+
},
37+
},
38+
buzz: "fizz",
39+
};
40+
expect(patch(a, diff(a, b))).toEqual(b);
41+
});
42+
});

packages/obj-diff/jsr.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"name": "@opentf/obj-diff",
3-
"version": "0.1.1",
3+
"version": "0.5.0",
44
"exports": "./src/index.ts"
55
}

packages/obj-diff/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@opentf/obj-diff",
3-
"description": "The Fast, Accurate, JavaScript Objects Diffing Library.",
3+
"description": "The Fast, Accurate, JavaScript Objects Diffing & Patching Library.",
44
"keywords": [
55
"diff",
66
"object",
@@ -61,5 +61,8 @@
6161
"tsup": "^8.0.2",
6262
"typescript": "^5.4.5",
6363
"typescript-eslint": "^7.7.1"
64+
},
65+
"dependencies": {
66+
"@opentf/std": "^0.11.0"
6467
}
6568
}

packages/obj-diff/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import diff from "./diff";
2+
import patch from "./patch";
23
import type { DiffResult } from "./types";
34

4-
export { diff, DiffResult };
5+
export { diff, patch, DiffResult };

packages/obj-diff/src/patch.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { clone, set, unset } from "@opentf/std";
2+
import { DiffResult } from "./types";
3+
4+
export default function patch(obj: object, patches: Array<DiffResult>) {
5+
const c = clone(obj);
6+
7+
for (const p of patches) {
8+
if (p.t === 1 || p.t === 2) {
9+
set(c, p.p, p.v);
10+
}
11+
12+
if (p.t === 0) {
13+
unset(c, p.p);
14+
}
15+
}
16+
17+
return c;
18+
}

pnpm-lock.yaml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)