@becollective/utils 1.1.0 → 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 CHANGED
@@ -40,25 +40,53 @@ var password = {
40
40
 
41
41
  var moment = require('moment');
42
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
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
47
49
  */
48
50
 
49
51
 
50
- var isUnderSixteen = function isUnderSixteen(dateOfBirth) {
51
- var age = moment().diff(dateOfBirth, 'years');
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);
52
55
 
53
56
  if (isNaN(age)) {
54
57
  throw new Error('Invalid date');
55
58
  }
56
59
 
57
- return age < 16;
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;
58
85
  };
59
86
 
60
87
  var util = {
61
88
  password: password,
62
- isUnderSixteen: isUnderSixteen
89
+ isUnderSixteen: isUnderSixteen,
90
+ getAge: getAge
63
91
  };
64
92
  module.exports = util;
package/date-time.js CHANGED
@@ -1,15 +1,39 @@
1
1
  const moment = require('moment');
2
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
+ };
3
18
  /**
4
19
  * Check if a user requires parent(guardian) consent
5
20
  * i.e. Is under the age of 16 years, based on GDPR
6
21
  * https://gdpr-info.eu/art-8-gdpr/
7
- * @param {*} dateOfBirth moment.js compatible date time
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
8
32
  */
9
33
  export const isUnderSixteen = dateOfBirth => {
10
- const age = moment().diff(dateOfBirth, 'years');
11
- if (isNaN(age)) {
12
- throw new Error('Invalid date');
34
+ if (!dateOfBirth || !moment(dateOfBirth).isValid()) {
35
+ return false;
13
36
  }
14
- return age < 16;
37
+ const age = getAge(dateOfBirth);
38
+ return isNaN(age) ? false : age < 16;
15
39
  };
package/index.js CHANGED
@@ -1,9 +1,10 @@
1
1
  import { password } from './password';
2
- import { isUnderSixteen } from './date-time';
2
+ import { isUnderSixteen, getAge } from './date-time';
3
3
 
4
4
  const util = {
5
5
  password,
6
6
  isUnderSixteen,
7
+ getAge,
7
8
  };
8
9
 
9
10
  module.exports = util;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@becollective/utils",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Common utilities",
5
5
  "main": "bundle.js",
6
6
  "scripts": {
@@ -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
+ });
@@ -1,26 +0,0 @@
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
- });