@kermank/nldp
Version:
A modular date/time parser for converting natural language into dates and times
72 lines • 3.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.timeRangesRule = void 0;
const luxon_1 = require("luxon");
const time_parser_1 = require("../utils/time-parser");
function createTimeRangeComponent(startHour, startMinute, endHour, endMinute, span, originalText, preferences) {
const referenceDate = (preferences === null || preferences === void 0 ? void 0 : preferences.referenceDate) || luxon_1.DateTime.now();
// Create times in target timezone first if specified, otherwise UTC
const createTime = (hour, minute, seconds = 0, milliseconds = 0) => {
return (preferences === null || preferences === void 0 ? void 0 : preferences.timeZone)
? luxon_1.DateTime.fromObject({
year: referenceDate.year,
month: referenceDate.month,
day: referenceDate.day,
hour,
minute,
second: seconds,
millisecond: milliseconds
}, { zone: preferences.timeZone })
: luxon_1.DateTime.utc(referenceDate.year, referenceDate.month, referenceDate.day, hour, minute, seconds, milliseconds);
};
let start = createTime(startHour, startMinute);
let end = createTime(endHour, endMinute, 0, 0);
// Compare times in the same timezone
if (end < start) {
end = end.plus({ days: 1 });
}
return {
type: 'range',
span,
value: { start, end },
confidence: 1,
metadata: {
originalText,
rangeType: 'time'
}
};
}
exports.timeRangesRule = {
name: 'time-ranges',
patterns: [
{
// Matches patterns like "3:30 PM to 5:00 PM" or "15:30 to 17:00"
regex: /(?:from\s+)?(\d{1,2}(?::\d{2})?(?:\s*[AaPp][Mm])?)\s+(?:to|until|till|-)\s+(\d{1,2}(?::\d{2})?(?:\s*[AaPp][Mm])?)/i,
parse: (matches, preferences) => {
const [fullMatch, startTimeStr, endTimeStr] = matches;
const startTime = (0, time_parser_1.parseTimeString)(startTimeStr, { allow12Hour: true });
const endTime = (0, time_parser_1.parseTimeString)(endTimeStr, { allow12Hour: true });
if (!startTime || !endTime)
return null;
const matchStart = matches.index + (fullMatch.startsWith(' ') ? 1 : 0);
const matchEnd = matchStart + fullMatch.trim().length;
return createTimeRangeComponent(startTime.hours, startTime.minutes, endTime.hours, endTime.minutes, { start: matchStart, end: matchEnd }, fullMatch.trim(), preferences);
}
},
{
// Matches patterns like "from 3 PM - 5 PM" or "between 15:00-17:00"
regex: /(?:from|between)\s+(\d{1,2}(?::\d{2})?(?:\s*[AaPp][Mm])?)\s*[-–—]\s*(\d{1,2}(?::\d{2})?(?:\s*[AaPp][Mm])?)/i,
parse: (matches, preferences) => {
const [fullMatch, startTimeStr, endTimeStr] = matches;
const startTime = (0, time_parser_1.parseTimeString)(startTimeStr, { allow12Hour: true });
const endTime = (0, time_parser_1.parseTimeString)(endTimeStr, { allow12Hour: true });
if (!startTime || !endTime)
return null;
const matchStart = matches.index + (fullMatch.startsWith(' ') ? 1 : 0);
const matchEnd = matchStart + fullMatch.trim().length;
return createTimeRangeComponent(startTime.hours, startTime.minutes, endTime.hours, endTime.minutes, { start: matchStart, end: matchEnd }, fullMatch.trim(), preferences);
}
}
]
};
//# sourceMappingURL=time-ranges.js.map