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