@fr0st/ui-passwordstrength 2.0.8 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,162 +0,0 @@
1
- import $ from '@fr0st/query';
2
-
3
- /**
4
- * Find character sequences in a string.
5
- * @param {string} string The input string.
6
- * @param {array} locations The character locations.
7
- * @return {array} The character sequences.
8
- */
9
- function findSequences(string, locations) {
10
- const sequences = [];
11
- let sequence = [];
12
-
13
- for (let i = 0; i < locations.length - 1; i++) {
14
- const current = locations[i];
15
- const next = locations[i + 1];
16
- const distance = next - current;
17
-
18
- const char = string[current];
19
- const nextChar = string[next];
20
- const charDistance = nextChar.charCodeAt(0) - char.charCodeAt(0);
21
-
22
- if (distance === 1 && charDistance === 1) {
23
- if (!sequence.length) {
24
- sequence.push(char);
25
- }
26
- sequence.push(nextChar);
27
- } else if (sequence.length) {
28
- sequences.push(sequence);
29
- sequence = [];
30
- }
31
- }
32
-
33
- if (sequence.length) {
34
- sequences.push(sequence);
35
- }
36
-
37
- return sequences;
38
- };
39
-
40
- /**
41
- * Get the strength of a password.
42
- * @param {string} password The password.
43
- * @return {number} The password strength.
44
- */
45
- export function getStrength(password) {
46
- if (password.match(/^password/i)) {
47
- password = password.substring(8);
48
- }
49
-
50
- const length = password.length;
51
-
52
- let score = 0;
53
-
54
- if (length) {
55
- score += length * 6;
56
- }
57
-
58
- const upper = [];
59
- const lower = [];
60
- const numbers = [];
61
- const symbols = [];
62
- const dictionary = {};
63
-
64
- for (let i = 0; i < length; i++) {
65
- const char = password[i];
66
- const code = char.charCodeAt(0);
67
-
68
- if (code >= 48 && code <= 57) {
69
- numbers.push(i);
70
- } else if (code >= 65 && code <= 90) {
71
- upper.push(i);
72
- } else if (code >= 97 && code <= 122) {
73
- lower.push(i);
74
- } else {
75
- symbols.push(i);
76
- }
77
-
78
- if (!(char in dictionary)) {
79
- dictionary[char] = 0;
80
- }
81
-
82
- dictionary[char]++;
83
- }
84
-
85
- // reward upper/lower case
86
- if (upper.length !== length && lower.length !== length) {
87
- if (upper.length) {
88
- score += length - upper.length;
89
- }
90
-
91
- if (lower.length) {
92
- score += length - lower.length;
93
- }
94
- }
95
-
96
- // reward numbers
97
- if (numbers.length !== length) {
98
- score += numbers.length * 3;
99
- }
100
-
101
- // reward symbols
102
- score += symbols.length * 6;
103
-
104
- // reward inner numbers/symbols
105
- for (const list of [numbers, symbols]) {
106
- const reward = list.filter((v) => v && v !== length - 1).length;
107
- score += reward * 3;
108
- }
109
-
110
- // punish only letters
111
- if (!numbers.length && !symbols.length) {
112
- score -= length;
113
- }
114
-
115
- // punish only numbers
116
- if (!upper.length && !lower.length && !symbols.length) {
117
- score -= length;
118
- }
119
-
120
- // punish no symbols
121
- if (!symbols.length) {
122
- score -= length;
123
- }
124
-
125
- // repeating characters
126
- const repeats = Object.values(dictionary)
127
- .reduce((acc, count) => acc + count - 1, 0);
128
-
129
- if (repeats > 0) {
130
- score -= repeats;
131
- }
132
-
133
- if (length >= 2) {
134
- // consecutive characters
135
- const matches = password.matchAll(/(.)\1+/g);
136
-
137
- for (const match of matches) {
138
- score -= Math.min(match[0].length, 12) * 3;
139
- }
140
-
141
- const lowerPassword = password.toLowerCase();
142
-
143
- // sequential letters
144
- const letters = upper.concat(lower);
145
- const letterSequences = findSequences(lowerPassword, letters);
146
- for (const sequence of letterSequences) {
147
- if (sequence.length > 2) {
148
- score -= (Math.min(sequence.length - 2, 16)) * 3;
149
- }
150
- }
151
-
152
- // sequential numbers
153
- const numberSequences = findSequences(lowerPassword, numbers);
154
- for (const sequence of numberSequences) {
155
- if (sequence.length > 2) {
156
- score -= (Math.min(sequence.length - 2, 16)) * 3;
157
- }
158
- }
159
- }
160
-
161
- return $._clamp(score, 0, 100);
162
- };