chrono-node
Version:
A natural language date parser in Javascript
32 lines • 1.18 kB
JavaScript
export class AbstractParserWithWordBoundaryChecking {
constructor() {
this.cachedInnerPattern = null;
this.cachedPattern = null;
}
innerPatternHasChange(context, currentInnerPattern) {
return this.innerPattern(context) !== currentInnerPattern;
}
patternLeftBoundary() {
return `(\\W|^)`;
}
pattern(context) {
if (this.cachedInnerPattern) {
if (!this.innerPatternHasChange(context, this.cachedInnerPattern)) {
return this.cachedPattern;
}
}
this.cachedInnerPattern = this.innerPattern(context);
this.cachedPattern = new RegExp(`${this.patternLeftBoundary()}${this.cachedInnerPattern.source}`, this.cachedInnerPattern.flags);
return this.cachedPattern;
}
extract(context, match) {
const header = match[1] ?? "";
match.index = match.index + header.length;
match[0] = match[0].substring(header.length);
for (let i = 2; i < match.length; i++) {
match[i - 1] = match[i];
}
return this.innerExtract(context, match);
}
}
//# sourceMappingURL=AbstractParserWithWordBoundary.js.map