Permalink
Cannot retrieve contributors at this time
32 lines (30 sloc)
1.25 KB
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
codeql-action/node_modules/eslint-plugin-jsx-a11y/lib/util/getSuggestion.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | |
Object.defineProperty(exports, "__esModule", { | |
value: true | |
}); | |
exports["default"] = getSuggestion; | |
var _damerauLevenshtein = _interopRequireDefault(require("damerau-levenshtein")); | |
var _object = _interopRequireDefault(require("object.fromentries")); | |
// Minimum edit distance to be considered a good suggestion. | |
var THRESHOLD = 2; | |
/** | |
* Returns an array of suggestions given a word and a dictionary and limit of suggestions | |
* to return. | |
*/ | |
function getSuggestion(word) { | |
var dictionary = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; | |
var limit = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 2; | |
var distances = (0, _object["default"])(dictionary.map(function (dictionaryWord) { | |
var distance = (0, _damerauLevenshtein["default"])(word.toUpperCase(), dictionaryWord.toUpperCase()); | |
var steps = distance.steps; | |
return [dictionaryWord, steps]; | |
})); | |
return Object.keys(distances).filter(function (suggestion) { | |
return distances[suggestion] <= THRESHOLD; | |
}).sort(function (a, b) { | |
return distances[a] - distances[b]; | |
}) // Sort by distance | |
.slice(0, limit); | |
} | |
module.exports = exports.default; |