Commit c4a1886
feat: Add native Date object support to fast-json-patch
This commit adds proper handling for JavaScript Date objects in JSON Patch operations.
## Changes
### 1. Enhanced `_deepClone` function (src/helpers.ts)
- Added special handling for Date objects to preserve them as Date instances
- Changed from JSON.stringify/parse approach to recursive cloning
- Date objects are now cloned using `new Date(obj.getTime())` to maintain type and value
- Arrays and objects are recursively cloned to handle nested Date objects
### 2. Enhanced `_generate` function (src/duplex.ts)
- Added Date-specific comparison logic
- Date objects are now compared by value (using `getTime()`) instead of by reference
- When two Date objects have different timestamps, a replace patch is generated
- Preserves Date objects in the generated patches (not converted to ISO strings)
### 3. Added comprehensive test suite (test/spec/dateSpec.mjs)
- Tests for cloning Date objects
- Tests for comparing objects with Date properties
- Tests for nested Date objects in arrays and objects
- Real-world scenario test simulating calendar event workflows
## Benefits
- **Type Preservation**: Date objects remain as Date instances throughout patch operations
- **Correct Comparisons**: Dates are compared by value, not reference
- **Backward Compatible**: All existing tests pass (215 specs, 0 failures)
- **Real-world Use Case**: Solves issues when working with API responses containing dates
## Use Case
This fix addresses the common scenario where workflow contexts contain Date objects
(e.g., calendar events, timestamps, scheduled tasks) and need to generate accurate
diffs without converting dates to strings.
Example:
```javascript
const oldContext = { events: [] };
const newContext = {
events: [{
startTime: new Date('2025-11-15T10:00:00Z'),
endTime: new Date('2025-11-15T11:00:00Z')
}]
};
const patches = compare(oldContext, newContext);
// Patches now contain actual Date objects, not ISO strings
```
## Testing
All tests pass:
- New Date-specific tests: 11 specs, 0 failures
- Existing core tests: 215 specs, 0 failures
Co-authored-by: Claude <noreply@anthropic.com>1 parent 9d313ac commit c4a1886
8 files changed
Lines changed: 11116 additions & 10838 deletions
File tree
- commonjs
- module
- src
- test/spec
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
132 | 132 | | |
133 | 133 | | |
134 | 134 | | |
135 | | - | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
136 | 148 | | |
137 | 149 | | |
138 | 150 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
55 | | - | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
56 | 74 | | |
57 | 75 | | |
58 | 76 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
128 | 128 | | |
129 | 129 | | |
130 | 130 | | |
131 | | - | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
132 | 144 | | |
133 | 145 | | |
134 | 146 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
52 | | - | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
53 | 71 | | |
54 | 72 | | |
55 | 73 | | |
| |||
0 commit comments