diff --git a/data/strings/en/iban-generator.yml b/data/strings/en/iban-generator.yml index c8233db..24b779a 100644 --- a/data/strings/en/iban-generator.yml +++ b/data/strings/en/iban-generator.yml @@ -6,6 +6,7 @@ country.label: "Country" option.count: "IBAN Count" option.human.readable: "Format for readability" +option.prefer.numbers: "Prefer numbers over letters" option.for.each: "Generate X for each country" option.sepa.enable: "Enable SEPA countries" option.non-sepa.enable: "Enable non-SEPA countries" diff --git a/data/strings/fr/iban-generator.yml b/data/strings/fr/iban-generator.yml index 15943a4..849e842 100644 --- a/data/strings/fr/iban-generator.yml +++ b/data/strings/fr/iban-generator.yml @@ -6,6 +6,7 @@ country.label: "Pays" option.count: "Nombre d'IBAN" option.human.readable: "Formatter pour lecture" +option.prefer.numbers: "Favoriser les nombres aux lettres" option.for.each: "Générer X pour chaque pays" option.sepa.enable: "Activer les pays SEPA" option.non-sepa.enable: "Activer les pays non-SEPA" diff --git a/static/resources/NibblePoker/applets/iban-generator/iban-generator.mjs b/static/resources/NibblePoker/applets/iban-generator/iban-generator.mjs index 4629f4c..3d5f7d8 100644 --- a/static/resources/NibblePoker/applets/iban-generator/iban-generator.mjs +++ b/static/resources/NibblePoker/applets/iban-generator/iban-generator.mjs @@ -3,10 +3,8 @@ // License: Public Domain (This code) import { - parseStandardIban, IbanSpecification, countriesSpecs, - getIbanChecksumFromParts, StandardIban } from "../../libs/iban.mjs"; @@ -34,6 +32,8 @@ import {initCore} from "../../js/nibblepoker-core.mjs"; const eOptionCount = document.querySelector("input#iban-generator-option-count"); /** @type {HTMLInputElement} */ const eOptionPrettyPrint = document.querySelector("input#iban-generator-option-pretty"); + /** @type {HTMLInputElement} */ + const eOptionPreferNumbers = document.querySelector("input#iban-generator-option-prefer-numbers"); /** @type {HTMLElement} */ const eGenerateButton = document.querySelector("#iban-generator-generate"); @@ -62,6 +62,44 @@ import {initCore} from "../../js/nibblepoker-core.mjs"; } 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 eOptionCount.addEventListener("change", function() { @@ -96,7 +134,4 @@ import {initCore} from "../../js/nibblepoker-core.mjs"; 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()); } diff --git a/static/resources/NibblePoker/libs/iban.mjs b/static/resources/NibblePoker/libs/iban.mjs index 7c4a4b7..927ba99 100644 --- a/static/resources/NibblePoker/libs/iban.mjs +++ b/static/resources/NibblePoker/libs/iban.mjs @@ -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 { /** * ISO-3166 Country Code @@ -264,6 +268,50 @@ export class IbanSpecification { getFormattedIban(iban) { 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 = { diff --git a/templates/applets/iban-generator.jinja b/templates/applets/iban-generator.jinja index e6caf1e..e1db376 100644 --- a/templates/applets/iban-generator.jinja +++ b/templates/applets/iban-generator.jinja @@ -120,6 +120,11 @@ +
+ + + +