@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 ADDED
@@ -0,0 +1,7 @@
1
+ const password = require('./password');
2
+
3
+ const util = {
4
+ password
5
+ }
6
+
7
+ module.exports = util;
package/package.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "@becollective/utils",
3
+ "version": "1.0.0",
4
+ "description": "Common utilities",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "NODE_ENV=localtest jest tests/*"
8
+ },
9
+ "author": "",
10
+ "license": "ISC"
11
+ }
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
+ });
package/tests/setup.js ADDED
@@ -0,0 +1,9 @@
1
+ process.env.NODE_ENV = 'localtest';
2
+
3
+ let hasListened = false;
4
+ if(!hasListened) {
5
+ hasListened = true;
6
+ process.on('unhandledRejection', (err) => {
7
+ console.error(err.stack);
8
+ });
9
+ }