Skip to content

feat: better support for patching#142

Merged
ssaarela merged 5 commits intomasterfrom
f/patch
Nov 28, 2025
Merged

feat: better support for patching#142
ssaarela merged 5 commits intomasterfrom
f/patch

Conversation

@ssaarela
Copy link
Collaborator

Old Diff was designed primarily for change detection but used also internally for patching. While path/value "Changes" can be sent over line and applied, they are not very good for that (i.e. result in unnecessary large payloads). While deletions from the head of an array, with this new DiffNode, still result in a large diff/patch, this feature addresses how new object/array values are handled: DiffNode.patch contains the new value as-is in case the old value is missing (or it's type has changed). Also deleted object values will not be "exploaded" into path/value -deletions. For example

const a = {};
const b = { nested: { foo: 'bar' } }

new DiffNode({ oldValue: a, newValue: b }).patch
// => [ { path: '$.nested', value: { foo: 'bar' } } ]

new DiffNode({ oldValue: b, newValue: a }).patch
// => [ { path: '$.nested' } ]

DiffNode.patch can be used to patch objects using Path.set just like the results of the Diff.changeset.

Signed-off-by: Samppa Saarela <samppa.saarela@iki.fi>
Old Diff was designed primarily for change detection but used also internally for patching. While path/value "Changes" can be sent over line and applied, they are not very good for that (i.e. result in unnecessary large payloads). While deletions from the head of an array, with this new DiffNode, still result in a large diff/patch, this feature addresses how new object/array values are handled: `DiffNode.patch` contains the new value as-is in case the old value is missing (or it's type has changed). Also deleted object values will not be "exploaded" into path/value -deletions. For example
```ts
const a = {};
const b = { nested: { foo: 'bar' } }

new DiffNode({ oldValue: a, newValue: b }).patch
// => [ { path: '$.nested', value: { foo: 'bar' } } ]

new DiffNode({ oldValue: b, newValue: a }).patch
// => [ { path: '$.nested' } ]
```

`DiffNode.patch` can be used to patch objects using `Path.set` just like the results of the `Diff.changeset`.

Signed-off-by: Samppa Saarela <samppa.saarela@iki.fi>
@codecov
Copy link

codecov bot commented Nov 27, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (c8006a8) to head (8315144).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##            master      #142    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files           20        21     +1     
  Lines         3486      3639   +153     
  Branches       705       746    +41     
==========================================
+ Hits          3486      3639   +153     
Files with missing lines Coverage Δ
packages/diff/src/Diff.ts 100.00% <100.00%> (ø)
packages/diff/src/DiffNode.ts 100.00% <100.00%> (ø)
packages/diff/src/VersionInfo.ts 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

NOTE: This commit introduces a slight alteration to the result of `VersionInfo.paths` for a case where there is no previous version or it's type has changed significantly and includeObjects is true: the root object is also included. This makes sense and fixes an oversight of not including it in the first place.
Signed-off-by: Samppa Saarela <samppa.saarela@iki.fi>
@ssaarela ssaarela marked this pull request as ready for review November 27, 2025 10:18
Signed-off-by: Samppa Saarela <samppa.saarela@iki.fi>
Copy link

@mlison mlison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, small comments but ultimately inconsequential

Signed-off-by: Samppa Saarela <samppa.saarela@iki.fi>
@ssaarela ssaarela merged commit b097c74 into master Nov 28, 2025
7 checks passed
@ssaarela ssaarela deleted the f/patch branch November 28, 2025 09:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants