Repository URL to install this package:
|
Version:
3.12.20 ▾
|
import levenshtein from 'js-levenshtein';
export default function getClosestStr(str, options) {
if (str) {
var closestValue = (options || []).filter(function (option) {
return option && typeof option === 'string';
}).reduce(function (acum, currentOption) {
var value = currentOption || '';
var distance = levenshtein((str || '').toLowerCase(), value.toLowerCase());
if (!acum || distance < (acum === null || acum === void 0 ? void 0 : acum.distance)) {
return {
value: value,
distance: distance
};
}
return acum;
}, null);
if (closestValue !== null && closestValue !== void 0 && closestValue.value) {
return closestValue.value;
}
}
return null;
}