Finished IBAN generator base functionalities, Advanced features still missing

Update iban-generator.yml, iban-generator.yml, and 3 more files...
This commit is contained in:
2025-03-18 21:01:02 +01:00
parent 18bb55cea9
commit 05ea0ca732
5 changed files with 95 additions and 5 deletions

View File

@@ -6,6 +6,7 @@ country.label: "Country"
option.count: "IBAN Count" option.count: "IBAN Count"
option.human.readable: "Format for readability" option.human.readable: "Format for readability"
option.prefer.numbers: "Prefer numbers over letters"
option.for.each: "Generate <i>X</i> for each country" option.for.each: "Generate <i>X</i> for each country"
option.sepa.enable: "Enable SEPA countries" option.sepa.enable: "Enable SEPA countries"
option.non-sepa.enable: "Enable non-SEPA countries" option.non-sepa.enable: "Enable non-SEPA countries"

View File

@@ -6,6 +6,7 @@ country.label: "Pays"
option.count: "Nombre d'IBAN" option.count: "Nombre d'IBAN"
option.human.readable: "Formatter pour lecture" option.human.readable: "Formatter pour lecture"
option.prefer.numbers: "Favoriser les nombres aux lettres"
option.for.each: "Générer <i>X</i> pour chaque pays" option.for.each: "Générer <i>X</i> pour chaque pays"
option.sepa.enable: "Activer les pays SEPA" option.sepa.enable: "Activer les pays SEPA"
option.non-sepa.enable: "Activer les pays non-SEPA" option.non-sepa.enable: "Activer les pays non-SEPA"

View File

@@ -3,10 +3,8 @@
// License: Public Domain (This code) // License: Public Domain (This code)
import { import {
parseStandardIban,
IbanSpecification, IbanSpecification,
countriesSpecs, countriesSpecs,
getIbanChecksumFromParts,
StandardIban StandardIban
} from "../../libs/iban.mjs"; } from "../../libs/iban.mjs";
@@ -34,6 +32,8 @@ import {initCore} from "../../js/nibblepoker-core.mjs";
const eOptionCount = document.querySelector("input#iban-generator-option-count"); const eOptionCount = document.querySelector("input#iban-generator-option-count");
/** @type {HTMLInputElement} */ /** @type {HTMLInputElement} */
const eOptionPrettyPrint = document.querySelector("input#iban-generator-option-pretty"); const eOptionPrettyPrint = document.querySelector("input#iban-generator-option-pretty");
/** @type {HTMLInputElement} */
const eOptionPreferNumbers = document.querySelector("input#iban-generator-option-prefer-numbers");
/** @type {HTMLElement} */ /** @type {HTMLElement} */
const eGenerateButton = document.querySelector("#iban-generator-generate"); const eGenerateButton = document.querySelector("#iban-generator-generate");
@@ -62,6 +62,44 @@ import {initCore} from "../../js/nibblepoker-core.mjs";
} }
window.onload = function () { window.onload = function () {
// FIXME: Handle the exclusions properly
// Generation
eGenerateButton.addEventListener("click", function() {
ePreviewTextArea.value = "";
lastIBANs = [];
let desiredCount = getDesiredCount();
let preferNumbers = eOptionPreferNumbers.checked;
let prettyIban = eOptionPrettyPrint.checked;
/** @type {IbanSpecification[]} */
let targetSpecs;
if(eOptionForEach.checked) {
targetSpecs = Object.values(countriesSpecs);
} else {
targetSpecs = [countriesSpecs[eOptionCountry.value]];
}
targetSpecs.forEach(spec => {
for(let i = 0; i < desiredCount; i++) {
if(prettyIban) {
lastIBANs.push(
spec.getFormattedIban(
new StandardIban(spec.countryCode, spec.generateRandomBban(preferNumbers), spec).toString()
)
);
} else {
lastIBANs.push(
new StandardIban(spec.countryCode, spec.generateRandomBban(preferNumbers), spec).toString()
);
}
}
});
ePreviewTextArea.value = lastIBANs.join("\n");
});
// Count option // Count option
eOptionCount.addEventListener("change", function() { eOptionCount.addEventListener("change", function() {
@@ -96,7 +134,4 @@ import {initCore} from "../../js/nibblepoker-core.mjs";
downloadStringAsFile("- \"" + lastIBANs.join("\"\n- \"") + "\"", "uuids.yaml", "text/yaml"); downloadStringAsFile("- \"" + lastIBANs.join("\"\n- \"") + "\"", "uuids.yaml", "text/yaml");
}); });
}; };
console.log(new StandardIban("LU", "0108783391941421", countriesSpecs.LU).toString());
console.log(new StandardIban("LU", "123456ABCDEFGHIL", countriesSpecs.LU).toString());
} }

View File

@@ -195,6 +195,10 @@ export class StandardIban extends SimpleIban {
} }
} }
export const charsN = "0123456789";
export const charsA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
export const charsC = charsN + charsA;
export class IbanSpecification { export class IbanSpecification {
/** /**
* ISO-3166 Country Code * ISO-3166 Country Code
@@ -264,6 +268,50 @@ export class IbanSpecification {
getFormattedIban(iban) { getFormattedIban(iban) {
return iban.match(/.{1,4}/g).join(' '); return iban.match(/.{1,4}/g).join(' ');
} }
generateRandomBban(preferNumbers = false) {
let returnedBban = "";
let patternParts = ("_" + this.bbanFormat + "0").split("!");
for(let i = 0; i < patternParts.length; i++) {
let elementCount = parseInt(patternParts[i].substring(1));
if(elementCount === 0) {
continue;
}
let elementType = patternParts[i + 1].substring(0, 1);
let elementChoices;
switch(elementType) {
case 'n':
case 'N':
elementChoices = charsN;
break;
case 'c':
case 'C':
if(preferNumbers) {
elementChoices = charsN;
} else {
elementChoices = charsC;
}
break;
case 'a':
case 'A':
elementChoices = charsA;
break;
}
if(elementChoices === undefined || elementChoices === null) {
throw new IncorrectIbanFormatError(
`The format '${this.bbanFormat}' contains an unhandled element type '${elementType}' !`)
}
for (let i = 0; i < elementCount; i++) {
returnedBban += elementChoices.charAt(Math.floor(Math.random() * elementChoices.length));
}
}
return returnedBban;
}
} }
export const countriesSpecs = { export const countriesSpecs = {

View File

@@ -120,6 +120,11 @@
<label for="iban-generator-option-pretty" class="mr-xxs">{{ l10n("option.human.readable", "iban-generator", user_lang) }}:</label> <label for="iban-generator-option-pretty" class="mr-xxs">{{ l10n("option.human.readable", "iban-generator", user_lang) }}:</label>
<input id="iban-generator-option-pretty" class="r-m border" type="checkbox" checked> <input id="iban-generator-option-pretty" class="r-m border" type="checkbox" checked>
<br>
<label for="iban-generator-option-prefer-numbers" class="mr-xxs">{{ l10n("option.prefer.numbers", "iban-generator", user_lang) }}:</label>
<input id="iban-generator-option-prefer-numbers" class="r-m border" type="checkbox">
<hr class="subtle"> <hr class="subtle">