UNPKG

chrono-node

Version:

A natural language date parser in Javascript

60 lines 2.23 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("(?:on\\s*?)?" + `(${ORDINAL_NUMBER_PATTERN})` + "(?:\\s*" + "(?:tot|\\-|\\–|until|through|till|\\s)\\s*" + `(${ORDINAL_NUMBER_PATTERN})` + ")?" + "(?:-|/|\\s*(?:of)?\\s*)" + "(" + matchAnyPattern(MONTH_DICTIONARY) + ")" + "(?:" + "(?:-|/|,?\\s*)" + `(${YEAR_PATTERN}(?![^\\s]\\d))` + ")?" + "(?=\\W|$)", "i"); const MONTH_NAME_GROUP = 3; const DATE_GROUP = 1; const DATE_TO_GROUP = 2; const YEAR_GROUP = 4; export default class NLMonthNameMiddleEndianParser extends AbstractParserWithWordBoundaryChecking { innerPattern() { return PATTERN; } innerExtract(context, match) { const month = MONTH_DICTIONARY[match[MONTH_NAME_GROUP].toLowerCase()]; const day = parseOrdinalNumberPattern(match[DATE_GROUP]); if (day > 31) { match.index = match.index + match[DATE_GROUP].length; return null; } const components = context.createParsingComponents({ day: day, month: month, }); 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=NLMonthNameMiddleEndianParser.js.map