@medipass/utils 11.71.0 → 11.71.1-feature-abn-validators.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/abn.js ADDED
@@ -0,0 +1,72 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var isValidAbn = function isValidAbn(abn) {
6
+ var VALID_ABN_LENGTH = 11; // strip whitespace from value
7
+
8
+ abn = String(abn).replace(/\s+/g, '');
9
+
10
+ if (abn.length === VALID_ABN_LENGTH) {
11
+ var weights = [10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19];
12
+ var abnArray = [];
13
+
14
+ for (var i = 0; i < abn.length; i++) {
15
+ abnArray[i] = abn.charAt(i);
16
+ } // subtract 1 from the left-most digit
17
+
18
+
19
+ abnArray[0] -= 1; // multiply each of the digits by its weighting factor
20
+
21
+ for (var _i = 0; _i < abnArray.length; _i++) {
22
+ abnArray[_i] *= weights[_i];
23
+ } // sum all the digits
24
+
25
+
26
+ var abnSum = 0;
27
+
28
+ for (var _i2 = 0; _i2 < abnArray.length; _i2++) {
29
+ abnSum += abnArray[_i2];
30
+ }
31
+
32
+ return abnSum % 89 === 0;
33
+ } else {
34
+ return false;
35
+ }
36
+ };
37
+ var isValidAcn = function isValidAcn(acn) {
38
+ var VALID_ACN_LENGTH = 9; // strip whitespace from value
39
+
40
+ acn = String(acn).replace(/\s+/g, '');
41
+
42
+ if (acn.length === VALID_ACN_LENGTH) {
43
+ var weights = [8, 7, 6, 5, 4, 3, 2, 1];
44
+ var acnArray = [];
45
+
46
+ for (var i = 0; i < acn.length; i++) {
47
+ acnArray[i] = parseInt(acn.charAt(i));
48
+ }
49
+
50
+ var acnSum = 0; // sum the digits 1-8
51
+
52
+ for (var _i3 = 0; _i3 < weights.length; _i3++) {
53
+ acnSum = acnSum + acnArray[_i3] * weights[_i3];
54
+ } // divide by 10 and take remainder
55
+
56
+
57
+ var remainder = acnSum % 10; // complement the remainder to 10
58
+
59
+ var complement = 10 - remainder; // if the complement equals 10, set it to 0
60
+
61
+ if (complement === 10) {
62
+ complement = 0;
63
+ }
64
+
65
+ return acnArray[8] === complement;
66
+ } else {
67
+ return false;
68
+ }
69
+ };
70
+
71
+ exports.isValidAbn = isValidAbn;
72
+ exports.isValidAcn = isValidAcn;
package/lib/abn.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export declare const isValidAbn: (abn: string) => boolean;
2
+ 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.71.0",
3
+ "version": "11.71.1-feature-abn-validators.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": "c0b96948bea84482e689ab5353934eca2491d4a9"
54
+ "gitHead": "63c6d536710da6ae0452f1f7b2a12038047ea516"
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;