roc
Version:
Build modern web applications easily
60 lines (45 loc) • 1.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = getSuggestions;
var _leven = require('leven');
var _leven2 = _interopRequireDefault(_leven);
var _chalk = require('chalk');
var _chalk2 = _interopRequireDefault(_chalk);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Will create a string with suggestions for possible typos.
*
* @param {string[]} current - The current values that might be incorrect.
* @param {string[]} possible - All the possible correct values.
* @param {string} [prefix=''] - Something that the suggestion should be prefixed with. Useful for CLI options.
*
* @returns {string} - A string with possible suggestions for typos.
*/
function getSuggestions(current, possible) {
let prefix = arguments.length <= 2 || arguments[2] === undefined ? '' : arguments[2];
const info = [];
current.forEach(currentKey => {
let shortest = 0;
let closest;
for (let key of possible) {
let distance = (0, _leven2.default)(currentKey, key);
if (distance <= 0 || distance > 4) {
continue;
}
if (shortest && distance >= shortest) {
continue;
}
closest = key;
shortest = distance;
}
if (closest) {
info.push('Did not understand ' + _chalk2.default.underline(prefix + currentKey) + ' - Did you mean ' + _chalk2.default.underline(prefix + closest));
} else {
info.push('Did not understand ' + _chalk2.default.underline(prefix + currentKey));
}
});
return info.join('\n');
}
//# sourceMappingURL=get-suggestions.js.map