@becollective/utils 1.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.
- package/index.js +7 -0
- package/package.json +11 -0
- package/password.js +32 -0
- package/tests/password.test.js +44 -0
- package/tests/setup.js +9 -0
package/index.js
ADDED
package/package.json
ADDED
package/password.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const password = {
|
|
2
|
+
hasUppercase: (input) => {
|
|
3
|
+
return !!input.match(/[A-Z]/);
|
|
4
|
+
},
|
|
5
|
+
hasLowerCase: (input) => {
|
|
6
|
+
return !!input.match(/[a-z]/);
|
|
7
|
+
},
|
|
8
|
+
hasNumeral: (input) => {
|
|
9
|
+
return !!input.match(/[0-9]/);
|
|
10
|
+
},
|
|
11
|
+
hasSpecialCharacter: (input) => {
|
|
12
|
+
return !!input.match(/[!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]/);
|
|
13
|
+
},
|
|
14
|
+
validate: (input) => {
|
|
15
|
+
if(input.length < 8) {
|
|
16
|
+
throw new Error('invalid-length');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
let rules = 0;
|
|
20
|
+
if(password.hasUppercase(input)) rules++;
|
|
21
|
+
if(password.hasLowerCase(input)) rules++;
|
|
22
|
+
if(password.hasNumeral(input)) rules++;
|
|
23
|
+
if(password.hasSpecialCharacter(input)) rules++;
|
|
24
|
+
if(rules < 3) {
|
|
25
|
+
throw new Error('invalid-minimum-rules');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
module.exports = password;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
const util = require('../index.js');
|
|
2
|
+
|
|
3
|
+
describe('password.valiate', () => {
|
|
4
|
+
test('validate a valid password', async () => {
|
|
5
|
+
const password = 'eihe1soKah@c';
|
|
6
|
+
let result;
|
|
7
|
+
try {
|
|
8
|
+
result = util.password.validate(password);
|
|
9
|
+
} catch(e) {
|
|
10
|
+
result = e;
|
|
11
|
+
}
|
|
12
|
+
expect(result).toBe(true);
|
|
13
|
+
}, 1000);
|
|
14
|
+
test('validate a short password', async () => {
|
|
15
|
+
const password = 'foobar';
|
|
16
|
+
expect(() => {
|
|
17
|
+
util.password.validate(password);
|
|
18
|
+
}).toThrow('invalid-length');
|
|
19
|
+
}, 1000);
|
|
20
|
+
test('validate a one rule password', async () => {
|
|
21
|
+
const password = 'foobarab';
|
|
22
|
+
expect(() => {
|
|
23
|
+
util.password.validate(password);
|
|
24
|
+
}).toThrow('invalid-minimum-rules');
|
|
25
|
+
}, 1000);
|
|
26
|
+
test('validate a two rule password', async () => {
|
|
27
|
+
const password = 'foobar123';
|
|
28
|
+
expect(() => {
|
|
29
|
+
util.password.validate(password);
|
|
30
|
+
}).toThrow('invalid-minimum-rules');
|
|
31
|
+
}, 1000);
|
|
32
|
+
test('validate a three rule password', async () => {
|
|
33
|
+
const password = 'foobar123M';
|
|
34
|
+
expect(() => {
|
|
35
|
+
util.password.validate(password);
|
|
36
|
+
}).not.toThrow('invalid-minimum-rules');
|
|
37
|
+
}, 1000);
|
|
38
|
+
test('validate a four rule password', async () => {
|
|
39
|
+
const password = 'foobar123M@';
|
|
40
|
+
expect(() => {
|
|
41
|
+
util.password.validate(password);
|
|
42
|
+
}).not.toThrow('invalid-minimum-rules');
|
|
43
|
+
}, 1000);
|
|
44
|
+
});
|