"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]; } };