feat: Add native Date object support to fast-json-patch #330
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add Native Date Object Support
Summary
This PR adds proper handling for JavaScript Date objects in JSON Patch operations. Currently,
fast-json-patchconverts Date objects to ISO strings during cloning, which causes type loss and incorrect comparisons. This PR fixes that by preserving Date objects throughout the patching process.Problem
When using
compare()on objects containing Date objects:Root Causes:
_deepCloneusesJSON.stringify/parse- This converts Date objects to ISO strings===), not by valueSolution
1. Enhanced
_deepCloneFunctionnew Date(obj.getTime())Before:
After:
2. Enhanced
_generateFunctiongetTime()replacepatchesBefore:
After:
Changes
src/helpers.ts_deepClonewith Date supportsrc/duplex.ts_generatetest/spec/dateSpec.mjsTesting
New Tests (11 specs)
✅ Clone Date objects correctly
✅ Clone objects containing Date objects
✅ Clone arrays with Date objects
✅ Detect Date changes in
compare()✅ No patches for identical Dates
✅ Handle Date objects in nested structures
✅ Detect Date additions
✅ Detect Date removals
✅ Apply Date patches with
applyPatch()✅ Real-world calendar events scenario
Existing Tests
✅ All 215 existing tests pass (core, duplex, validate)
Real-World Use Case
This fix is crucial for workflows that handle:
Example: Workflow Context Diffing
Performance
Backward Compatibility
✅ Fully backward compatible
Related Issues
This fixes issues where:
Checklist
Would love feedback on: