ibantools-germany
Version:
IBAN Validator and Generator for German Bank Accounts
65 lines (62 loc) • 2.88 kB
JavaScript
;
/**
* ibantools-germany
* Copyright (C) 2022-2024 Markus Baumer <markus@baumer.dev>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.method00Core = void 0;
const helper_1 = require("../helper");
const getGivenCheckDigit = (digits, checkDigitPosition) => checkDigitPosition === null
? digits.pop() // Check digit is last digit
: digits.splice(checkDigitPosition - 1, 1)[0]; // Check digit is at position n-th position
const getWeightedDigits = (digits, weights, rtl) => rtl ? (0, helper_1.weightDigitsRTL)(digits, weights) : (0, helper_1.weightDigits)(digits, weights);
const getSum = (weightedDigits, crossSums) => {
const crossSumDigits = crossSums
? (0, helper_1.calculateCrossSums)(weightedDigits)
: weightedDigits;
return (0, helper_1.calculateSum)(crossSumDigits);
};
const sumHandler = (crossSum, handleSum) => {
if (handleSum === "CROSS_SUM") {
let calcCrossSum = crossSum;
while (calcCrossSum >= 10) {
calcCrossSum = (0, helper_1.calculateCrossSum)(calcCrossSum);
}
return calcCrossSum;
}
return (0, helper_1.getUnitFromNumber)(crossSum);
};
const coreOptionsWithDefaults = (options) => {
return Object.assign({
checkDigitPosition: null,
crossSums: true,
handleSum: "UNIT",
rtl: true,
}, options);
};
const method00Core = (number, weights, options) => {
const fullOptions = coreOptionsWithDefaults(options !== null && options !== void 0 ? options : {});
const digits = (0, helper_1.getDigits)(number);
const givenCheckDigit = getGivenCheckDigit(digits, fullOptions.checkDigitPosition);
const weightedDigits = getWeightedDigits(digits, weights, fullOptions.rtl);
const sum = getSum(weightedDigits, fullOptions.crossSums);
const handledCrossSum = sumHandler(sum, fullOptions.handleSum);
const calculatedCheckDigit = (0, helper_1.getUnitFromNumber)(10 - handledCrossSum);
if (givenCheckDigit === calculatedCheckDigit) {
return "VALID";
}
return "INVALID";
};
exports.method00Core = method00Core;
exports.default = (number) => (0, exports.method00Core)(number, [2, 1, 2, 1, 2, 1, 2, 1, 2]);