@becollective/utils 1.0.3 → 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 ADDED
@@ -0,0 +1,64 @@
1
+ 'use strict';
2
+
3
+ var password = {
4
+ hasUppercase: function hasUppercase(input) {
5
+ return !!input.match(/[A-Z]/);
6
+ },
7
+ hasLowerCase: function hasLowerCase(input) {
8
+ return !!input.match(/[a-z]/);
9
+ },
10
+ hasNumeral: function hasNumeral(input) {
11
+ return !!input.match(/[0-9]/);
12
+ },
13
+ validate: function validate(input) {
14
+ if (typeof input !== 'string') {
15
+ throw new Error('not-string');
16
+ } else if (input.length < 8) {
17
+ throw new Error('invalid-length');
18
+ }
19
+
20
+ var rules = 0;
21
+ if (password.hasUppercase(input)) rules++;
22
+ if (password.hasLowerCase(input)) rules++;
23
+ if (password.hasNumeral(input)) rules++;
24
+
25
+ if (rules < 3) {
26
+ throw new Error('invalid-minimum-rules');
27
+ }
28
+
29
+ return true;
30
+ },
31
+ isValid: function isValid(input) {
32
+ try {
33
+ password.validate(input);
34
+ return true;
35
+ } catch (e) {
36
+ return false;
37
+ }
38
+ }
39
+ };
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
+
60
+ var util = {
61
+ password: password,
62
+ isUnderSixteen: isUnderSixteen
63
+ };
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
@@ -1,7 +1,9 @@
1
- const password = require('./password');
1
+ import { password } from './password';
2
+ import { isUnderSixteen } from './date-time';
2
3
 
3
4
  const util = {
4
5
  password,
6
+ isUnderSixteen,
5
7
  };
6
8
 
7
9
  module.exports = util;
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@becollective/utils",
3
- "version": "1.0.3",
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/rollup/bin/rollup -c",
9
- "prepush": "npm run build; npm test;"
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"
10
10
  },
11
11
  "author": "",
12
12
  "license": "ISC",
@@ -17,5 +17,8 @@
17
17
  "rollup": "^0.67.1",
18
18
  "rollup-plugin-babel": "^4.0.3",
19
19
  "rollup-plugin-node-resolve": "^3.4.0"
20
+ },
21
+ "dependencies": {
22
+ "moment": "^2.24.0"
20
23
  }
21
24
  }
package/password.js CHANGED
@@ -1,4 +1,4 @@
1
- const password = {
1
+ export const password = {
2
2
  hasUppercase: (input) => {
3
3
  return !!input.match(/[A-Z]/);
4
4
  },
@@ -8,9 +8,6 @@ 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 @@ 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
  }
@@ -39,5 +35,3 @@ const password = {
39
35
  }
40
36
  }
41
37
  }
42
-
43
- module.exports = password;
@@ -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
+ });
@@ -1,8 +1,8 @@
1
- const util = require('../index.js');
1
+ const util = require('../bundle.js');
2
2
 
3
3
  describe('password.valiate', () => {
4
4
  test('validate a valid password', async () => {
5
- const password = 'eihe1soKah@c';
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();