node-vehicle-logos
Version:
A collection of vehicle manufacturer logos
34 lines • 1.44 kB
JavaScript
/**
* Finds the make that best matches the specified name in the provided list of makes.
* @param text The text to search
* @param makes The list of VehicleMake objects. An internal list is provided by {@link VehicleMakes}
* @param [match="full"] The type of match to execute
* @returns The VehicleMake if found, or undefined
*/
export function findMake(text, makes, match = "full") {
var _a, _b;
text = text.toLocaleUpperCase();
// Instead of using reduce or keeping track of all matches, track the best match only
const bestMatch = {
name: "",
make: undefined,
};
for (const make of makes) {
const allNames = [
make.name.toLocaleUpperCase(),
...(_b = (_a = make.altNames) === null || _a === void 0 ? void 0 : _a.map((x) => x.toLocaleUpperCase())) !== null && _b !== void 0 ? _b : [],
];
allNames.forEach((name) => {
const found = (match === "full" && text === name)
|| (match === "contains" && text.includes(name))
|| (match === "end" && text.endsWith(name))
|| (match === "start" && text.startsWith(name));
if (found && name.length > bestMatch.name.length) {
bestMatch.name = name;
bestMatch.make = make;
}
});
}
return bestMatch.make;
}
//# sourceMappingURL=functions.js.map