Calculates delta of old list and new list.
This function implements the algorithm described in "A Technique for Isolating Differences Between Files" (Commun. ACM, April 1978, Volume 21, Number 4, Pages 264-268).
Items in the arrays are not compared directly. Instead, a substitute symbol is determined for each item by applying the provided function fnSymbol
to it. Items with strictly equal symbols are assumed to represent the same logical item:
fnSymbol(a) === fnSymbol(b) <=> a 'is logically the same as' bAs an additional constraint, casting the symbols to string should not modify the comparison result. If this second constraint is not met, this method might report more diffs than necessary.
If no symbol function is provided, a default implementation is used which applies JSON.stringify
to non-string items and reduces the strings to a hash code. It is not guaranteed that this default implementation fulfills the above constraint in all cases, but it is a compromise between implementation effort, generality and performance. If items are known to be non-stringifiable (e.g. because they may contain cyclic references) or when hash collisions are likely, an own symbol
function must be provided via the configuration object.
The result of the diff is a sequence of update operations, each consisting of a type
(either "insert"
, "delete"
or "replace"
when enabled via configuration object) and an index
. By applying the operations one after the other to the old array, it can be transformed to an array whose items are equal to the new array.
Param | Type | Default Value | Description |
---|---|---|---|
aOld | Array | Old Array | |
aNew | Array | New Array | |
vConfigOrSymbol? | object|function | Configuration object or a function to calculate substitute symbols for array items |
Method | Description |
---|