diff options
Diffstat (limited to 'src/components/ui/input/scorePassword.ts')
-rw-r--r-- | src/components/ui/input/scorePassword.ts | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/components/ui/input/scorePassword.ts b/src/components/ui/input/scorePassword.ts new file mode 100644 index 000000000..59502e2b0 --- /dev/null +++ b/src/components/ui/input/scorePassword.ts | |||
@@ -0,0 +1,42 @@ | |||
1 | interface ILetters { | ||
2 | [key: string]: number; | ||
3 | } | ||
4 | |||
5 | interface IVariations { | ||
6 | [index: string]: boolean; | ||
7 | digits: boolean; | ||
8 | lower: boolean; | ||
9 | nonWords: boolean; | ||
10 | upper: boolean; | ||
11 | } | ||
12 | |||
13 | export function scorePasswordFunc(password: string): number { | ||
14 | let score = 0; | ||
15 | if (!password) { | ||
16 | return score; | ||
17 | } | ||
18 | |||
19 | // award every unique letter until 5 repetitions | ||
20 | const letters: ILetters = {}; | ||
21 | for (const element of password) { | ||
22 | letters[element] = (letters[element] || 0) + 1; | ||
23 | score += 5 / letters[element]; | ||
24 | } | ||
25 | |||
26 | // bonus points for mixing it up | ||
27 | const variations: IVariations = { | ||
28 | digits: /\d/.test(password), | ||
29 | lower: /[a-z]/.test(password), | ||
30 | nonWords: /\W/.test(password), | ||
31 | upper: /[A-Z]/.test(password), | ||
32 | }; | ||
33 | |||
34 | let variationCount = 0; | ||
35 | for (const key of Object.keys(variations)) { | ||
36 | variationCount += variations[key] === true ? 1 : 0; | ||
37 | } | ||
38 | |||
39 | score += (variationCount - 1) * 10; | ||
40 | |||
41 | return Math.round(score); | ||
42 | } | ||