ibantools 3.3.1 → 4.0.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 CHANGED
@@ -1,3 +1,14 @@
1
+ 2021-11-17 Saša Jovanić <sasa@simplify.ba>
2
+ * Version 4.0.0
3
+ * Fixed Senegal (SN) regular expression
4
+ * Updated Burundi (BI) specification
5
+ * Added Spain (ES) extra BBAN validation
6
+ * Added Poland (PL) extra BBAN validation
7
+ * Added test to check for extra BBAN validation function
8
+
9
+ 2021-09-30 Simen Mailund Svendsen <simen.m.s@hotmail.com>
10
+ * Fix invalid norwegian BBANS (failing MOD11 check) being incorrectly returned as valid
11
+
1
12
  2021-07-24 Saša Jovanić <sasa@simplify.ba>
2
13
  * Version 3.3.1
3
14
  * Fixed issue not showing AD and BG as SEPA countries
@@ -227,6 +227,7 @@ export interface CountryMap {
227
227
  interface CountrySpecInternal {
228
228
  chars?: number;
229
229
  bban_regexp?: string;
230
+ bban_validation_func?: (bban: string) => boolean;
230
231
  IBANRegistry?: boolean;
231
232
  SEPA?: boolean;
232
233
  }
@@ -5,10 +5,10 @@
5
5
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
6
  /**
7
7
  * Validation, extraction and creation of IBAN, BBAN, BIC/SWIFT numbers plus some other helpful stuff
8
- * @packageDocumentation
8
+ * @package Documentation
9
9
  * @author Saša Jovanić
10
10
  * @module ibantools
11
- * @version 3.3.1
11
+ * @version 4.0.0
12
12
  * @license MPL-2.0
13
13
  * @preferred
14
14
  */
@@ -36,7 +36,7 @@ function isValidIBAN(iban) {
36
36
  spec.chars &&
37
37
  spec.chars === iban.length &&
38
38
  reg.test(iban.slice(2, 4)) &&
39
- checkFormatBBAN(iban.slice(4), spec.bban_regexp) &&
39
+ isValidBBAN(iban.slice(4), iban.slice(0, 2)) &&
40
40
  isValidIBANChecksum(iban)) {
41
41
  return true;
42
42
  }
@@ -75,7 +75,7 @@ function validateIBAN(iban) {
75
75
  result.valid = false;
76
76
  result.errorCodes.push(ValidationErrorsIBAN.WrongBBANLength);
77
77
  }
78
- if (spec && spec.bban_regexp && !checkFormatBBAN(iban.slice(4), spec.bban_regexp)) {
78
+ if (spec && spec.bban_regexp && !isValidBBAN(iban.slice(4), iban.slice(0, 2))) {
79
79
  result.valid = false;
80
80
  result.errorCodes.push(ValidationErrorsIBAN.WrongBBANFormat);
81
81
  }
@@ -119,6 +119,9 @@ function isValidBBAN(bban, countryCode) {
119
119
  spec.chars !== null &&
120
120
  spec.chars - 4 === bban.length &&
121
121
  checkFormatBBAN(bban, spec.bban_regexp)) {
122
+ if (spec.bban_validation_func) {
123
+ return spec.bban_validation_func(bban.replace(/[\s.]+/g, ''));
124
+ }
122
125
  return true;
123
126
  }
124
127
  }
@@ -421,6 +424,71 @@ function extractBIC(inputBic) {
421
424
  return result;
422
425
  }
423
426
  exports.extractBIC = extractBIC;
427
+ /**
428
+ * Used for Norway BBAN check
429
+ *
430
+ * @ignore
431
+ */
432
+ var checkNorwayBBAN = function (bban) {
433
+ var weights = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2];
434
+ var bbanWithoutSpacesAndPeriods = bban.replace(/[\s.]+/g, '');
435
+ if (bbanWithoutSpacesAndPeriods.length !== 11) {
436
+ return false;
437
+ }
438
+ else {
439
+ var controlDigit = parseInt(bbanWithoutSpacesAndPeriods.charAt(10), 10);
440
+ var bbanWithoutControlDigit = bbanWithoutSpacesAndPeriods.substring(0, 10);
441
+ var sum = 0;
442
+ for (var index = 0; index < 10; index++) {
443
+ sum += parseInt(bbanWithoutControlDigit.charAt(index), 10) * weights[index];
444
+ }
445
+ var remainder = sum % 11;
446
+ return controlDigit === (remainder === 0 ? 0 : 11 - remainder);
447
+ }
448
+ };
449
+ /**
450
+ * Used for Poland BBAN check
451
+ *
452
+ * @ignore
453
+ */
454
+ var checkPolandBBAN = function (bban) {
455
+ var weights = [3, 9, 7, 1, 3, 9, 7];
456
+ var controlDigit = parseInt(bban.charAt(7), 10);
457
+ var toCheck = bban.substring(0, 7);
458
+ var sum = 0;
459
+ for (var index = 0; index < 7; index++) {
460
+ sum += parseInt(toCheck.charAt(index), 10) * weights[index];
461
+ }
462
+ var remainder = sum % 10;
463
+ return controlDigit === (remainder === 0 ? 0 : 10 - remainder);
464
+ };
465
+ /**
466
+ * Spain (ES) BBAN check
467
+ *
468
+ * @ignore
469
+ */
470
+ var checkSpainBBAN = function (bban) {
471
+ var weightsBankBranch = [4, 8, 5, 10, 9, 7, 3, 6];
472
+ var weightsAccount = [1, 2, 4, 8, 5, 10, 9, 7, 3, 6];
473
+ var controlBankBranch = parseInt(bban.charAt(8), 10);
474
+ var controlAccount = parseInt(bban.charAt(9), 10);
475
+ var bankBranch = bban.substring(0, 8);
476
+ var account = bban.substring(10, 20);
477
+ var sum = 0;
478
+ for (var index = 0; index < 8; index++) {
479
+ sum += parseInt(bankBranch.charAt(index), 10) * weightsBankBranch[index];
480
+ }
481
+ var remainder = sum % 11;
482
+ if (controlBankBranch !== (remainder === 0 ? 0 : 11 - remainder)) {
483
+ return false;
484
+ }
485
+ sum = 0;
486
+ for (var index = 0; index < 10; index++) {
487
+ sum += parseInt(account.charAt(index), 10) * weightsAccount[index];
488
+ }
489
+ remainder = sum % 11;
490
+ return controlAccount === (remainder === 0 ? 0 : 11 - remainder);
491
+ };
424
492
  /**
425
493
  * Country specifications
426
494
  */
@@ -489,8 +557,8 @@ exports.countrySpecs = {
489
557
  IBANRegistry: true,
490
558
  },
491
559
  BI: {
492
- chars: 16,
493
- bban_regexp: '^[0-9]{12}$',
560
+ chars: 27,
561
+ bban_regexp: '^[0-9]{23}$',
494
562
  },
495
563
  BJ: {
496
564
  chars: 28,
@@ -586,7 +654,7 @@ exports.countrySpecs = {
586
654
  EG: { chars: 29, bban_regexp: '^[0-9]{25}', IBANRegistry: true },
587
655
  EH: {},
588
656
  ER: {},
589
- ES: { chars: 24, bban_regexp: '^[0-9]{20}$', IBANRegistry: true, SEPA: true },
657
+ ES: { chars: 24, bban_validation_func: checkSpainBBAN, bban_regexp: '^[0-9]{20}$', IBANRegistry: true, SEPA: true },
590
658
  ET: {},
591
659
  FI: { chars: 18, bban_regexp: '^[0-9]{14}$', IBANRegistry: true, SEPA: true },
592
660
  FJ: {},
@@ -861,7 +929,7 @@ exports.countrySpecs = {
861
929
  IBANRegistry: true,
862
930
  SEPA: true,
863
931
  },
864
- NO: { chars: 15, bban_regexp: '^[0-9]{11}$', IBANRegistry: true, SEPA: true },
932
+ NO: { chars: 15, bban_regexp: '^[0-9]{11}$', bban_validation_func: checkNorwayBBAN, IBANRegistry: true, SEPA: true },
865
933
  NP: {},
866
934
  NR: {},
867
935
  NU: {},
@@ -881,7 +949,7 @@ exports.countrySpecs = {
881
949
  bban_regexp: '^[A-Z0-9]{4}[0-9]{16}$',
882
950
  IBANRegistry: true,
883
951
  },
884
- PL: { chars: 28, bban_regexp: '^[0-9]{24}$', IBANRegistry: true, SEPA: true },
952
+ PL: { chars: 28, bban_validation_func: checkPolandBBAN, bban_regexp: '^[0-9]{24}$', IBANRegistry: true, SEPA: true },
885
953
  PM: {
886
954
  chars: 27,
887
955
  bban_regexp: '^[0-9]{10}[A-Z0-9]{11}[0-9]{2}$',
@@ -947,7 +1015,7 @@ exports.countrySpecs = {
947
1015
  },
948
1016
  SN: {
949
1017
  chars: 28,
950
- bban_regexp: '^[A-Z]{1}[0-9]{23}$',
1018
+ bban_regexp: '^[A-Z]{2}[0-9]{22}$',
951
1019
  },
952
1020
  SO: {},
953
1021
  SR: {},
@@ -5,10 +5,10 @@
5
5
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
6
  /**
7
7
  * Validation, extraction and creation of IBAN, BBAN, BIC/SWIFT numbers plus some other helpful stuff
8
- * @packageDocumentation
8
+ * @package Documentation
9
9
  * @author Saša Jovanić
10
10
  * @module ibantools
11
- * @version 3.3.1
11
+ * @version 4.0.0
12
12
  * @license MPL-2.0
13
13
  * @preferred
14
14
  */
@@ -34,7 +34,7 @@ export function isValidIBAN(iban) {
34
34
  spec.chars &&
35
35
  spec.chars === iban.length &&
36
36
  reg.test(iban.slice(2, 4)) &&
37
- checkFormatBBAN(iban.slice(4), spec.bban_regexp) &&
37
+ isValidBBAN(iban.slice(4), iban.slice(0, 2)) &&
38
38
  isValidIBANChecksum(iban)) {
39
39
  return true;
40
40
  }
@@ -72,7 +72,7 @@ export function validateIBAN(iban) {
72
72
  result.valid = false;
73
73
  result.errorCodes.push(ValidationErrorsIBAN.WrongBBANLength);
74
74
  }
75
- if (spec && spec.bban_regexp && !checkFormatBBAN(iban.slice(4), spec.bban_regexp)) {
75
+ if (spec && spec.bban_regexp && !isValidBBAN(iban.slice(4), iban.slice(0, 2))) {
76
76
  result.valid = false;
77
77
  result.errorCodes.push(ValidationErrorsIBAN.WrongBBANFormat);
78
78
  }
@@ -115,6 +115,9 @@ export function isValidBBAN(bban, countryCode) {
115
115
  spec.chars !== null &&
116
116
  spec.chars - 4 === bban.length &&
117
117
  checkFormatBBAN(bban, spec.bban_regexp)) {
118
+ if (spec.bban_validation_func) {
119
+ return spec.bban_validation_func(bban.replace(/[\s.]+/g, ''));
120
+ }
118
121
  return true;
119
122
  }
120
123
  }
@@ -407,6 +410,71 @@ export function extractBIC(inputBic) {
407
410
  }
408
411
  return result;
409
412
  }
413
+ /**
414
+ * Used for Norway BBAN check
415
+ *
416
+ * @ignore
417
+ */
418
+ var checkNorwayBBAN = function (bban) {
419
+ var weights = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2];
420
+ var bbanWithoutSpacesAndPeriods = bban.replace(/[\s.]+/g, '');
421
+ if (bbanWithoutSpacesAndPeriods.length !== 11) {
422
+ return false;
423
+ }
424
+ else {
425
+ var controlDigit = parseInt(bbanWithoutSpacesAndPeriods.charAt(10), 10);
426
+ var bbanWithoutControlDigit = bbanWithoutSpacesAndPeriods.substring(0, 10);
427
+ var sum = 0;
428
+ for (var index = 0; index < 10; index++) {
429
+ sum += parseInt(bbanWithoutControlDigit.charAt(index), 10) * weights[index];
430
+ }
431
+ var remainder = sum % 11;
432
+ return controlDigit === (remainder === 0 ? 0 : 11 - remainder);
433
+ }
434
+ };
435
+ /**
436
+ * Used for Poland BBAN check
437
+ *
438
+ * @ignore
439
+ */
440
+ var checkPolandBBAN = function (bban) {
441
+ var weights = [3, 9, 7, 1, 3, 9, 7];
442
+ var controlDigit = parseInt(bban.charAt(7), 10);
443
+ var toCheck = bban.substring(0, 7);
444
+ var sum = 0;
445
+ for (var index = 0; index < 7; index++) {
446
+ sum += parseInt(toCheck.charAt(index), 10) * weights[index];
447
+ }
448
+ var remainder = sum % 10;
449
+ return controlDigit === (remainder === 0 ? 0 : 10 - remainder);
450
+ };
451
+ /**
452
+ * Spain (ES) BBAN check
453
+ *
454
+ * @ignore
455
+ */
456
+ var checkSpainBBAN = function (bban) {
457
+ var weightsBankBranch = [4, 8, 5, 10, 9, 7, 3, 6];
458
+ var weightsAccount = [1, 2, 4, 8, 5, 10, 9, 7, 3, 6];
459
+ var controlBankBranch = parseInt(bban.charAt(8), 10);
460
+ var controlAccount = parseInt(bban.charAt(9), 10);
461
+ var bankBranch = bban.substring(0, 8);
462
+ var account = bban.substring(10, 20);
463
+ var sum = 0;
464
+ for (var index = 0; index < 8; index++) {
465
+ sum += parseInt(bankBranch.charAt(index), 10) * weightsBankBranch[index];
466
+ }
467
+ var remainder = sum % 11;
468
+ if (controlBankBranch !== (remainder === 0 ? 0 : 11 - remainder)) {
469
+ return false;
470
+ }
471
+ sum = 0;
472
+ for (var index = 0; index < 10; index++) {
473
+ sum += parseInt(account.charAt(index), 10) * weightsAccount[index];
474
+ }
475
+ remainder = sum % 11;
476
+ return controlAccount === (remainder === 0 ? 0 : 11 - remainder);
477
+ };
410
478
  /**
411
479
  * Country specifications
412
480
  */
@@ -475,8 +543,8 @@ export var countrySpecs = {
475
543
  IBANRegistry: true,
476
544
  },
477
545
  BI: {
478
- chars: 16,
479
- bban_regexp: '^[0-9]{12}$',
546
+ chars: 27,
547
+ bban_regexp: '^[0-9]{23}$',
480
548
  },
481
549
  BJ: {
482
550
  chars: 28,
@@ -572,7 +640,7 @@ export var countrySpecs = {
572
640
  EG: { chars: 29, bban_regexp: '^[0-9]{25}', IBANRegistry: true },
573
641
  EH: {},
574
642
  ER: {},
575
- ES: { chars: 24, bban_regexp: '^[0-9]{20}$', IBANRegistry: true, SEPA: true },
643
+ ES: { chars: 24, bban_validation_func: checkSpainBBAN, bban_regexp: '^[0-9]{20}$', IBANRegistry: true, SEPA: true },
576
644
  ET: {},
577
645
  FI: { chars: 18, bban_regexp: '^[0-9]{14}$', IBANRegistry: true, SEPA: true },
578
646
  FJ: {},
@@ -847,7 +915,7 @@ export var countrySpecs = {
847
915
  IBANRegistry: true,
848
916
  SEPA: true,
849
917
  },
850
- NO: { chars: 15, bban_regexp: '^[0-9]{11}$', IBANRegistry: true, SEPA: true },
918
+ NO: { chars: 15, bban_regexp: '^[0-9]{11}$', bban_validation_func: checkNorwayBBAN, IBANRegistry: true, SEPA: true },
851
919
  NP: {},
852
920
  NR: {},
853
921
  NU: {},
@@ -867,7 +935,7 @@ export var countrySpecs = {
867
935
  bban_regexp: '^[A-Z0-9]{4}[0-9]{16}$',
868
936
  IBANRegistry: true,
869
937
  },
870
- PL: { chars: 28, bban_regexp: '^[0-9]{24}$', IBANRegistry: true, SEPA: true },
938
+ PL: { chars: 28, bban_validation_func: checkPolandBBAN, bban_regexp: '^[0-9]{24}$', IBANRegistry: true, SEPA: true },
871
939
  PM: {
872
940
  chars: 27,
873
941
  bban_regexp: '^[0-9]{10}[A-Z0-9]{11}[0-9]{2}$',
@@ -933,7 +1001,7 @@ export var countrySpecs = {
933
1001
  },
934
1002
  SN: {
935
1003
  chars: 28,
936
- bban_regexp: '^[A-Z]{1}[0-9]{23}$',
1004
+ bban_regexp: '^[A-Z]{2}[0-9]{22}$',
937
1005
  },
938
1006
  SO: {},
939
1007
  SR: {},
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ibantools",
3
- "version": "3.3.1",
3
+ "version": "4.0.0",
4
4
  "description": "Validation, extraction and creation of IBAN, BBAN, BIC/SWIFT numbers plus some other helpful stuff like ISO 3136-1 alpha 2 country list",
5
5
  "keywords": [
6
6
  "IBAN",