UNPKG

chrono-node

Version:

A natural language date parser in Javascript

35 lines 1.39 kB
import { MONTH_DICTIONARY } from "../constants.js"; import { findYearClosestToRef } from "../../../calculation/years.js"; import { matchAnyPattern } from "../../../utils/pattern.js"; import { YEAR_PATTERN, parseYear } from "../constants.js"; import { AbstractParserWithWordBoundaryChecking } from "../../../common/parsers/AbstractParserWithWordBoundary.js"; const PATTERN = new RegExp(`(${matchAnyPattern(MONTH_DICTIONARY)})` + `\\s*` + `(?:` + `[,-]?\\s*(${YEAR_PATTERN})?` + ")?" + "(?=[^\\s\\w]|\\s+[^0-9]|\\s+$|$)", "i"); const MONTH_NAME_GROUP = 1; const YEAR_GROUP = 2; export default class NLMonthNameParser extends AbstractParserWithWordBoundaryChecking { innerPattern() { return PATTERN; } innerExtract(context, match) { const components = context.createParsingComponents(); components.imply("day", 1); const monthName = match[MONTH_NAME_GROUP]; const month = MONTH_DICTIONARY[monthName.toLowerCase()]; components.assign("month", month); if (match[YEAR_GROUP]) { const year = parseYear(match[YEAR_GROUP]); components.assign("year", year); } else { const year = findYearClosestToRef(context.refDate, 1, month); components.imply("year", year); } return components; } } //# sourceMappingURL=NLMonthNameParser.js.map