levenshtein.esm.js 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. import { distance } from 'fastest-levenshtein';
  2. const getUsedThreshold = (password, entry, threshold) => {
  3. const isPasswordToShort = password.length <= entry.length;
  4. const isThresholdLongerThanPassword = password.length <= threshold;
  5. const shouldUsePasswordLength = isPasswordToShort || isThresholdLongerThanPassword;
  6. // if password is too small use the password length divided by 4 while the threshold needs to be at least 1
  7. return shouldUsePasswordLength ? Math.ceil(password.length / 4) : threshold;
  8. };
  9. const findLevenshteinDistance = (password, rankedDictionary, threshold) => {
  10. let foundDistance = 0;
  11. const found = Object.keys(rankedDictionary).find(entry => {
  12. const usedThreshold = getUsedThreshold(password, entry, threshold);
  13. const foundEntryDistance = distance(password, entry);
  14. const isInThreshold = foundEntryDistance <= usedThreshold;
  15. if (isInThreshold) {
  16. foundDistance = foundEntryDistance;
  17. }
  18. return isInThreshold;
  19. });
  20. if (found) {
  21. return {
  22. levenshteinDistance: foundDistance,
  23. levenshteinDistanceEntry: found
  24. };
  25. }
  26. return {};
  27. };
  28. export { findLevenshteinDistance as default };
  29. //# sourceMappingURL=levenshtein.esm.js.map