@becollective/utils 1.0.4 → 1.2.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 +49 -5
- package/date-time.js +39 -0
- package/index.js +3 -0
- package/package.json +7 -9
- package/password.js +0 -4
- package/tests/date-time.test.js +56 -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,55 @@ var password = {
|
|
|
42
38
|
}
|
|
43
39
|
};
|
|
44
40
|
|
|
41
|
+
var moment = require('moment');
|
|
42
|
+
/**
|
|
43
|
+
* Get current age based on the given birth date
|
|
44
|
+
* @param {string} dateOfBirth
|
|
45
|
+
* @param {string} unit the measurement of the the difference
|
|
46
|
+
* The supported measurements are:
|
|
47
|
+
* years, months, weeks, days, hours, minutes, and seconds.
|
|
48
|
+
* @returns {number} age
|
|
49
|
+
*/
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
var getAge = function getAge(dateOfBirth) {
|
|
53
|
+
var unit = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'years';
|
|
54
|
+
var age = moment().diff(dateOfBirth, unit);
|
|
55
|
+
|
|
56
|
+
if (isNaN(age)) {
|
|
57
|
+
throw new Error('Invalid date');
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return age;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Check if a user requires parent(guardian) consent
|
|
64
|
+
* i.e. Is under the age of 16 years, based on GDPR
|
|
65
|
+
* https://gdpr-info.eu/art-8-gdpr/
|
|
66
|
+
* @param {string} dateOfBirth moment.js compatible date time
|
|
67
|
+
* @returns {boolean}
|
|
68
|
+
* NOTE:
|
|
69
|
+
* We used to not allow child (under 16) to register,
|
|
70
|
+
* and birthdate is not a required field for ghost user
|
|
71
|
+
* So an existing user could have no birthdate, we assume these users are adults
|
|
72
|
+
* TODO:
|
|
73
|
+
* Remove above logic after we migrated all legacy
|
|
74
|
+
* data to have a consistent birthdate property.
|
|
75
|
+
* Then we should throw error if birthdate is not valid
|
|
76
|
+
*/
|
|
77
|
+
|
|
78
|
+
var isUnderSixteen = function isUnderSixteen(dateOfBirth) {
|
|
79
|
+
if (!dateOfBirth || !moment(dateOfBirth).isValid()) {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
var age = getAge(dateOfBirth);
|
|
84
|
+
return isNaN(age) ? false : age < 16;
|
|
85
|
+
};
|
|
86
|
+
|
|
45
87
|
var util = {
|
|
46
|
-
password: password
|
|
88
|
+
password: password,
|
|
89
|
+
isUnderSixteen: isUnderSixteen,
|
|
90
|
+
getAge: getAge
|
|
47
91
|
};
|
|
48
92
|
module.exports = util;
|
package/date-time.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
const moment = require('moment');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Get current age based on the given birth date
|
|
5
|
+
* @param {string} dateOfBirth
|
|
6
|
+
* @param {string} unit the measurement of the the difference
|
|
7
|
+
* The supported measurements are:
|
|
8
|
+
* years, months, weeks, days, hours, minutes, and seconds.
|
|
9
|
+
* @returns {number} age
|
|
10
|
+
*/
|
|
11
|
+
export const getAge = (dateOfBirth, unit='years') => {
|
|
12
|
+
const age = moment().diff(dateOfBirth, unit);
|
|
13
|
+
if (isNaN(age)) {
|
|
14
|
+
throw new Error('Invalid date');
|
|
15
|
+
}
|
|
16
|
+
return age;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Check if a user requires parent(guardian) consent
|
|
20
|
+
* i.e. Is under the age of 16 years, based on GDPR
|
|
21
|
+
* https://gdpr-info.eu/art-8-gdpr/
|
|
22
|
+
* @param {string} dateOfBirth moment.js compatible date time
|
|
23
|
+
* @returns {boolean}
|
|
24
|
+
* NOTE:
|
|
25
|
+
* We used to not allow child (under 16) to register,
|
|
26
|
+
* and birthdate is not a required field for ghost user
|
|
27
|
+
* So an existing user could have no birthdate, we assume these users are adults
|
|
28
|
+
* TODO:
|
|
29
|
+
* Remove above logic after we migrated all legacy
|
|
30
|
+
* data to have a consistent birthdate property.
|
|
31
|
+
* Then we should throw error if birthdate is not valid
|
|
32
|
+
*/
|
|
33
|
+
export const isUnderSixteen = dateOfBirth => {
|
|
34
|
+
if (!dateOfBirth || !moment(dateOfBirth).isValid()) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
const age = getAge(dateOfBirth);
|
|
38
|
+
return isNaN(age) ? false : age < 16;
|
|
39
|
+
};
|
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.2.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,56 @@
|
|
|
1
|
+
const { isUnderSixteen, getAge } = require('../bundle.js');
|
|
2
|
+
const moment = require('moment');
|
|
3
|
+
|
|
4
|
+
describe('getAge', () => {
|
|
5
|
+
const refPoint = moment().subtract(16, 'years');
|
|
6
|
+
|
|
7
|
+
test('Returns the correct age', () => {
|
|
8
|
+
const dob = moment(refPoint);
|
|
9
|
+
|
|
10
|
+
expect(getAge(dob)).toBe(16);
|
|
11
|
+
expect(getAge(dob.subtract(1, 'year').utc().format())).toBe(17);
|
|
12
|
+
expect(getAge(dob.add(13, 'months').utc().format())).toBe(15);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test('Returns the age in specified measurement', () => {
|
|
16
|
+
const dob = moment().subtract(15, 'months');
|
|
17
|
+
|
|
18
|
+
expect(getAge(dob)).toBe(1);
|
|
19
|
+
expect(getAge(dob, 'years')).toBe(1);
|
|
20
|
+
expect(getAge(dob, 'months')).toBe(15);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test('Throws Error if dob is not a valid date-string', () => {
|
|
24
|
+
expect(() => getAge(notDefined)).toThrow(/not defined/);
|
|
25
|
+
expect(() => getAge(null)).toThrow(/Invalid date/);
|
|
26
|
+
expect(() => getAge('not-valid')).toThrow(/Invalid date/);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe('isUnderSixteen', () => {
|
|
31
|
+
const refPoint = moment().subtract(16, 'years');
|
|
32
|
+
const obj = {};
|
|
33
|
+
test('Is not U16 if dob is 16 years ago', () => {
|
|
34
|
+
const dob = moment(refPoint);
|
|
35
|
+
|
|
36
|
+
expect(isUnderSixteen(dob.subtract(1, 'second').utc().format())).toBe(false);
|
|
37
|
+
expect(isUnderSixteen(dob.subtract(100, 'year').utc().format())).toBe(false);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test('Is U16 if dob is less than 16 years ago', () => {
|
|
41
|
+
const dob = moment(refPoint);
|
|
42
|
+
|
|
43
|
+
expect(isUnderSixteen(dob.add(1, 'second').utc().format())).toBe(true);
|
|
44
|
+
expect(isUnderSixteen(dob.add(100, 'year').utc().format())).toBe(true);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('Undefined dob', () => {
|
|
48
|
+
expect(isUnderSixteen(obj.notDefined)).toBe(false);
|
|
49
|
+
});
|
|
50
|
+
test('Null dob', () => {
|
|
51
|
+
expect(isUnderSixteen(null)).toBe(false);
|
|
52
|
+
});
|
|
53
|
+
test('Invalid date string', () => {
|
|
54
|
+
expect(isUnderSixteen('not-valid')).toBe(false);
|
|
55
|
+
});
|
|
56
|
+
});
|
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();
|