Function: diff()
Call Signature
diff<
T,TId>(oldItems,newItems,getId,isEqual?):DiffResult<T>
Defined in: collection/diff/index.ts:45
Compares two collections and returns added, updated, removed, and unchanged items.
Useful for syncing data, detecting changes, and implementing optimistic updates.
Type Parameters
T
T
TId
TId extends string | number
Parameters
oldItems
readonly T[]
The original collection
newItems
readonly T[]
The updated collection
getId
(item) => TId
Function to extract unique identifier
isEqual?
Comparator<T>
Optional custom equality checker (defaults to deep equality)
Returns
DiffResult<T>
A DiffResult containing categorized changes
Example
// Data-first
const oldUsers = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' }
]
const newUsers = [
{ id: 1, name: 'Alicia' }, // updated
{ id: 3, name: 'Charlie' } // added
]
diff(oldUsers, newUsers, (u) => u.id)
// => {
// added: [{ id: 3, name: 'Charlie' }],
// updated: [{ old: { id: 1, name: 'Alice' }, new: { id: 1, name: 'Alicia' } }],
// removed: [{ id: 2, name: 'Bob' }],
// unchanged: []
// }
// Data-last (in pipe)
pipe(
oldUsers,
diff(newUsers, (u) => u.id)
)
See
- union - for merging collections
- intersect - for finding common elements
Call Signature
diff<
T,TId>(newItems,getId,isEqual?): (oldItems) =>DiffResult<T>
Defined in: collection/diff/index.ts:51
Compares two collections and returns added, updated, removed, and unchanged items.
Useful for syncing data, detecting changes, and implementing optimistic updates.
Type Parameters
T
T
TId
TId extends string | number
Parameters
newItems
readonly T[]
The updated collection
getId
(item) => TId
Function to extract unique identifier
isEqual?
Comparator<T>
Optional custom equality checker (defaults to deep equality)
Returns
A DiffResult containing categorized changes
(
oldItems):DiffResult<T>
Parameters
oldItems
readonly T[]
Returns
DiffResult<T>
Example
// Data-first
const oldUsers = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' }
]
const newUsers = [
{ id: 1, name: 'Alicia' }, // updated
{ id: 3, name: 'Charlie' } // added
]
diff(oldUsers, newUsers, (u) => u.id)
// => {
// added: [{ id: 3, name: 'Charlie' }],
// updated: [{ old: { id: 1, name: 'Alice' }, new: { id: 1, name: 'Alicia' } }],
// removed: [{ id: 2, name: 'Bob' }],
// unchanged: []
// }
// Data-last (in pipe)
pipe(
oldUsers,
diff(newUsers, (u) => u.id)
)
See
- union - for merging collections
- intersect - for finding common elements