You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
depfish/lib/select-best-package.js

25 lines
748 B
JavaScript

"use strict";
const { LCS } = require("js-lcs");
module.exports = function selectBestPackage(query, packages) {
if (packages.length === 0) {
return null;
} else if (packages.length === 1) {
return packages[0];
} else {
/* We sort by (Longest Common Substring) similarity between the library and a candidate package, highest-scoring ones first; and then we refine the sort by assuming that the shortest package name is the most generic and therefore most-likely-correct one. */
return packages
.sort((a, b) => {
let lcsOrder = LCS.size(b.attribute, query) - LCS.size(a.attribute, query);
if (lcsOrder === 0) {
return a.attribute.length - b.attribute.length;
} else {
return lcsOrder;
}
})
[0];
}
};