Dev Build / cleanup (push) Canceled after 0s
Dev Build / typecheck (push) Canceled after 0s
Dev Build / build (linux-arm64, ubuntu-24.04-arm, linux-arm64) (push) Canceled after 0s
Dev Build / build (linux-x64, ubuntu-latest, linux-x64) (push) Canceled after 0s
Dev Build / build (win-x64, windows-latest, win32-x64) (push) Canceled after 0s
Dev Build / dispatch-dev-docker (push) Canceled after 0s
37 lines
1.3 KiB
Plaintext
37 lines
1.3 KiB
Plaintext
'use strict';
|
|
|
|
var fastestLevenshtein = require('fastest-levenshtein');
|
|
|
|
const getUsedThreshold = (password, entry, threshold) => {
|
|
const isPasswordToShort = password.length <= entry.length;
|
|
const isThresholdLongerThanPassword = password.length <= threshold;
|
|
const shouldUsePasswordLength = isPasswordToShort || isThresholdLongerThanPassword;
|
|
// if password is too small use the password length divided by 4 while the threshold needs to be at least 1
|
|
return shouldUsePasswordLength ? Math.ceil(password.length / 4) : threshold;
|
|
};
|
|
const findLevenshteinDistance = (password, rankedDictionary, threshold) => {
|
|
let foundDistance = 0;
|
|
const found = Object.keys(rankedDictionary).find(entry => {
|
|
const usedThreshold = getUsedThreshold(password, entry, threshold);
|
|
if (Math.abs(password.length - entry.length) > usedThreshold) {
|
|
return false;
|
|
}
|
|
const foundEntryDistance = fastestLevenshtein.distance(password, entry);
|
|
const isInThreshold = foundEntryDistance <= usedThreshold;
|
|
if (isInThreshold) {
|
|
foundDistance = foundEntryDistance;
|
|
}
|
|
return isInThreshold;
|
|
});
|
|
if (found) {
|
|
return {
|
|
levenshteinDistance: foundDistance,
|
|
levenshteinDistanceEntry: found
|
|
};
|
|
}
|
|
return {};
|
|
};
|
|
|
|
module.exports = findLevenshteinDistance;
|
|
//# sourceMappingURL=levenshtein.cjs.map
|