UNPKG

chrono-node

Version:

A natural language date parser in Javascript

32 lines 1.45 kB
import { TIME_UNITS_PATTERN, parseTimeUnits, TIME_UNITS_NO_ABBR_PATTERN } from "../constants.js"; import { ParsingComponents } from "../../../results.js"; import { AbstractParserWithWordBoundaryChecking } from "../../../common/parsers/AbstractParserWithWordBoundary.js"; import { reverseDuration } from "../../../calculation/duration.js"; const PATTERN = new RegExp(`(this|last|past|next|after|\\+|-)\\s*(${TIME_UNITS_PATTERN})(?=\\W|$)`, "i"); const PATTERN_NO_ABBR = new RegExp(`(this|last|past|next|after|\\+|-)\\s*(${TIME_UNITS_NO_ABBR_PATTERN})(?=\\W|$)`, "i"); export default class ENTimeUnitCasualRelativeFormatParser extends AbstractParserWithWordBoundaryChecking { allowAbbreviations; constructor(allowAbbreviations = true) { super(); this.allowAbbreviations = allowAbbreviations; } innerPattern() { return this.allowAbbreviations ? PATTERN : PATTERN_NO_ABBR; } innerExtract(context, match) { const prefix = match[1].toLowerCase(); let duration = parseTimeUnits(match[2]); if (!duration) { return null; } switch (prefix) { case "last": case "past": case "-": duration = reverseDuration(duration); break; } return ParsingComponents.createRelativeFromReference(context.reference, duration); } } //# sourceMappingURL=ENTimeUnitCasualRelativeFormatParser.js.map