tell-me-when
Version:
human relative date and time parser
58 lines • 1.41 kB
JavaScript
export class ParseNode {
name;
from;
to;
children;
constructor(arg0, from, to, children) {
if (arg0 instanceof ParseNode) {
this.name = arg0.name;
this.from = arg0.from;
this.to = arg0.to;
this.children = arg0.children;
} else {
if (from == null || to == null) {
throw new Error(`from and to are required if first arg isn't a ParseNode`);
}
this.name = arg0;
this.from = from;
this.to = to;
this.children = children;
}
}
static error(from, to = from) {
return new ParseNode('⚠', from, to);
}
static empty(index) {
return new ParseNode(undefined, index, index);
}
substringOf(input) {
return input.substring(this.from, this.to);
}
get isError() {
return this.name === '⚠';
}
get isEmpty() {
return this.from === this.to;
}
find(predicate) {
for (const node of this.findAll(predicate)) {
return node;
}
return undefined;
}
*findAll(predicate) {
if (typeof predicate === 'string') {
yield* this.findAll(node => node.name === predicate);
return;
}
if (predicate.prototype instanceof ParseNode ? this instanceof predicate : predicate(this)) {
yield this;
}
if (this.children) {
for (const child of this.children) {
yield* child.findAll(predicate);
}
}
}
}
//# sourceMappingURL=ParseNode.mjs.map