@medipass/utils 11.72.0 → 11.73.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/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [11.73.0](https://github.com/medipass/web-medicules/compare/@medipass/utils@11.72.0...@medipass/utils@11.73.0) (2022-11-04)
7
+
8
+
9
+ ### Features
10
+
11
+ * **web-sdk, utils:** abr resource and abn validators ([#704](https://github.com/medipass/web-medicules/issues/704)) ([531b969](https://github.com/medipass/web-medicules/commit/531b969))
12
+
13
+
14
+
15
+
16
+
6
17
  # [11.72.0](https://github.com/medipass/web-medicules/compare/@medipass/utils@11.71.0...@medipass/utils@11.72.0) (2022-10-30)
7
18
 
8
19
 
package/abn.js ADDED
@@ -0,0 +1,89 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var ABN_LENGTH = 11;
6
+ var ACN_LENGTH = 9;
7
+ var normaliseAbn = function normaliseAbn(abn) {
8
+ var trimmedABN = abn == null ? void 0 : abn.replace(/\s/g, ''); // The input is an ABN, in which case the format should be 99 999 999 999
9
+
10
+ if (trimmedABN.length === ABN_LENGTH) {
11
+ return trimmedABN.substring(0, 2) + ' ' + trimmedABN.substring(2, 5) + ' ' + trimmedABN.substring(5, 8) + ' ' + trimmedABN.substring(8);
12
+ } //The input is an ACN, in which case the format should be 999 999 999
13
+
14
+
15
+ if (trimmedABN.length === ACN_LENGTH) {
16
+ return trimmedABN.substring(0, 3) + ' ' + trimmedABN.substring(3, 6) + ' ' + trimmedABN.substring(6);
17
+ }
18
+
19
+ return abn;
20
+ };
21
+ var isValidAbn = function isValidAbn(abn) {
22
+ var VALID_ABN_LENGTH = 11; // strip whitespace from value
23
+
24
+ abn = String(abn).replace(/\s+/g, '');
25
+
26
+ if (abn.length === VALID_ABN_LENGTH) {
27
+ var weights = [10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19];
28
+ var abnArray = [];
29
+
30
+ for (var i = 0; i < abn.length; i++) {
31
+ abnArray[i] = abn.charAt(i);
32
+ } // subtract 1 from the left-most digit
33
+
34
+
35
+ abnArray[0] -= 1; // multiply each of the digits by its weighting factor
36
+
37
+ for (var _i = 0; _i < abnArray.length; _i++) {
38
+ abnArray[_i] *= weights[_i];
39
+ } // sum all the digits
40
+
41
+
42
+ var abnSum = 0;
43
+
44
+ for (var _i2 = 0; _i2 < abnArray.length; _i2++) {
45
+ abnSum += abnArray[_i2];
46
+ }
47
+
48
+ return abnSum % 89 === 0;
49
+ } else {
50
+ return false;
51
+ }
52
+ };
53
+ var isValidAcn = function isValidAcn(acn) {
54
+ var VALID_ACN_LENGTH = 9; // strip whitespace from value
55
+
56
+ acn = String(acn).replace(/\s+/g, '');
57
+
58
+ if (acn.length === VALID_ACN_LENGTH) {
59
+ var weights = [8, 7, 6, 5, 4, 3, 2, 1];
60
+ var acnArray = [];
61
+
62
+ for (var i = 0; i < acn.length; i++) {
63
+ acnArray[i] = parseInt(acn.charAt(i));
64
+ }
65
+
66
+ var acnSum = 0; // sum the digits 1-8
67
+
68
+ for (var _i3 = 0; _i3 < weights.length; _i3++) {
69
+ acnSum = acnSum + acnArray[_i3] * weights[_i3];
70
+ } // divide by 10 and take remainder
71
+
72
+
73
+ var remainder = acnSum % 10; // complement the remainder to 10
74
+
75
+ var complement = 10 - remainder; // if the complement equals 10, set it to 0
76
+
77
+ if (complement === 10) {
78
+ complement = 0;
79
+ }
80
+
81
+ return acnArray[8] === complement;
82
+ } else {
83
+ return false;
84
+ }
85
+ };
86
+
87
+ exports.isValidAbn = isValidAbn;
88
+ exports.isValidAcn = isValidAcn;
89
+ exports.normaliseAbn = normaliseAbn;
@@ -0,0 +1 @@
1
+ export {};
package/lib/abn.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export declare const normaliseAbn: (abn: string) => string;
2
+ export declare const isValidAbn: (abn: string) => boolean;
3
+ export declare const isValidAcn: (acn: string) => boolean;
@@ -41,5 +41,8 @@ export declare const hasSpaceAfterHyphen: (value: any) => string;
41
41
  export declare const hasSpaceBeforeHyphen: (value: any) => string;
42
42
  export declare const hasSpaceAfterApostrophe: (value: any) => string;
43
43
  export declare const hasSpaceBeforeApostrophe: (value: any) => string;
44
+ export declare const isValidAbn: (value: string) => string;
45
+ export declare const isValidAcn: (value: string) => string;
46
+ export declare const isValidAbnOrAcn: (value: string) => string;
44
47
  declare const combineValidators: (...fns: any[]) => (value: any) => any;
45
48
  export default combineValidators;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@medipass/utils",
3
- "version": "11.72.0",
3
+ "version": "11.73.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "module": "index.js",
@@ -51,5 +51,5 @@
51
51
  "rimraf": "^2.6.2",
52
52
  "typescript": "4.8.4"
53
53
  },
54
- "gitHead": "6bd756261aacc641c65a4f7f82033fd02f21e432"
54
+ "gitHead": "beae09bac477dfe4b64e1818cdebce9823044fb0"
55
55
  }
package/validate-form.js CHANGED
@@ -18,6 +18,7 @@ var isBefore = _interopDefault(require('date-fns/isBefore'));
18
18
  var isFuture = _interopDefault(require('date-fns/isFuture'));
19
19
  var isValid = _interopDefault(require('date-fns/isValid'));
20
20
  var parse = _interopDefault(require('date-fns/parse'));
21
+ var abn = require('./abn.js');
21
22
 
22
23
  var isRequired = function isRequired(value) {
23
24
  return !value || value.trim && !value.trim() || _isArray(value) && value.length === 0 ? 'This field is required' : undefined;
@@ -331,6 +332,29 @@ var hasSpaceAfterApostrophe = function hasSpaceAfterApostrophe(value) {
331
332
  var hasSpaceBeforeApostrophe = function hasSpaceBeforeApostrophe(value) {
332
333
  return value && /(\s['])/.test(value) ? "Name cannot have a space before an apostrophe (')" : undefined;
333
334
  };
335
+ var isValidAbn = function isValidAbn(value) {
336
+ var isValid = abn.isValidAbn(value);
337
+
338
+ if (isValid) return undefined;
339
+ return 'Invalid ABN';
340
+ };
341
+ var isValidAcn = function isValidAcn(value) {
342
+ var isValid = abn.isValidAcn(value);
343
+
344
+ if (isValid) return undefined;
345
+ return 'Invalid ACN';
346
+ };
347
+ var isValidAbnOrAcn = function isValidAbnOrAcn(value) {
348
+ if (abn.isValidAbn(value)) {
349
+ return undefined;
350
+ }
351
+
352
+ if (abn.isValidAcn(value)) {
353
+ return undefined;
354
+ }
355
+
356
+ return 'Invalid ABN or ACN';
357
+ };
334
358
 
335
359
  var combineValidators = function combineValidators() {
336
360
  for (var _len = arguments.length, fns = new Array(_len), _key = 0; _key < _len; _key++) {
@@ -376,6 +400,9 @@ exports.isMoreThanThreeYears = isMoreThanThreeYears;
376
400
  exports.isNumber = isNumber;
377
401
  exports.isRequired = isRequired;
378
402
  exports.isSameValueAsField = isSameValueAsField;
403
+ exports.isValidAbn = isValidAbn;
404
+ exports.isValidAbnOrAcn = isValidAbnOrAcn;
405
+ exports.isValidAcn = isValidAcn;
379
406
  exports.isValidBankAccountName = isValidBankAccountName;
380
407
  exports.isValidDOB = isValidDOB;
381
408
  exports.isValidDate = isValidDate;
@@ -1,2 +0,0 @@
1
- declare const _default: (abn: any) => any;
2
- export default _default;
package/normalise-abn.js DELETED
@@ -1,22 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var ABN_LENGTH = 11;
6
- var ACN_LENGTH = 9;
7
- var normaliseAbn = (function (abn) {
8
- var trimmedABN = abn && abn.replace(/\s/g, ''); // The input is an ABN, in which case the format should be 99 999 999 999
9
-
10
- if (trimmedABN.length === ABN_LENGTH) {
11
- return trimmedABN.substring(0, 2) + ' ' + trimmedABN.substring(2, 5) + ' ' + trimmedABN.substring(5, 8) + ' ' + trimmedABN.substring(8);
12
- } //The input is an ACN, in which case the format should be 999 999 999
13
-
14
-
15
- if (trimmedABN.length === ACN_LENGTH) {
16
- return trimmedABN.substring(0, 3) + ' ' + trimmedABN.substring(3, 6) + ' ' + trimmedABN.substring(6);
17
- }
18
-
19
- return abn;
20
- });
21
-
22
- exports.default = normaliseAbn;