blob: 0b7719ec1fca43886e6032fbc47dcee7506c8cc1 (
plain) (
blame)
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
|
interface ILetters {
[key: string]: number;
}
interface IVariations {
[index: string]: boolean;
digits: boolean;
lower: boolean;
nonWords: boolean;
upper: boolean;
}
export function scorePasswordFunc(password: string): number {
let score: number = 0;
if (!password) {
return score;
}
// award every unique letter until 5 repetitions
const letters: ILetters = {};
for (let i = 0; i < password.length; i += 1) {
letters[password[i]] = (letters[password[i]] || 0) + 1;
score += 5.0 / letters[password[i]];
}
// bonus points for mixing it up
const variations: IVariations = {
digits: /\d/.test(password),
lower: /[a-z]/.test(password),
nonWords: /\W/.test(password),
upper: /[A-Z]/.test(password),
};
let variationCount = 0;
Object.keys(variations).forEach((key) => {
variationCount += (variations[key] === true) ? 1 : 0;
});
score += (variationCount - 1) * 10;
return Math.round(score);
}
|