@becollective/utils 1.0.4 → 1.1.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/bundle.js +21 -5
- package/date-time.js +15 -0
- package/index.js +2 -0
- package/package.json +7 -9
- package/password.js +0 -4
- package/tests/isUnderSixteen.test.js +26 -0
- package/tests/password.test.js +1 -7
package/bundle.js
CHANGED
|
@@ -10,9 +10,6 @@ var password = {
|
|
|
10
10
|
hasNumeral: function hasNumeral(input) {
|
|
11
11
|
return !!input.match(/[0-9]/);
|
|
12
12
|
},
|
|
13
|
-
hasSpecialCharacter: function hasSpecialCharacter(input) {
|
|
14
|
-
return !!input.match(/[!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]/);
|
|
15
|
-
},
|
|
16
13
|
validate: function validate(input) {
|
|
17
14
|
if (typeof input !== 'string') {
|
|
18
15
|
throw new Error('not-string');
|
|
@@ -24,7 +21,6 @@ var password = {
|
|
|
24
21
|
if (password.hasUppercase(input)) rules++;
|
|
25
22
|
if (password.hasLowerCase(input)) rules++;
|
|
26
23
|
if (password.hasNumeral(input)) rules++;
|
|
27
|
-
if (password.hasSpecialCharacter(input)) rules++;
|
|
28
24
|
|
|
29
25
|
if (rules < 3) {
|
|
30
26
|
throw new Error('invalid-minimum-rules');
|
|
@@ -42,7 +38,27 @@ var password = {
|
|
|
42
38
|
}
|
|
43
39
|
};
|
|
44
40
|
|
|
41
|
+
var moment = require('moment');
|
|
42
|
+
/**
|
|
43
|
+
* Check if a user requires parent(guardian) consent
|
|
44
|
+
* i.e. Is under the age of 16 years, based on GDPR
|
|
45
|
+
* https://gdpr-info.eu/art-8-gdpr/
|
|
46
|
+
* @param {*} dateOfBirth moment.js compatible date time
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
var isUnderSixteen = function isUnderSixteen(dateOfBirth) {
|
|
51
|
+
var age = moment().diff(dateOfBirth, 'years');
|
|
52
|
+
|
|
53
|
+
if (isNaN(age)) {
|
|
54
|
+
throw new Error('Invalid date');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return age < 16;
|
|
58
|
+
};
|
|
59
|
+
|
|
45
60
|
var util = {
|
|
46
|
-
password: password
|
|
61
|
+
password: password,
|
|
62
|
+
isUnderSixteen: isUnderSixteen
|
|
47
63
|
};
|
|
48
64
|
module.exports = util;
|
package/date-time.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const moment = require('moment');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Check if a user requires parent(guardian) consent
|
|
5
|
+
* i.e. Is under the age of 16 years, based on GDPR
|
|
6
|
+
* https://gdpr-info.eu/art-8-gdpr/
|
|
7
|
+
* @param {*} dateOfBirth moment.js compatible date time
|
|
8
|
+
*/
|
|
9
|
+
export const isUnderSixteen = dateOfBirth => {
|
|
10
|
+
const age = moment().diff(dateOfBirth, 'years');
|
|
11
|
+
if (isNaN(age)) {
|
|
12
|
+
throw new Error('Invalid date');
|
|
13
|
+
}
|
|
14
|
+
return age < 16;
|
|
15
|
+
};
|
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,26 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@becollective/utils",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Common utilities",
|
|
5
5
|
"main": "bundle.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "NODE_ENV=localtest ./node_modules/.bin/jest tests/*",
|
|
8
|
-
"build": "./node_modules/.bin/rollup -c"
|
|
9
|
-
|
|
10
|
-
"husky": {
|
|
11
|
-
"hooks": {
|
|
12
|
-
"pre-push": "npm run build; npm test"
|
|
13
|
-
}
|
|
7
|
+
"test": "NODE_ENV=localtest npm run build && ./node_modules/.bin/jest tests/*",
|
|
8
|
+
"build": "./node_modules/.bin/rollup -c",
|
|
9
|
+
"prepublish": "npm run build; npm test"
|
|
14
10
|
},
|
|
15
11
|
"author": "",
|
|
16
12
|
"license": "ISC",
|
|
17
13
|
"devDependencies": {
|
|
18
14
|
"@babel/core": "^7.1.6",
|
|
19
15
|
"@babel/preset-env": "^7.1.6",
|
|
20
|
-
"husky": "^1.1.4",
|
|
21
16
|
"jest": "^23.6.0",
|
|
22
17
|
"rollup": "^0.67.1",
|
|
23
18
|
"rollup-plugin-babel": "^4.0.3",
|
|
24
19
|
"rollup-plugin-node-resolve": "^3.4.0"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"moment": "^2.24.0"
|
|
25
23
|
}
|
|
26
24
|
}
|
package/password.js
CHANGED
|
@@ -8,9 +8,6 @@ export const password = {
|
|
|
8
8
|
hasNumeral: (input) => {
|
|
9
9
|
return !!input.match(/[0-9]/);
|
|
10
10
|
},
|
|
11
|
-
hasSpecialCharacter: (input) => {
|
|
12
|
-
return !!input.match(/[!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]/);
|
|
13
|
-
},
|
|
14
11
|
validate: (input) => {
|
|
15
12
|
if(typeof input !== 'string') {
|
|
16
13
|
throw new Error('not-string');
|
|
@@ -22,7 +19,6 @@ export const password = {
|
|
|
22
19
|
if(password.hasUppercase(input)) rules++;
|
|
23
20
|
if(password.hasLowerCase(input)) rules++;
|
|
24
21
|
if(password.hasNumeral(input)) rules++;
|
|
25
|
-
if(password.hasSpecialCharacter(input)) rules++;
|
|
26
22
|
if(rules < 3) {
|
|
27
23
|
throw new Error('invalid-minimum-rules');
|
|
28
24
|
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
const { isUnderSixteen } = require('../bundle.js');
|
|
2
|
+
const moment = require('moment');
|
|
3
|
+
|
|
4
|
+
describe('isUnderSixteen', () => {
|
|
5
|
+
const refPoint = moment().subtract(16, 'years');
|
|
6
|
+
|
|
7
|
+
test('Is not U16 if dob is 16 years ago', () => {
|
|
8
|
+
const dob = moment(refPoint);
|
|
9
|
+
|
|
10
|
+
expect(isUnderSixteen(dob.subtract(1, 'second').utc().format())).toBe(false);
|
|
11
|
+
expect(isUnderSixteen(dob.subtract(100, 'year').utc().format())).toBe(false);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test('Is U16 if dob is less than 16 years ago', () => {
|
|
15
|
+
const dob = moment(refPoint);
|
|
16
|
+
|
|
17
|
+
expect(isUnderSixteen(dob.add(1, 'second').utc().format())).toBe(true);
|
|
18
|
+
expect(isUnderSixteen(dob.add(100, 'year').utc().format())).toBe(true);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test('Throws Error if dob is not a valid date-string', () => {
|
|
22
|
+
expect(() => isUnderSixteen(notDefined)).toThrow(/not defined/);
|
|
23
|
+
expect(() => isUnderSixteen(null)).toThrow(/Invalid date/);
|
|
24
|
+
expect(() => isUnderSixteen('not-valid')).toThrow(/Invalid date/);
|
|
25
|
+
});
|
|
26
|
+
});
|
package/tests/password.test.js
CHANGED
|
@@ -2,7 +2,7 @@ const util = require('../bundle.js');
|
|
|
2
2
|
|
|
3
3
|
describe('password.valiate', () => {
|
|
4
4
|
test('validate a valid password', async () => {
|
|
5
|
-
const password = '
|
|
5
|
+
const password = 'Password123';
|
|
6
6
|
let result;
|
|
7
7
|
try {
|
|
8
8
|
result = util.password.validate(password);
|
|
@@ -35,12 +35,6 @@ describe('password.valiate', () => {
|
|
|
35
35
|
util.password.validate(password);
|
|
36
36
|
}).not.toThrow('invalid-minimum-rules');
|
|
37
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
38
|
test('validate a missing password property', async () => {
|
|
45
39
|
expect(() => {
|
|
46
40
|
util.password.validate();
|