-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (55 loc) · 1.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict';
//Config
const _config = {
inputTemplate: {
hasUppercase: /[A-Z]/,
hasLowercase: /[a-z]/,
hasDigits: /[0-9]/,
hasSpecials: /[_\W]/,
hasCyrilic: /[а-яё]/i
}
};
function passScore(val, minPassLength, analysis = ['hasUppercase', 'hasLowercase', 'hasDigits', 'hasSpecials']){
let power = 0,
result = {};
result.length = (val.length < minPassLength) ? 0 : val.length;
result.analysis = {};
analysis.forEach(elem => {
if(_config.inputTemplate[elem]){
result.analysis[elem] = _config.inputTemplate[elem].test(val);
}
});
power = setScorePassword(val, result.analysis, result.length);
result.power = power;
return result;
}
function setScorePassword(password, analyse, length){
let score = 0,
variationCount = 0,
letters = {},
i = 0;
if(!password){
return score;
}
for(i; i < length; i++){
letters[password[i]] = (letters[password[i]] || 0) + 1;
score += 5.0 / letters[password[i]];
}
for(var check in analyse){
variationCount += analyse[check] ? 1 : 0;
}
score += (variationCount - 1) * 10;
return parseInt(score);
}
function passAccept(passScore){
for (let key in passScore.analysis) {
if (!passScore.analysis[key]) {
return false;
}
}
if (!passScore.length) {
return false;
}
return true;
}
module.exports = {passScore, setScorePassword, passAccept}