parent
567b364a59
commit
451ee3b4b8
|
@ -55,7 +55,7 @@ class NumeralSystem {
|
|||
* @param alphabet the symbols representing values in this system
|
||||
* @param caseSensitive `true` if and only if capitalization affects the value of a number in this system
|
||||
*/
|
||||
constructor(base, alphabet, caseSensitive) {
|
||||
constructor(base, alphabet, caseSensitive = false) {
|
||||
this.base = base;
|
||||
this.alphabet = alphabet;
|
||||
this.caseSensitive = caseSensitive;
|
||||
|
@ -103,19 +103,19 @@ class NumeralSystem {
|
|||
}
|
||||
|
||||
/**
|
||||
* Removes disallowed symbols from `baseString`.
|
||||
* Checks whether `testString` is a legal substring of a number in this numerical system.
|
||||
*
|
||||
* @param baseString {string} the string representation of a number in this system
|
||||
* @returns {string} the filtered base string
|
||||
* @param string {string} a substring of a number in this numerical system
|
||||
* @returns {boolean} the filtered base string
|
||||
*/
|
||||
filterBaseString(baseString) {
|
||||
isLegal(string) {
|
||||
const alphabet = this.caseSensitive
|
||||
? (this.alphabet + ",")
|
||||
: (this.alphabet.toLowerCase() + this.alphabet.toUpperCase() + ",");
|
||||
// Regex from https://stackoverflow.com/a/3561711/
|
||||
const alphabetRegex = alphabet.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
|
||||
|
||||
return baseString.replace(new RegExp(`[^${alphabetRegex}]`, "g"), "");
|
||||
return (new RegExp(`^[${alphabetRegex}]*$`)).test(string);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -140,10 +140,13 @@ class NumeralSystemInput {
|
|||
this.wrapper.appendChild(label);
|
||||
|
||||
this.textarea = stringToHtml(`<textarea id="b${base}-input" class="number-input"></textarea>`);
|
||||
this.textarea.addEventListener("beforeinput", event => {
|
||||
if (!this.numeralSystem.isLegal(event.data))
|
||||
event.preventDefault();
|
||||
});
|
||||
this.textarea.addEventListener("input", () => {
|
||||
if (this.textarea.value === "") return;
|
||||
|
||||
this.textarea.value = this.numeralSystem.filterBaseString(this.textarea.value);
|
||||
updateAllInputs(this, this.numeralSystem.basesToDecimals(this.getValues()));
|
||||
});
|
||||
this.wrapper.appendChild(this.textarea);
|
||||
|
@ -361,8 +364,8 @@ const inputs = [
|
|||
new NumeralSystemInput("Binary", new NumeralSystem(2, "01")),
|
||||
new NumeralSystemInput("Octal", new NumeralSystem(8, "01234567")),
|
||||
new NumeralSystemInput("Decimal", new NumeralSystem(10, "0123456789")),
|
||||
new NumeralSystemInput("Duodecimal", new NumeralSystem(12, "0123456789ab", false)),
|
||||
new NumeralSystemInput("Hexadecimal", new NumeralSystem(16, "0123456789abcdef", false)),
|
||||
new NumeralSystemInput("Duodecimal", new NumeralSystem(12, "0123456789ab")),
|
||||
new NumeralSystemInput("Hexadecimal", new NumeralSystem(16, "0123456789abcdef")),
|
||||
new Base64NumeralSystemInput("Base64"),
|
||||
new NumeralSystemInputWithToggleableSeparator(
|
||||
"ASCII",
|
||||
|
|
Loading…
Reference in New Issue