kontonummer 2.0.0 → 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.
Files changed (40) hide show
  1. package/README.md +1 -6
  2. package/dist/cjs/banks.js +282 -0
  3. package/dist/cjs/banks.js.map +1 -0
  4. package/dist/cjs/errors.js +11 -0
  5. package/dist/cjs/errors.js.map +1 -0
  6. package/dist/cjs/format.js +84 -0
  7. package/dist/cjs/format.js.map +1 -0
  8. package/dist/cjs/index.js +150 -0
  9. package/dist/cjs/index.js.map +1 -0
  10. package/dist/cjs/validate.js +46 -0
  11. package/dist/cjs/validate.js.map +1 -0
  12. package/dist/esm/banks.js +279 -0
  13. package/dist/esm/banks.js.map +1 -0
  14. package/dist/esm/errors.js +7 -0
  15. package/dist/esm/errors.js.map +1 -0
  16. package/dist/esm/format.js +80 -0
  17. package/dist/esm/format.js.map +1 -0
  18. package/dist/esm/index.js +120 -0
  19. package/dist/esm/index.js.map +1 -0
  20. package/dist/esm/validate.js +41 -0
  21. package/dist/esm/validate.js.map +1 -0
  22. package/dist/types/banks.d.ts +19 -0
  23. package/{dist-types → dist/types}/format.d.ts +2 -2
  24. package/{dist-types → dist/types}/index.d.ts +1 -1
  25. package/{dist-types → dist/types}/validate.d.ts +2 -2
  26. package/package.json +35 -54
  27. package/CHANGELOG.md +0 -49
  28. package/dist-node/index.js +0 -927
  29. package/dist-node/index.js.map +0 -1
  30. package/dist-src/banks.js +0 -247
  31. package/dist-src/errors.js +0 -49
  32. package/dist-src/format.js +0 -102
  33. package/dist-src/index.js +0 -208
  34. package/dist-src/validate.js +0 -41
  35. package/dist-types/banks.d.ts +0 -19
  36. package/dist-web/index.bundled.js +0 -2
  37. package/dist-web/index.bundled.js.map +0 -1
  38. package/dist-web/index.js +0 -648
  39. package/dist-web/index.js.map +0 -1
  40. /package/{dist-types → dist/types}/errors.d.ts +0 -0
@@ -1,927 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- function _slicedToArray(arr, i) {
6
- return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
7
- }
8
-
9
- function _nonIterableRest() {
10
- throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
11
- }
12
-
13
- function _unsupportedIterableToArray(o, minLen) {
14
- if (!o) return;
15
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
16
- var n = Object.prototype.toString.call(o).slice(8, -1);
17
- if (n === "Object" && o.constructor) n = o.constructor.name;
18
- if (n === "Map" || n === "Set") return Array.from(o);
19
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
20
- }
21
-
22
- function _arrayLikeToArray(arr, len) {
23
- if (len == null || len > arr.length) len = arr.length;
24
-
25
- for (var i = 0, arr2 = new Array(len); i < len; i++) {
26
- arr2[i] = arr[i];
27
- }
28
-
29
- return arr2;
30
- }
31
-
32
- function _iterableToArrayLimit(arr, i) {
33
- if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return;
34
- var _arr = [];
35
- var _n = true;
36
- var _d = false;
37
- var _e = undefined;
38
-
39
- try {
40
- for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
41
- _arr.push(_s.value);
42
-
43
- if (i && _arr.length === i) break;
44
- }
45
- } catch (err) {
46
- _d = true;
47
- _e = err;
48
- } finally {
49
- try {
50
- if (!_n && _i["return"] != null) _i["return"]();
51
- } finally {
52
- if (_d) throw _e;
53
- }
54
- }
55
-
56
- return _arr;
57
- }
58
-
59
- function _arrayWithHoles(arr) {
60
- if (Array.isArray(arr)) return arr;
61
- }
62
-
63
- var banks = [// Type 1 accounts. Always 11 digits long sortingCode included.
64
- // SSSSAAAAAAC
65
- {
66
- bankName: 'Avanza Bank',
67
- type: 1,
68
- comment: 2,
69
- ranges: [[9550, 9569]]
70
- }, {
71
- bankName: 'BlueStep Finans',
72
- type: 1,
73
- comment: 1,
74
- ranges: [[9680, 9689]]
75
- }, {
76
- bankName: 'BNP Paribas SA.',
77
- type: 1,
78
- comment: 2,
79
- ranges: [[9470, 9479]]
80
- }, {
81
- bankName: 'Citibank',
82
- type: 1,
83
- comment: 2,
84
- ranges: [[9040, 9049]]
85
- }, {
86
- bankName: 'Danske Bank',
87
- type: 1,
88
- comment: 1,
89
- ranges: [[1200, 1399], [2400, 2499]]
90
- }, {
91
- bankName: 'DNB Bank',
92
- type: 1,
93
- comment: 2,
94
- ranges: [[9190, 9199], [9260, 9269]]
95
- }, {
96
- bankName: 'Ekobanken',
97
- type: 1,
98
- comment: 2,
99
- ranges: [[9700, 9709]]
100
- }, {
101
- bankName: 'Erik Penser',
102
- type: 1,
103
- comment: 2,
104
- ranges: [[9590, 9599]]
105
- }, {
106
- bankName: 'Forex Bank',
107
- type: 1,
108
- comment: 1,
109
- ranges: [[9400, 9449]]
110
- }, {
111
- bankName: 'ICA Banken',
112
- type: 1,
113
- comment: 1,
114
- ranges: [[9270, 9279]]
115
- }, {
116
- bankName: 'IKANO Bank',
117
- type: 1,
118
- comment: 1,
119
- ranges: [[9170, 9179]]
120
- }, {
121
- bankName: 'JAK Medlemsbank',
122
- type: 1,
123
- comment: 2,
124
- ranges: [[9670, 9679]]
125
- }, {
126
- bankName: 'Klarna Bank',
127
- type: 1,
128
- comment: 2,
129
- ranges: [[9780, 9789]]
130
- }, {
131
- bankName: 'Landshypotek',
132
- type: 1,
133
- comment: 2,
134
- ranges: [[9390, 9399]]
135
- }, {
136
- bankName: 'Lån & Spar Bank Sverige',
137
- type: 1,
138
- comment: 1,
139
- ranges: [[9630, 9639]]
140
- }, {
141
- bankName: 'Länsförsäkringar Bank',
142
- type: 1,
143
- comment: 1,
144
- ranges: [[3400, 3499], [9060, 9069]]
145
- }, {
146
- bankName: 'Länsförsäkringar Bank',
147
- type: 1,
148
- comment: 2,
149
- ranges: [[9020, 9029]]
150
- }, {
151
- bankName: 'Marginalen Bank',
152
- type: 1,
153
- comment: 1,
154
- ranges: [[9230, 9239]]
155
- }, {
156
- bankName: 'Nordax Bank',
157
- type: 1,
158
- comment: 2,
159
- ranges: [[9640, 9649]]
160
- }, {
161
- bankName: 'Nordea',
162
- type: 1,
163
- comment: 1,
164
- ranges: [[1100, 1199], [1400, 2099], [3000, 3299], [3301, 3399], [3410, 3781], [3783, 3999]]
165
- }, {
166
- bankName: 'Nordea',
167
- type: 1,
168
- comment: 2,
169
- ranges: [[4000, 4999]]
170
- }, {
171
- bankName: 'Nordnet Bank',
172
- type: 1,
173
- comment: 2,
174
- ranges: [[9100, 9109]]
175
- }, {
176
- bankName: 'Northmill Bank',
177
- type: 1,
178
- comment: 2,
179
- ranges: [[9750, 9759]]
180
- }, {
181
- bankName: 'Resurs Bank',
182
- type: 1,
183
- comment: 1,
184
- ranges: [[9280, 9289]]
185
- }, {
186
- bankName: 'Riksgälden',
187
- type: 1,
188
- comment: 2,
189
- ranges: [[9880, 9889]]
190
- }, {
191
- bankName: 'Santander Consumer Bank',
192
- type: 1,
193
- comment: 1,
194
- ranges: [[9460, 9469]]
195
- }, {
196
- bankName: 'SBAB',
197
- type: 1,
198
- comment: 1,
199
- ranges: [[9250, 9259]]
200
- }, {
201
- bankName: 'SEB',
202
- type: 1,
203
- comment: 1,
204
- ranges: [[5000, 5999], [9120, 9124], [9130, 9149]]
205
- }, {
206
- bankName: 'Skandiabanken',
207
- type: 1,
208
- comment: 2,
209
- ranges: [[9150, 9169]]
210
- }, {
211
- bankName: 'Svea Bank',
212
- type: 1,
213
- comment: 2,
214
- ranges: [[9660, 9669]]
215
- }, {
216
- bankName: 'Swedbank',
217
- type: 1,
218
- comment: 1,
219
- ranges: [[7000, 7999]]
220
- }, {
221
- bankName: 'Ålandsbanken',
222
- type: 1,
223
- comment: 2,
224
- ranges: [[2300, 2399]]
225
- }, // Type 2 accounts - the messy ones
226
- {
227
- bankName: 'Danske Bank',
228
- type: 2,
229
- comment: 1,
230
- ranges: [[9180, 9189]],
231
- accountMinLength: 10,
232
- accountMaxLength: 10
233
- }, {
234
- bankName: 'Handelsbanken',
235
- type: 2,
236
- comment: 2,
237
- ranges: [[6000, 6999]],
238
- accountMinLength: 8,
239
- accountMaxLength: 9
240
- }, {
241
- bankName: 'Nordea',
242
- type: 2,
243
- comment: 1,
244
- ranges: [[3300, 3300], [3782, 3782]],
245
- accountMinLength: 10,
246
- accountMaxLength: 10
247
- }, {
248
- bankName: 'Nordea Plusgirot',
249
- type: 2,
250
- comment: 3,
251
- ranges: [[9500, 9549], [9960, 9969]],
252
- accountMinLength: 2,
253
- accountMaxLength: 8
254
- }, {
255
- bankName: 'Riksgälden',
256
- type: 2,
257
- comment: 1,
258
- ranges: [[9890, 9899]],
259
- accountMinLength: 10,
260
- accountMaxLength: 10
261
- }, {
262
- bankName: 'Sparbanken Syd',
263
- type: 2,
264
- comment: 1,
265
- ranges: [[9570, 9579]],
266
- accountMinLength: 10,
267
- accountMaxLength: 10
268
- }, {
269
- bankName: 'Swedbank',
270
- type: 2,
271
- comment: 3,
272
- ranges: [[8000, 8999]],
273
- // adding an extra 5-digit case to catch their 5 digit sorting codes
274
- accountMinLength: 10,
275
- // source: https://www.swedbank.se/privat/kort-och-betala/konton-for-in-och-utbetalningar/clearingnummer.html
276
- accountMaxLength: 11 // Allowing 11 here in case clearingnumber is sent as the first four instead of the first five
277
-
278
- }, {
279
- bankName: 'Swedbank',
280
- type: 2,
281
- comment: 1,
282
- ranges: [[9300, 9349]],
283
- accountMinLength: 10,
284
- accountMaxLength: 10
285
- }];
286
- var _getSortingCodeInfo = (function (sortingCode) {
287
- return banks.find(function (bank) {
288
- return bank.ranges.some(function (_ref) {
289
- var _ref2 = _slicedToArray(_ref, 2),
290
- min = _ref2[0],
291
- max = _ref2[1];
292
-
293
- var sortingCodeNumber = Number("".concat(sortingCode).replace(/[^\d]/g, '').substring(0, 4));
294
- return sortingCodeNumber >= min && sortingCodeNumber <= max;
295
- });
296
- });
297
- });
298
-
299
- function _typeof(obj) {
300
- "@babel/helpers - typeof";
301
-
302
- if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
303
- _typeof = function _typeof(obj) {
304
- return typeof obj;
305
- };
306
- } else {
307
- _typeof = function _typeof(obj) {
308
- return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
309
- };
310
- }
311
-
312
- return _typeof(obj);
313
- }
314
-
315
- function _classCallCheck(instance, Constructor) {
316
- if (!(instance instanceof Constructor)) {
317
- throw new TypeError("Cannot call a class as a function");
318
- }
319
- }
320
-
321
- function _inherits(subClass, superClass) {
322
- if (typeof superClass !== "function" && superClass !== null) {
323
- throw new TypeError("Super expression must either be null or a function");
324
- }
325
-
326
- subClass.prototype = Object.create(superClass && superClass.prototype, {
327
- constructor: {
328
- value: subClass,
329
- writable: true,
330
- configurable: true
331
- }
332
- });
333
- if (superClass) _setPrototypeOf(subClass, superClass);
334
- }
335
-
336
- function _createSuper(Derived) {
337
- var hasNativeReflectConstruct = _isNativeReflectConstruct();
338
-
339
- return function _createSuperInternal() {
340
- var Super = _getPrototypeOf(Derived),
341
- result;
342
-
343
- if (hasNativeReflectConstruct) {
344
- var NewTarget = _getPrototypeOf(this).constructor;
345
-
346
- result = Reflect.construct(Super, arguments, NewTarget);
347
- } else {
348
- result = Super.apply(this, arguments);
349
- }
350
-
351
- return _possibleConstructorReturn(this, result);
352
- };
353
- }
354
-
355
- function _possibleConstructorReturn(self, call) {
356
- if (call && (_typeof(call) === "object" || typeof call === "function")) {
357
- return call;
358
- }
359
-
360
- return _assertThisInitialized(self);
361
- }
362
-
363
- function _assertThisInitialized(self) {
364
- if (self === void 0) {
365
- throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
366
- }
367
-
368
- return self;
369
- }
370
-
371
- function _wrapNativeSuper(Class) {
372
- var _cache = typeof Map === "function" ? new Map() : undefined;
373
-
374
- _wrapNativeSuper = function _wrapNativeSuper(Class) {
375
- if (Class === null || !_isNativeFunction(Class)) return Class;
376
-
377
- if (typeof Class !== "function") {
378
- throw new TypeError("Super expression must either be null or a function");
379
- }
380
-
381
- if (typeof _cache !== "undefined") {
382
- if (_cache.has(Class)) return _cache.get(Class);
383
-
384
- _cache.set(Class, Wrapper);
385
- }
386
-
387
- function Wrapper() {
388
- return _construct(Class, arguments, _getPrototypeOf(this).constructor);
389
- }
390
-
391
- Wrapper.prototype = Object.create(Class.prototype, {
392
- constructor: {
393
- value: Wrapper,
394
- enumerable: false,
395
- writable: true,
396
- configurable: true
397
- }
398
- });
399
- return _setPrototypeOf(Wrapper, Class);
400
- };
401
-
402
- return _wrapNativeSuper(Class);
403
- }
404
-
405
- function _construct(Parent, args, Class) {
406
- if (_isNativeReflectConstruct()) {
407
- _construct = Reflect.construct;
408
- } else {
409
- _construct = function _construct(Parent, args, Class) {
410
- var a = [null];
411
- a.push.apply(a, args);
412
- var Constructor = Function.bind.apply(Parent, a);
413
- var instance = new Constructor();
414
- if (Class) _setPrototypeOf(instance, Class.prototype);
415
- return instance;
416
- };
417
- }
418
-
419
- return _construct.apply(null, arguments);
420
- }
421
-
422
- function _isNativeReflectConstruct() {
423
- if (typeof Reflect === "undefined" || !Reflect.construct) return false;
424
- if (Reflect.construct.sham) return false;
425
- if (typeof Proxy === "function") return true;
426
-
427
- try {
428
- Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
429
- return true;
430
- } catch (e) {
431
- return false;
432
- }
433
- }
434
-
435
- function _isNativeFunction(fn) {
436
- return Function.toString.call(fn).indexOf("[native code]") !== -1;
437
- }
438
-
439
- function _setPrototypeOf(o, p) {
440
- _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
441
- o.__proto__ = p;
442
- return o;
443
- };
444
-
445
- return _setPrototypeOf(o, p);
446
- }
447
-
448
- function _getPrototypeOf(o) {
449
- _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
450
- return o.__proto__ || Object.getPrototypeOf(o);
451
- };
452
- return _getPrototypeOf(o);
453
- }
454
-
455
- function _defineProperty(obj, key, value) {
456
- if (key in obj) {
457
- Object.defineProperty(obj, key, {
458
- value: value,
459
- enumerable: true,
460
- configurable: true,
461
- writable: true
462
- });
463
- } else {
464
- obj[key] = value;
465
- }
466
-
467
- return obj;
468
- }
469
-
470
- var KontonummerError = /*#__PURE__*/function (_Error) {
471
- _inherits(KontonummerError, _Error);
472
-
473
- var _super = _createSuper(KontonummerError);
474
-
475
- function KontonummerError() {
476
- var _this;
477
-
478
- _classCallCheck(this, KontonummerError);
479
-
480
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
481
- args[_key] = arguments[_key];
482
- }
483
-
484
- _this = _super.call.apply(_super, [this].concat(args));
485
-
486
- _defineProperty(_assertThisInitialized(_this), "name", 'KontonummerError');
487
-
488
- return _this;
489
- }
490
-
491
- return KontonummerError;
492
- }( /*#__PURE__*/_wrapNativeSuper(Error));
493
-
494
- var mod10 = function mod10(number) {
495
- number = "".concat(number);
496
- var len = number.length;
497
- var bit = 1;
498
- var sum = 0;
499
- var val;
500
- var arr = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9];
501
-
502
- while (len) {
503
- val = parseInt(number.charAt(--len), 10);
504
- bit ^= 1;
505
- sum += bit ? arr[val] : val;
506
- }
507
-
508
- return !!sum && sum % 10 === 0;
509
- };
510
- var mod11 = function mod11(number) {
511
- number = "".concat(number);
512
- var len = number.length;
513
- var sum = 0;
514
- var val;
515
- var weights = [1, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
516
- var arr = weights.splice(weights.length - len, weights.length - (weights.length - len));
517
-
518
- while (len) {
519
- val = parseInt(number.charAt(--len), 10);
520
- sum += arr[len] * val;
521
- }
522
-
523
- return !!sum && sum % 11 === 0;
524
- };
525
- var validateCheckDigit = (function (type, comment, sortingCode, accountNumber) {
526
- // 1:1 => mod11 on 3 last of clearing + whole account number
527
- if (type === 1 && comment === 1) return mod11("".concat(sortingCode.substring(1)).concat(accountNumber.padStart(7, '0'))); // 1:2 => mod 11 on whole clearing + whole account number
528
-
529
- if (type === 1 && comment === 2) return mod11("".concat(sortingCode).concat(accountNumber.padStart(7, '0'))); // 2:2 => mod11 on whole account number (SHB) 9 digits
530
-
531
- if (type === 2 && comment === 2) return mod11("".concat(accountNumber.padStart(9, '0'))); // 2:1 & 2:3 => mod10 on whole account number
532
-
533
- return mod10(accountNumber.padStart(10, '0'));
534
- });
535
-
536
- function _toConsumableArray(arr) {
537
- return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray$1(arr) || _nonIterableSpread();
538
- }
539
-
540
- function _nonIterableSpread() {
541
- throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
542
- }
543
-
544
- function _unsupportedIterableToArray$1(o, minLen) {
545
- if (!o) return;
546
- if (typeof o === "string") return _arrayLikeToArray$1(o, minLen);
547
- var n = Object.prototype.toString.call(o).slice(8, -1);
548
- if (n === "Object" && o.constructor) n = o.constructor.name;
549
- if (n === "Map" || n === "Set") return Array.from(o);
550
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray$1(o, minLen);
551
- }
552
-
553
- function _iterableToArray(iter) {
554
- if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter);
555
- }
556
-
557
- function _arrayWithoutHoles(arr) {
558
- if (Array.isArray(arr)) return _arrayLikeToArray$1(arr);
559
- }
560
-
561
- function _arrayLikeToArray$1(arr, len) {
562
- if (len == null || len > arr.length) len = arr.length;
563
-
564
- for (var i = 0, arr2 = new Array(len); i < len; i++) {
565
- arr2[i] = arr[i];
566
- }
567
-
568
- return arr2;
569
- }
570
-
571
- var formatter = function formatter(sortingCode, accountNumber, mask) {
572
- var pad = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false; // we need to split these because sortingCode goes ltr and accountNumber goes rtl
573
-
574
- var accountNumberMask = mask.substring(mask.lastIndexOf('S') + 1);
575
- var sortingCodeMask = mask.substring(0, mask.lastIndexOf('S') + 1);
576
- var sortingCodeResult = [];
577
- var accountNumberResult = [];
578
- var sortingCodeChars = sortingCode.split('');
579
- var accountNumberChars = accountNumber.split('');
580
- /*
581
- * Here's what will happen we'll go through the mask and replace every 'S' with the next
582
- * character in the sorting code. if there's no mask for that character we output the character
583
- * If there's no more characters we end the loop.
584
- */
585
-
586
- for (var idx = 0; idx < Math.max(sortingCodeMask.length, sortingCodeChars.length); idx++) {
587
- if (sortingCodeMask[idx] === 'S' || sortingCodeMask[idx] === undefined) {
588
- var _sortingCodeChars$shi;
589
-
590
- sortingCodeResult.push((_sortingCodeChars$shi = sortingCodeChars.shift()) !== null && _sortingCodeChars$shi !== void 0 ? _sortingCodeChars$shi : '');
591
- } else {
592
- sortingCodeResult.push(sortingCodeMask[idx]);
593
- }
594
-
595
- if (sortingCodeChars.length === 0) break;
596
- }
597
- /*
598
- * for the account number we do a similar thing to the sorting code, except that we start
599
- * from the right. Why? Because we might want to zero-pad the number based on the mask, and we
600
- * always want the zero padding to the left/the start of the number
601
- */
602
-
603
-
604
- for (var _idx = accountNumberMask.length - 1; _idx >= 0; _idx--) {
605
- if (accountNumberMask[_idx] === 'A') {
606
- var _accountNumberChars$p;
607
-
608
- accountNumberResult.unshift((_accountNumberChars$p = accountNumberChars.pop()) !== null && _accountNumberChars$p !== void 0 ? _accountNumberChars$p : pad ? '0' : '');
609
- } else {
610
- accountNumberResult.unshift(accountNumberMask[_idx]);
611
- } // if we have more account number than we have mask we just dump it at the start
612
-
613
-
614
- if (_idx === accountNumberMask.indexOf('A') && accountNumberChars.length > 0) {
615
- while (accountNumberChars.length) {
616
- var _accountNumberChars$p2;
617
-
618
- accountNumberResult.unshift((_accountNumberChars$p2 = accountNumberChars.pop()) !== null && _accountNumberChars$p2 !== void 0 ? _accountNumberChars$p2 : '');
619
- }
620
- }
621
-
622
- if (accountNumberChars.length === 0 && !pad) {
623
- accountNumberResult.unshift.apply(accountNumberResult, _toConsumableArray(accountNumberMask.slice(0, accountNumberMask.indexOf('A'))));
624
- break;
625
- }
626
- }
627
-
628
- return "".concat(sortingCodeResult.join('')).concat(accountNumberResult.join(''));
629
- };
630
- var formatter$1 = (function (sortingCode, accountNumber, sortingCodeInfo) {
631
- var _sortingCode, _accountNumber;
632
-
633
- var format = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'numeric';
634
- sortingCode = "".concat((_sortingCode = sortingCode) !== null && _sortingCode !== void 0 ? _sortingCode : '');
635
- accountNumber = "".concat((_accountNumber = accountNumber) !== null && _accountNumber !== void 0 ? _accountNumber : '');
636
- var bankName = sortingCodeInfo.bankName,
637
- type = sortingCodeInfo.type;
638
-
639
- if (format === 'pretty') {
640
- if (type === 1 && bankName === 'Swedbank') {
641
- return formatter(sortingCode, accountNumber, 'SSSS-AA-AAAAA', true);
642
- } else if (type === 1) {
643
- return formatter(sortingCode, accountNumber, 'SSSS AA AAA AA', true);
644
- } else if (bankName === 'Swedbank') {
645
- return formatter(sortingCode, accountNumber, 'SSSS-S, AAA AAA AAA-A');
646
- } else if (bankName === 'Handelsbanken') {
647
- return formatter(sortingCode, accountNumber, 'SSSS, AAA AAA AAA');
648
- } else if (bankName === 'Nordea Plusgirot') {
649
- return formatter(sortingCode, accountNumber, 'SSSS, AAA AA AA-A');
650
- } else if (bankName === 'Nordea') {
651
- return formatter(sortingCode, accountNumber, 'SSSS, AAAAAA-AAAA');
652
- } else {
653
- return formatter(sortingCode, accountNumber, 'SSSS-S, AA AAAA AAAA');
654
- }
655
- } else {
656
- var _accountMinLength;
657
-
658
- return formatter(sortingCode, accountNumber, 'SSSSS' + new Array((_accountMinLength = sortingCodeInfo.accountMinLength) !== null && _accountMinLength !== void 0 ? _accountMinLength : 7).fill('A').join(''), true);
659
- }
660
- });
661
-
662
- var _Symbol$for;
663
-
664
- function _typeof$1(obj) {
665
- "@babel/helpers - typeof";
666
-
667
- if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
668
- _typeof$1 = function _typeof(obj) {
669
- return typeof obj;
670
- };
671
- } else {
672
- _typeof$1 = function _typeof(obj) {
673
- return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
674
- };
675
- }
676
-
677
- return _typeof$1(obj);
678
- }
679
-
680
- function _classCallCheck$1(instance, Constructor) {
681
- if (!(instance instanceof Constructor)) {
682
- throw new TypeError("Cannot call a class as a function");
683
- }
684
- }
685
-
686
- function _defineProperties(target, props) {
687
- for (var i = 0; i < props.length; i++) {
688
- var descriptor = props[i];
689
- descriptor.enumerable = descriptor.enumerable || false;
690
- descriptor.configurable = true;
691
- if ("value" in descriptor) descriptor.writable = true;
692
- Object.defineProperty(target, descriptor.key, descriptor);
693
- }
694
- }
695
-
696
- function _createClass(Constructor, protoProps, staticProps) {
697
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
698
- if (staticProps) _defineProperties(Constructor, staticProps);
699
- return Constructor;
700
- }
701
-
702
- function _classPrivateFieldSet(receiver, privateMap, value) {
703
- var descriptor = privateMap.get(receiver);
704
-
705
- if (!descriptor) {
706
- throw new TypeError("attempted to set private field on non-instance");
707
- }
708
-
709
- if (descriptor.set) {
710
- descriptor.set.call(receiver, value);
711
- } else {
712
- if (!descriptor.writable) {
713
- throw new TypeError("attempted to set read only private field");
714
- }
715
-
716
- descriptor.value = value;
717
- }
718
-
719
- return value;
720
- }
721
-
722
- function _classPrivateFieldGet(receiver, privateMap) {
723
- var descriptor = privateMap.get(receiver);
724
-
725
- if (!descriptor) {
726
- throw new TypeError("attempted to get private field on non-instance");
727
- }
728
-
729
- if (descriptor.get) {
730
- return descriptor.get.call(receiver);
731
- }
732
-
733
- return descriptor.value;
734
- }
735
-
736
- var _bankName = new WeakMap();
737
-
738
- var _sortingCode = new WeakMap();
739
-
740
- var _accountNumber = new WeakMap();
741
-
742
- var _type = new WeakMap();
743
-
744
- var _comment = new WeakMap();
745
-
746
- var _valid = new WeakMap();
747
-
748
- _Symbol$for = Symbol["for"]('nodejs.util.inspect.custom');
749
-
750
- var Kontonummer = /*#__PURE__*/function () {
751
- _createClass(Kontonummer, [{
752
- key: "bankName",
753
- // only relevant in `lax` mode
754
- get: function get() {
755
- return _classPrivateFieldGet(this, _bankName);
756
- }
757
- }, {
758
- key: "sortingCode",
759
- get: function get() {
760
- return _classPrivateFieldGet(this, _sortingCode);
761
- }
762
- }, {
763
- key: "accountNumber",
764
- get: function get() {
765
- return _classPrivateFieldGet(this, _accountNumber);
766
- }
767
- }, {
768
- key: "type",
769
- get: function get() {
770
- return _classPrivateFieldGet(this, _type);
771
- }
772
- }, {
773
- key: "comment",
774
- get: function get() {
775
- return _classPrivateFieldGet(this, _comment);
776
- }
777
- }, {
778
- key: "valid",
779
- get: function get() {
780
- return _classPrivateFieldGet(this, _valid);
781
- }
782
- }]);
783
-
784
- function Kontonummer(sortingCodeWithOrWithoutAccountNumber, accountOrOptions, optionsArg) {
785
- _classCallCheck$1(this, Kontonummer);
786
-
787
- _bankName.set(this, {
788
- writable: true,
789
- value: void 0
790
- });
791
-
792
- _sortingCode.set(this, {
793
- writable: true,
794
- value: void 0
795
- });
796
-
797
- _accountNumber.set(this, {
798
- writable: true,
799
- value: void 0
800
- });
801
-
802
- _type.set(this, {
803
- writable: true,
804
- value: void 0
805
- });
806
-
807
- _comment.set(this, {
808
- writable: true,
809
- value: void 0
810
- });
811
-
812
- _valid.set(this, {
813
- writable: true,
814
- value: void 0
815
- });
816
-
817
- var accountNumber;
818
- var options = {
819
- mode: 'strict'
820
- }; // parse params
821
- // sortingCode
822
-
823
- sortingCodeWithOrWithoutAccountNumber = "".concat(sortingCodeWithOrWithoutAccountNumber).replace(/[^\d]/g, ''); // Swedbank 8xxx-x have 5 digits
824
-
825
- var sortingCode = sortingCodeWithOrWithoutAccountNumber.substring(0, sortingCodeWithOrWithoutAccountNumber.startsWith('8') ? 5 : 4); // accountNumber
826
-
827
- if (_typeof$1(accountOrOptions) === 'object') {
828
- options = accountOrOptions;
829
- accountNumber = sortingCodeWithOrWithoutAccountNumber.substring(sortingCodeWithOrWithoutAccountNumber.startsWith('8') ? 5 : 4);
830
- } else if (typeof accountOrOptions === 'string' || typeof accountOrOptions === 'number') {
831
- accountNumber = "".concat(accountOrOptions).replace(/[^\d]/g, '');
832
- } else {
833
- accountNumber = sortingCodeWithOrWithoutAccountNumber.substring(sortingCodeWithOrWithoutAccountNumber.startsWith('8') ? 5 : 4);
834
- } // optionsArg
835
-
836
-
837
- if (_typeof$1(optionsArg) === 'object') {
838
- options = optionsArg;
839
- } // validate arguments
840
-
841
-
842
- if (sortingCode.length < 4 || (sortingCode.length > 4 ? !mod10(sortingCode) : false)) {
843
- throw new KontonummerError('Invalid sorting code');
844
- }
845
-
846
- if (accountNumber.length < 2) {
847
- throw new KontonummerError('Invalid account number');
848
- }
849
-
850
- var bank = Kontonummer.getSortingCodeInfo(sortingCode);
851
- var valid = validateCheckDigit(bank.type, bank.comment, sortingCode, accountNumber);
852
- if (!valid && options.mode === 'strict') throw new KontonummerError('Invalid account number');
853
- if (!valid && bank.type === 1 && options.mode === 'semi') throw new KontonummerError('Invalid account number');
854
-
855
- _classPrivateFieldSet(this, _bankName, bank.bankName);
856
-
857
- _classPrivateFieldSet(this, _type, bank.type);
858
-
859
- _classPrivateFieldSet(this, _comment, bank.comment);
860
-
861
- _classPrivateFieldSet(this, _sortingCode, sortingCode);
862
-
863
- _classPrivateFieldSet(this, _accountNumber, accountNumber);
864
-
865
- _classPrivateFieldSet(this, _valid, valid);
866
- }
867
-
868
- _createClass(Kontonummer, [{
869
- key: "format",
870
- value: function format(_format) {
871
- return formatter$1(this.sortingCode, this.accountNumber, Kontonummer.getSortingCodeInfo(this.sortingCode), _format);
872
- }
873
- }, {
874
- key: "toJSON",
875
- value: function toJSON() {
876
- return {
877
- bankName: this.bankName,
878
- sortingCode: this.sortingCode,
879
- accountNumber: this.accountNumber,
880
- type: this.type,
881
- comment: this.comment,
882
- valid: this.valid
883
- };
884
- }
885
- }, {
886
- key: _Symbol$for,
887
- value: function value() {
888
- return this.toJSON();
889
- }
890
- }], [{
891
- key: "parse",
892
- value: function parse(sortingCodeWithOrWithoutAccountNumber, accountOrOptions, options) {
893
- if (typeof accountOrOptions === 'string' || typeof accountOrOptions === 'number') return new Kontonummer(sortingCodeWithOrWithoutAccountNumber, accountOrOptions, options);else return new Kontonummer(sortingCodeWithOrWithoutAccountNumber, accountOrOptions);
894
- }
895
- }, {
896
- key: "valid",
897
- value: function valid(sortingCodeWithOrWithoutAccountNumber, accountNumber) {
898
- if (accountNumber && (typeof accountNumber !== 'string' || typeof accountNumber !== 'number')) throw new KontonummerError('Kontonummer.valid() does not accept an options argument');
899
-
900
- try {
901
- if (accountNumber) new Kontonummer(sortingCodeWithOrWithoutAccountNumber, accountNumber); // eslint-disable-line no-new
902
- else new Kontonummer(sortingCodeWithOrWithoutAccountNumber); // eslint-disable-line no-new
903
-
904
- return true;
905
- } catch (_unused) {
906
- return false;
907
- }
908
- }
909
- }, {
910
- key: "getSortingCodeInfo",
911
- value: function getSortingCodeInfo(sortingCode) {
912
- var bank = _getSortingCodeInfo(sortingCode);
913
-
914
- if (typeof bank === 'undefined') throw new KontonummerError("No Bank found with sorting code ".concat(sortingCode));
915
- return bank;
916
- }
917
- }]);
918
-
919
- return Kontonummer;
920
- }();
921
- var parse = Kontonummer.parse;
922
- var valid = Kontonummer.valid;
923
-
924
- exports.default = Kontonummer;
925
- exports.parse = parse;
926
- exports.valid = valid;
927
- //# sourceMappingURL=index.js.map