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:
@@ -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());
|
||||
}
|
||||
|
@@ -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 = {
|
||||
|
Reference in New Issue
Block a user