UNPKG

kontonummer

Version:

A validator for swedish banking numbers

120 lines 6.93 kB
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var _Kontonummer_bankName, _Kontonummer_sortingCode, _Kontonummer_accountNumber, _Kontonummer_type, _Kontonummer_comment, _Kontonummer_valid; import getSortingCodeInfo from './banks.js'; import { KontonummerError } from './errors.js'; import validateCheckDigit, { mod10 } from './validate.js'; import formatter from './format.js'; export default class Kontonummer { get bankName() { return __classPrivateFieldGet(this, _Kontonummer_bankName, "f"); } get sortingCode() { return __classPrivateFieldGet(this, _Kontonummer_sortingCode, "f"); } get accountNumber() { return __classPrivateFieldGet(this, _Kontonummer_accountNumber, "f"); } get type() { return __classPrivateFieldGet(this, _Kontonummer_type, "f"); } get comment() { return __classPrivateFieldGet(this, _Kontonummer_comment, "f"); } get valid() { return __classPrivateFieldGet(this, _Kontonummer_valid, "f"); } constructor(sortingCodeWithOrWithoutAccountNumber, accountOrOptions, optionsArg) { _Kontonummer_bankName.set(this, void 0); _Kontonummer_sortingCode.set(this, void 0); _Kontonummer_accountNumber.set(this, void 0); _Kontonummer_type.set(this, void 0); _Kontonummer_comment.set(this, void 0); _Kontonummer_valid.set(this, void 0); // only relevant in `lax` mode let accountNumber; let options = { mode: 'strict', }; // parse params // sortingCode sortingCodeWithOrWithoutAccountNumber = `${sortingCodeWithOrWithoutAccountNumber}`.replace(/[^\d]/g, ''); // Swedbank 8xxx-x have 5 digits const sortingCode = sortingCodeWithOrWithoutAccountNumber.substring(0, sortingCodeWithOrWithoutAccountNumber.startsWith('8') ? 5 : 4); // accountNumber if (typeof accountOrOptions === 'object') { options = accountOrOptions; accountNumber = sortingCodeWithOrWithoutAccountNumber.substring(sortingCodeWithOrWithoutAccountNumber.startsWith('8') ? 5 : 4); } else if (typeof accountOrOptions === 'string' || typeof accountOrOptions === 'number') { accountNumber = `${accountOrOptions}`.replace(/[^\d]/g, ''); } else { accountNumber = sortingCodeWithOrWithoutAccountNumber.substring(sortingCodeWithOrWithoutAccountNumber.startsWith('8') ? 5 : 4); } // optionsArg if (typeof optionsArg === 'object') { options = optionsArg; } // validate arguments if (sortingCode.length < 4 || (sortingCode.length > 4 ? !mod10(sortingCode) : false)) { throw new KontonummerError('Invalid sorting code'); } if (accountNumber.length < 2) { throw new KontonummerError('Invalid account number'); } const bank = Kontonummer.getSortingCodeInfo(sortingCode); const valid = validateCheckDigit(bank.type, bank.comment, sortingCode, accountNumber); if (!valid && options.mode === 'strict') throw new KontonummerError('Invalid account number'); if (!valid && bank.type === 1 && options.mode === 'semi') throw new KontonummerError('Invalid account number'); __classPrivateFieldSet(this, _Kontonummer_bankName, bank.bankName, "f"); __classPrivateFieldSet(this, _Kontonummer_type, bank.type, "f"); __classPrivateFieldSet(this, _Kontonummer_comment, bank.comment, "f"); __classPrivateFieldSet(this, _Kontonummer_sortingCode, sortingCode, "f"); __classPrivateFieldSet(this, _Kontonummer_accountNumber, accountNumber, "f"); __classPrivateFieldSet(this, _Kontonummer_valid, valid, "f"); } format(format) { return formatter(this.sortingCode, this.accountNumber, Kontonummer.getSortingCodeInfo(this.sortingCode), format); } static parse(sortingCodeWithOrWithoutAccountNumber, accountOrOptions, options) { if (typeof accountOrOptions === 'string' || typeof accountOrOptions === 'number') return new Kontonummer(sortingCodeWithOrWithoutAccountNumber, accountOrOptions, options); else return new Kontonummer(sortingCodeWithOrWithoutAccountNumber, accountOrOptions); } static valid(sortingCodeWithOrWithoutAccountNumber, accountNumber) { if (accountNumber && (typeof accountNumber !== 'string' || typeof accountNumber !== 'number')) throw new KontonummerError('Kontonummer.valid() does not accept an options argument'); try { if (accountNumber) new Kontonummer(sortingCodeWithOrWithoutAccountNumber, accountNumber); // eslint-disable-line no-new else new Kontonummer(sortingCodeWithOrWithoutAccountNumber); // eslint-disable-line no-new return true; } catch { return false; } } static getSortingCodeInfo(sortingCode) { const bank = getSortingCodeInfo(sortingCode); if (typeof bank === 'undefined') throw new KontonummerError(`No Bank found with sorting code ${sortingCode}`); return bank; } toJSON() { return { bankName: this.bankName, sortingCode: this.sortingCode, accountNumber: this.accountNumber, type: this.type, comment: this.comment, valid: this.valid, }; } [(_Kontonummer_bankName = new WeakMap(), _Kontonummer_sortingCode = new WeakMap(), _Kontonummer_accountNumber = new WeakMap(), _Kontonummer_type = new WeakMap(), _Kontonummer_comment = new WeakMap(), _Kontonummer_valid = new WeakMap(), Symbol.for('nodejs.util.inspect.custom'))]() { return this.toJSON(); } } export const parse = Kontonummer.parse.bind(Kontonummer); export const valid = Kontonummer.valid.bind(Kontonummer); //# sourceMappingURL=index.js.map