UNPKG

chrono-node

Version:

A natural language date parser in Javascript

49 lines 2.46 kB
import { TIME_UNIT_DICTIONARY } from "../constants.js"; import { ParsingComponents } from "../../../results.js"; import { matchAnyPattern } from "../../../utils/pattern.js"; import { AbstractParserWithLeftRightBoundaryChecking } from "./AbstractParserWithWordBoundaryChecking.js"; const MODIFIER_WORD_GROUP = 1; const RELATIVE_WORD_GROUP = 2; export default class RURelativeDateFormatParser extends AbstractParserWithLeftRightBoundaryChecking { innerPatternString(context) { return `(в прошлом|на прошлой|на следующей|в следующем|на этой|в этом)\\s*(${matchAnyPattern(TIME_UNIT_DICTIONARY)})`; } innerExtract(context, match) { const modifier = match[MODIFIER_WORD_GROUP].toLowerCase(); const unitWord = match[RELATIVE_WORD_GROUP].toLowerCase(); const timeunit = TIME_UNIT_DICTIONARY[unitWord]; if (modifier == "на следующей" || modifier == "в следующем") { const timeUnits = {}; timeUnits[timeunit] = 1; return ParsingComponents.createRelativeFromReference(context.reference, timeUnits); } if (modifier == "в прошлом" || modifier == "на прошлой") { const timeUnits = {}; timeUnits[timeunit] = -1; return ParsingComponents.createRelativeFromReference(context.reference, timeUnits); } const components = context.createParsingComponents(); let date = new Date(context.reference.instant.getTime()); if (timeunit.match(/week/i)) { date.setDate(date.getDate() - date.getDay()); components.imply("day", date.getDate()); components.imply("month", date.getMonth() + 1); components.imply("year", date.getFullYear()); } else if (timeunit.match(/month/i)) { date.setDate(1); components.imply("day", date.getDate()); components.assign("year", date.getFullYear()); components.assign("month", date.getMonth() + 1); } else if (timeunit.match(/year/i)) { date.setDate(1); date.setMonth(0); components.imply("day", date.getDate()); components.imply("month", date.getMonth() + 1); components.assign("year", date.getFullYear()); } return components; } } //# sourceMappingURL=RURelativeDateFormatParser.js.map