UNPKG

fast-array-diff

Version:

Implementation of paper 'An O(ND) Difference Algorithm and Its Variations' on array

22 lines (21 loc) 680 B
import bestSubSequence from './lcs'; export function diff(a, b, compareFunc) { if (compareFunc === void 0) { compareFunc = function (ia, ib) { return ia === ib; }; } var ret = { removed: [], added: [], }; bestSubSequence(a, b, compareFunc, function (type, oldArr, oldStart, oldEnd, newArr, newStart, newEnd) { if (type === 'add') { for (var i = newStart; i < newEnd; ++i) { ret.added.push(newArr[i]); } } else if (type === 'remove') { for (var i = oldStart; i < oldEnd; ++i) { ret.removed.push(oldArr[i]); } } }); return ret; }