
Security News
Feross on Risky Business Weekly Podcast: npm’s Ongoing Supply Chain Attacks
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
compare-lists
Advanced tools
Super efficiently compares two sorted lists (arrays, strings, anything that is iterable actually).
Super efficiently compares two sorted lists (arrays, strings, anything that is iterable actually).
npm install compare-lists --save
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.
const leftList = [
"documents/apples.txt",
"documents/funny-cats.mp4",
"documents/funny-dogs.avi",
"trash/linux.iso",
"trash/zebras.doc"
];
const rightList = [
"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!
Alternatively, you can ask for a "report" instead of handling events. Just keep in mind this will use more memory than simply handling events.
const report = compareLists({
left: leftList,
right: rightList,
compare: (left, right) => left.localeCompare(right),
returnReport: true
});
console.log(JSON.stringify(report, null, " "));
{
"missingInLeft": ["documents/taxes.doc"],
"missingInRight": ["documents/funny-dogs.avi"],
"matches": [
["documents/apples.txt", "documents/apples.txt"],
["documents/funny-cats.mp4", "documents/funny-cats.mp4"],
["trash/linux.iso", "trash/linux.iso"],
["trash/zebras.doc", "trash/zebras.doc"]
]
}
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.
const leftList = [{ name: "Morty Smith" }, { name: "Rick Sanchez" }];
const rightList = ["Morty Smith", "Mr. Poopy Butthole"];
const report = compareLists({
left: leftList,
right: rightList,
compare: (left, right) => left.name.localeCompare(right),
returnReport: true
});
console.log(JSON.stringify(report, null, " "));
Will output:
{
"missingInLeft": ["Mr. Poopy Butthole"],
"missingInRight": [{ "name": "Rick Sanchez" }],
"matches": [[{ "name": "Morty Smith" }, "Morty Smith"]]
}
The library works with any objects that implement the iterable protocol, including arrays, strings, maps, etc.
Here is an example of comparing characters in two strings:
const left = "abcdef";
const right = "abcxyz";
const report = compareLists({
left,
right,
compare: (left, right) => left.localeCompare(right)
});
console.log(JSON.stringify(report, null, " "));
Will output:
{
"missingInLeft": ["x", "y", "z"],
"missingInRight": ["d", "e", "f"],
"matches": [
["a", "a"],
["b", "b"],
["c", "c"]
]
}
There is also a handy compareIterators
function if you need it.
Don't forget the values need to be in ascending order!
import { compareIterators } from "compare-lists";
const leftIterator = function*() {
yield "a";
yield "b";
yield "c";
};
const rightIterator = function*() {
yield "a";
yield "c";
yield "d";
};
const report = compareIterators({
left: leftIterator(),
right: rightIterator(),
compare: (left, right) => left.localeCompare(right),
returnReport: true
});
console.log(JSON.stringify(report, null, " "));
Will output:
{
"missingInLeft": ["d"],
"missingInRight": ["b"],
"matches": [
["a", "a"],
["c", "c"]
]
}
Got an issue or a feature request? Log it.
Pull-requests are also welcome. 😸
FAQs
Super efficiently compares two sorted lists (arrays, strings, anything that is iterable actually).
We found that compare-lists demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
Research
/Security News
Ongoing npm supply chain attack spreads to DuckDB: multiple packages compromised with the same wallet-drainer malware.