@kermank/nldp
Version:
A modular date/time parser for converting natural language into dates and times
50 lines • 1.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.dateOnlyRule = void 0;
exports.parse = parse;
const luxon_1 = require("luxon");
function createDateComponent(date, span, originalText, preferences) {
var _a;
const targetZone = (preferences === null || preferences === void 0 ? void 0 : preferences.timeZone) || ((_a = preferences === null || preferences === void 0 ? void 0 : preferences.referenceDate) === null || _a === void 0 ? void 0 : _a.zoneName) || 'UTC';
const finalDate = date.setZone(targetZone, { keepLocalTime: true });
return {
type: 'date',
value: finalDate,
span,
confidence: 1.0,
metadata: {
originalText,
dateType: 'absolute'
}
};
}
function parse(state, input, preferences) {
const datePattern = /^(\d{4})-(\d{2})-(\d{2})$/;
const matches = input.match(datePattern);
if (!matches)
return null;
const [_, year, month, day] = matches;
const date = luxon_1.DateTime.utc(parseInt(year), parseInt(month), parseInt(day));
if (!date.isValid) {
return null;
}
return createDateComponent(date, { start: 0, end: matches[0].length }, matches[0], preferences);
}
const patterns = [
{
regex: /^(\d{4})-(\d{2})-(\d{2})$/,
parse: (matches, preferences) => {
const [_, year, month, day] = matches;
const date = luxon_1.DateTime.utc(parseInt(year), parseInt(month), parseInt(day));
if (!date.isValid) {
return null;
}
return createDateComponent(date, { start: 0, end: matches[0].length }, matches[0], preferences);
}
}
];
exports.dateOnlyRule = {
name: 'date-only',
patterns
};
//# sourceMappingURL=date-only.js.map