Extract Updated Fields
Extracts only the fields that have changed between two objects, including deep/nested changes.
Import
import { extractUpdatedFields } from 'nhb-toolbox';
Usage
Basic Usage
const original = { name: 'John', age: 30 };
const updated = { name: 'John', age: 31 };
const changes = extractUpdatedFields(original, updated);
// Returns { age: 31 }
Nested Objects
const v1 = { user: { id: 1, profile: { name: 'Alice' } } };
const v2 = { user: { id: 1, profile: { name: 'Alice', active: true } } };
extractUpdatedFields(v1, v2);
// Returns { user: { profile: { active: true } } }
API Reference
Type Parameters
Name | Description |
---|---|
T | Type of the base object |
Parameters
Name | Type | Description |
---|---|---|
baseObject | T | Original reference object |
updatedObject | FlattenPartial<T> | Object containing potential updates |
Returns
FlattenPartial<T>
: New object containing only changed fields (shallow copy)
Behavior
- Value Comparison: Uses deep equality check to detect changes
- Nested Objects: Recursively compares nested object structures
- Empty Results: Returns empty object if no changes found
- Reference Safety: Never modifies input objects
Examples
Detecting Configuration Changes
const oldConfig = { theme: 'dark', timeout: 30 };
const newConfig = { theme: 'light', timeout: 30 };
extractUpdatedFields(oldConfig, newConfig);
// { theme: 'light' }
Partial Updates
const dbRecord = { id: 1, content: 'Hello', meta: { views: 0 } };
const update = { content: 'Updated', meta: { views: 1 } };
extractUpdatedFields(dbRecord, update);
// { content: 'Updated', meta: { views: 1 } }
Limitations
- Circular References: Will fail on circular structures
- Special Types: May not handle special objects (Date, RegExp, etc.) as expected
- Performance: Deep comparison is slower for large objects
- Arrays: Treats array replacements as single changes (does not diff array items)
Type Definitions
type GenericObject = Record<string, any>;
type FlattenPartial<T> = Partial<{ [K in keyof T]: T[K] }>;
Suggested Use Cases
- API patch requests
- State change detection
- Configuration management
- Audit logging
- Database updates