@coat/cli
Version:
TODO: See #3
35 lines (33 loc) • 1.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.findPotentialPropertyMatch = findPotentialPropertyMatch;
var _leven = _interopRequireDefault(require("leven"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Finds a potential property misspelling using
* the Levenshtein distance algorithm.
*
* @param targetProperty The unknown property that might have a spelling error
* @param possibleProperties Potential properties that might have been misspelled
* @returns The best matching property or null if none has been found
*/
function findPotentialPropertyMatch(targetProperty, possibleProperties) {
let bestPropertyMatch = {
prop: "",
distance: Infinity
};
for (const possibleProp of possibleProperties) {
const distance = (0, _leven.default)(targetProperty, possibleProp);
if (distance < bestPropertyMatch.distance) {
bestPropertyMatch = {
prop: possibleProp,
distance
};
}
}
// We allow a maximum distance of 2 between strings to
// not be too lax on offering a potential property match
return bestPropertyMatch.distance < 3 ? bestPropertyMatch.prop : null;
}