@sb1/ffe-account-selector-react
Version:
Selector for bank accounts with autocomplete.
48 lines (47 loc) • 1.88 kB
JavaScript
import { formatNumber } from '@sb1/ffe-formatters';
export var currencyAffixNOK = {
nb: 'kr',
nn: 'kr',
en: 'NOK',
};
var getWeightedSumOfDigits = function (accountNumber) {
var weights = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2, 1];
var accountNumbersWeighted = weights.map(function (value, index) { return parseInt(accountNumber.charAt(index), 10) * value; });
return accountNumbersWeighted.reduce(function (previousValue, currentValue) { return previousValue + currentValue; });
};
export function isValidNorwegianAccountNumber(accountNumber) {
if (accountNumber.length !== 11) {
return false;
}
var sum = getWeightedSumOfDigits(accountNumber);
return sum % 11 === 0;
}
export function accountFormatter(accountNumber) {
if (accountNumber && isValidNorwegianAccountNumber(accountNumber)) {
return accountNumber.replace(/(\d{4})(\d{2})(\d{5})/g, '$1 $2 $3');
}
return accountNumber;
}
export function balanceWithCurrency(balance, locale, currencyCode) {
if (balance === void 0) { balance = ''; }
var amount = formatNumber(balance, { decimals: 2, locale: locale });
var currencyAffix = currencyCode && currencyCode !== 'NOK'
? currencyCode
: currencyAffixNOK[locale];
return locale === 'en'
? "".concat(currencyAffix, " ").concat(amount)
: "".concat(amount, " ").concat(currencyAffix);
}
export var formatIncompleteAccountNumber = function (accountNumber) {
var matchDigits = /^\d+$/;
if (!accountNumber) {
return accountNumber;
}
var accountNumberWithoutSpaces = accountNumber.replace(/\s/g, ''); // remove spaces
if (matchDigits.test(accountNumberWithoutSpaces)) {
return accountNumberWithoutSpaces
.replace(/(\d{4})(\d{1})/, '$1 $2')
.replace(/ (\d{2})(\d{1})/, ' $1 $2');
}
return accountNumber || '';
};