Provide a set of methods to access the diff algorithm by steps, there are three methods that defines each step. You can use them to extend your own diff algorithm.
All examples for each step will use the following variables:
import * as Differs from '@balmanth/differs';
const base = 'Example'.split(''); // Base char array.
const input = 'eXAMPLe'.split(''); // Input char array.
// Comparator function.
const comparator = (base, input) => base === input;
Step 1, use this method to calculate the diff table.
const table = Differs.Core.getTable(base, input, comparator);
You can use a custom
comparator
function, for example, to work with complex objects.
- base, the base values array, usually is the original/unchanged value.
- input, the changed values array that will be compared with the base values array.
- comparator, the comparison function that is called at every comparison.
- Returns the diff table where you can extract the changes in the next step.
Step 2, use this method to transform the diff table into a change array.
const changes = Differs.Core.getChanges(base, input, compare, table);
After this step you can already know what was removed and/or added.
- base, the base values array, usually is the original/unchanged value.
- input, the changed values array that will be compared with the base values array.
- comparator, the comparison function that is called at every comparison.
- table, the comparison table calculated by the
getTable
method.
- Returns the change array where you can extract the patches in the next step.
Step 3, use this method to transform the change array into the patch array.
const patches = Differs.Core.getPatches(changes);
After this step you will be able to know what was removed, changed and/or added.
- changes, the changes array generated by the
getChanges
method.
- Returns the patch array where you can see in more details what is the diff between the base and input values.