Super efficiently compares two sorted lists (arrays, strings, anything that is iterable actually).
Install
npm install compare-lists --save
Usage
import{compareLists}from"compare-lists";
Say you have two sorted lists of filenames and you need to know which files exist in the left list, which exist in the right list, and which exist in both lists.
constleftList=[
"documents/apples.txt",
"documents/funny-cats.mp4",
"documents/funny-dogs.avi",
"trash/linux.iso",
"trash/zebras.doc"
];
constrightList=[
"documents/apples.txt",
"documents/funny-cats.mp4",
"documents/taxes.doc",
"trash/linux.iso",
"trash/zebras.doc"
];
Just call compareLists passing both the lists, a function to compare items in each list and handlers for any events that you're interested in.
compareLists({
left:leftList,
right:rightList,
compare:(left,right)=>left.localeCompare(right),
onMatch:value=>console.log(`"${value}" is found in both lists`),
onMissingInLeft:right=>
console.log(`"${right}" is missing in the left list`),
onMissingInRight:left=>
console.log(`"${left}" is missing in the right list`)
});
The output will be:
"documents/apples.txt" is found in both lists
"documents/funny-cats.mp4" is found in both lists
"documents/funny-dogs.avi" is missing in the right list
"documents/taxes.doc" is missing in the left list
"trash/linux.iso" is found in both lists
"trash/zebras.doc" is found in both lists
That's the basics!
More...
Simple API
Alternatively, you can ask for a "report" instead of handling events. Just keep in mind this will use more memory than simply handling events.
The left and right lists do not need to be the same type as each other. Just remember the items still need to by sorted by the field you are comparing on.