UNPKG

chrono-node

Version:

A natural language date parser in Javascript

33 lines (26 loc) • 1.21 kB
import { ParsingContext } from "../../../chrono"; import { ParsingComponents } from "../../../results"; import { AbstractParserWithWordBoundaryChecking } from "../../../common/parsers/AbstractParserWithWordBoundary"; const PATTERN = new RegExp("([0-9]|0[1-9]|1[012])/([0-9]{4})" + "", "i"); const MONTH_GROUP = 1; const YEAR_GROUP = 2; /** * Month/Year date format with slash "/" (also "-" and ".") between numbers * - 11/05 * - 06/2005 */ export default class ENSlashMonthFormatParser extends AbstractParserWithWordBoundaryChecking { innerPattern(): RegExp { return PATTERN; } innerExtract(context: ParsingContext, match: RegExpMatchArray): ParsingComponents | null { // Do not extract a month/year suffix from an invalid numeric date (e.g. "50/6/2018"). // Parsing can also resume within a rejected match, so check the original text. if (/\d\/?$/.test(context.text.substring(0, match.index))) { return null; } const year = parseInt(match[YEAR_GROUP]); const month = parseInt(match[MONTH_GROUP]); return context.createParsingComponents().imply("day", 1).assign("month", month).assign("year", year); } }