UNPKG

chrono-node

Version:

A natural language date parser in Javascript

67 lines 2.52 kB
import { findYearClosestToRef } from "../../../calculation/years.js"; import { MONTH_DICTIONARY } from "../constants.js"; import { ORDINAL_NUMBER_PATTERN, parseOrdinalNumberPattern } from "../constants.js"; import { YEAR_PATTERN, parseYear } from "../constants.js"; import { matchAnyPattern } from "../../../utils/pattern.js"; import { AbstractParserWithWordBoundaryChecking } from "../../../common/parsers/AbstractParserWithWordBoundary.js"; const PATTERN = new RegExp(`(${matchAnyPattern(MONTH_DICTIONARY)})` + "(?:-|/|\\s*,?\\s*)" + `(${ORDINAL_NUMBER_PATTERN})(?!\\s*(?:am|pm))\\s*` + "(?:" + "(?:to|\\-)\\s*" + `(${ORDINAL_NUMBER_PATTERN})\\s*` + ")?" + "(?:" + `(?:-|/|\\s*,\\s*|\\s+)` + `(${YEAR_PATTERN})` + ")?" + "(?=\\W|$)(?!\\:\\d)", "i"); const MONTH_NAME_GROUP = 1; const DATE_GROUP = 2; const DATE_TO_GROUP = 3; const YEAR_GROUP = 4; export default class ENMonthNameMiddleEndianParser extends AbstractParserWithWordBoundaryChecking { constructor(shouldSkipYearLikeDate) { super(); this.shouldSkipYearLikeDate = shouldSkipYearLikeDate; } innerPattern() { return PATTERN; } innerExtract(context, match) { const month = MONTH_DICTIONARY[match[MONTH_NAME_GROUP].toLowerCase()]; const day = parseOrdinalNumberPattern(match[DATE_GROUP]); if (day > 31) { return null; } if (this.shouldSkipYearLikeDate) { if (!match[DATE_TO_GROUP] && !match[YEAR_GROUP] && match[DATE_GROUP].match(/^2[0-5]$/)) { return null; } } const components = context .createParsingComponents({ day: day, month: month, }) .addTag("parser/ENMonthNameMiddleEndianParser"); if (match[YEAR_GROUP]) { const year = parseYear(match[YEAR_GROUP]); components.assign("year", year); } else { const year = findYearClosestToRef(context.refDate, day, month); components.imply("year", year); } if (!match[DATE_TO_GROUP]) { return components; } const endDate = parseOrdinalNumberPattern(match[DATE_TO_GROUP]); const result = context.createParsingResult(match.index, match[0]); result.start = components; result.end = components.clone(); result.end.assign("day", endDate); return result; } } //# sourceMappingURL=ENMonthNameMiddleEndianParser.js.map