UNPKG

@kermank/nldp

Version:

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

92 lines 4.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.timeOnlyRule = void 0; const luxon_1 = require("luxon"); function createTimeComponent(hour, minute, span, originalText, preferences) { const referenceDate = (preferences === null || preferences === void 0 ? void 0 : preferences.referenceDate) || luxon_1.DateTime.now(); // Create time in target timezone first if specified, otherwise UTC let value = (preferences === null || preferences === void 0 ? void 0 : preferences.timeZone) ? luxon_1.DateTime.fromObject({ year: referenceDate.year, month: referenceDate.month, day: referenceDate.day, hour, minute }, { zone: preferences.timeZone }) : luxon_1.DateTime.utc(referenceDate.year, referenceDate.month, referenceDate.day, hour, minute); return { type: 'time', span, value, confidence: 1, metadata: { originalText, rangeType: 'time' } }; } exports.timeOnlyRule = { name: 'time-only', patterns: [ { regex: /(?:^|\s)(?:at\s+)?(\d{1,2}):(\d{2})(?:\s*(AM|PM))?(?:\s|$)/i, parse: (matches, preferences) => { let [fullMatch, hours, minutes, meridiem] = matches; let hour = parseInt(hours); const minute = parseInt(minutes); if (minute >= 60) return null; if (meridiem) { if (hour > 12) return null; if (meridiem.toUpperCase() === 'PM' && hour < 12) hour += 12; if (meridiem.toUpperCase() === 'AM' && hour === 12) hour = 0; } else { if (hour >= 24) return null; } const matchStart = matches.index + (fullMatch.startsWith(' ') ? 1 : 0); const matchEnd = matchStart + fullMatch.trim().length; return createTimeComponent(hour, minute, { start: matchStart, end: matchEnd }, fullMatch.trim(), preferences); } }, { regex: /(?:^|\s)(?:at\s+)?(\d{1,2})(?:\s*)(AM|PM)(?:\s|$)/i, parse: (matches, preferences) => { let [fullMatch, hours, meridiem] = matches; let hour = parseInt(hours); if (hour > 12) return null; if (meridiem.toUpperCase() === 'PM' && hour < 12) hour += 12; if (meridiem.toUpperCase() === 'AM' && hour === 12) hour = 0; const matchStart = matches.index + (fullMatch.startsWith(' ') ? 1 : 0); const matchEnd = matchStart + fullMatch.trim().length; return createTimeComponent(hour, 0, { start: matchStart, end: matchEnd }, fullMatch.trim(), preferences); } }, { regex: /(?:^|\s)(?:at\s+)?noon(?:\s|$)/i, parse: (matches, preferences) => { const fullMatch = matches[0]; const matchStart = matches.index + (fullMatch.startsWith(' ') ? 1 : 0); const matchEnd = matchStart + fullMatch.trim().length; return createTimeComponent(12, 0, { start: matchStart, end: matchEnd }, fullMatch.trim(), preferences); } }, { regex: /(?:^|\s)(?:at\s+)?midnight(?:\s|$)/i, parse: (matches, preferences) => { const fullMatch = matches[0]; const matchStart = matches.index + (fullMatch.startsWith(' ') ? 1 : 0); const matchEnd = matchStart + fullMatch.trim().length; return createTimeComponent(0, 0, { start: matchStart, end: matchEnd }, fullMatch.trim(), preferences); } } ] }; //# sourceMappingURL=time-only.js.map