hugo-commandbar/assets/commandbar/js/terms.js

54 lines
1,004 B
JavaScript

class Term {
stringValue;
constructor(stringValue) {
this.stringValue = stringValue;
}
toString() {
return `Term[${stringValue}]`;
}
valueOf() {
return this.stringValue;
}
}
export class NumberTerm extends Term {
value;
constructor(stringValue) {
super(stringValue);
const parsedValue = parseInt(stringValue);
if (isNaN(parsedValue)) {
throw new TypeError("Number value is not a valid number");
}
this.value = parsedValue;
}
valueOf() {
return this.value;
}
toString() {
return `Number[${this.value}]`;
}
}
export class NameTerm extends Term {
static regex = /[-\w]+/;
constructor(stringValue) {
if (!NameTerm.regex.test(stringValue)) {
throw new TypeError("Name must be string with non-zero length");
}
super(stringValue);
}
toString() {
return `Name[${this.stringValue}]`;
}
}