UNPKG

@kermank/nldp

Version:

A modular date/time parser for converting natural language into dates and times

92 lines 4.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.partialMonthRule = void 0; const Logger_1 = require("../utils/Logger"); const luxon_1 = require("luxon"); const MONTHS = { january: 1, jan: 1, february: 2, feb: 2, march: 3, mar: 3, april: 4, apr: 4, may: 5, june: 6, jun: 6, july: 7, jul: 7, august: 8, aug: 8, september: 9, sep: 9, october: 10, oct: 10, november: 11, nov: 11, december: 12, dec: 12 }; const PART_RANGES = { early: { start: 1, end: 10 }, beginning: { start: 1, end: 10 }, mid: { start: 11, end: 20 }, middle: { start: 11, end: 20 }, late: { start: 21, end: 31 }, end: { start: 21, end: 31 } }; function createPartialMonthComponent(start, end, span, originalText, preferences) { return { type: 'range', span, value: { start, end }, confidence: 1, metadata: { originalText, rangeType: 'partialMonth' } }; } exports.partialMonthRule = { name: 'partial-month', patterns: [ { regex: /^(early|beginning|mid|middle|late|end)\s+(january|jan|february|feb|march|mar|april|apr|may|june|jun|july|jul|august|aug|september|sep|october|oct|november|nov|december|dec)$/i, parse: (matches, preferences) => { var _a; Logger_1.Logger.debug('Parsing partial month', { matches, preferences }); const [fullMatch, part, month] = matches; if (!part || !month) { Logger_1.Logger.debug('Missing part or month', { part, month }); return null; } const monthNum = MONTHS[month.toLowerCase()]; if (!monthNum) { Logger_1.Logger.debug('Invalid month', { month }); return null; } const range = PART_RANGES[part.toLowerCase()]; if (!range) { Logger_1.Logger.debug('Invalid part', { part }); return null; } const matchStart = matches.index + (fullMatch.startsWith(' ') ? 1 : 0); const matchEnd = matchStart + fullMatch.trim().length; const referenceYear = ((_a = preferences.referenceDate) === null || _a === void 0 ? void 0 : _a.year) || luxon_1.DateTime.now().year; const targetZone = preferences.timeZone || 'UTC'; Logger_1.Logger.debug('Using timezone', { targetZone }); if (preferences.timeZone) { // If timezone specified, create dates directly in target timezone const start = luxon_1.DateTime.fromObject({ year: referenceYear, month: monthNum, day: range.start }, { zone: targetZone }).startOf('day'); const end = luxon_1.DateTime.fromObject({ year: referenceYear, month: monthNum, day: range.end }, { zone: targetZone }).endOf('day'); Logger_1.Logger.debug('Created dates', { start: start.toISO(), end: end.toISO() }); return createPartialMonthComponent(start, end, { start: matchStart, end: matchEnd }, fullMatch.trim(), preferences); } else { // Otherwise use UTC const start = luxon_1.DateTime.fromObject({ year: referenceYear, month: monthNum, day: range.start }, { zone: 'UTC' }).startOf('day'); const end = luxon_1.DateTime.fromObject({ year: referenceYear, month: monthNum, day: range.end }, { zone: 'UTC' }).endOf('day'); Logger_1.Logger.debug('Created dates', { start: start.toISO(), end: end.toISO() }); return createPartialMonthComponent(start, end, { start: matchStart, end: matchEnd }, fullMatch.trim(), preferences); } } } ] }; //# sourceMappingURL=partial-month.js.map