Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// If we're in July, the string "in June" should refer to next year's June
const rollOverYearRefiner = new chrono.Refiner();
rollOverYearRefiner.refine = (text, results, opt) => {
results.forEach((result) => {
if (result.start.isCertain('month') &&
!result.start.isCertain('year') &&
result.start.date().getTime() < result.ref.getTime()
) {
result.start.imply('year', result.start.get('year') + 1);
}
});
return results;
};
const rollOverDayRefiner = new chrono.Refiner();
rollOverDayRefiner.refine = (text, results, opt) => {
results.forEach((result) => {
if (result.start.isCertain('hour') &&
!result.start.isCertain('day') &&
result.start.date().getTime() < result.ref.getTime()
) {
result.start.imply('day', result.start.get('day') + 1);
}
});
return results;
};
// Examples: "Tuesday, 9th of July 2019. 19:00 GMT" and "tomorrow by 9pm"
// Both produce two results each: one with the date and one with the time
const combineDateAndTime = new chrono.Refiner();
combineDateAndTime.refine = (text, results, opt) => {
const rollOverDayRefiner = new chrono.Refiner();
rollOverDayRefiner.refine = (text, results, opt) => {
results.forEach((result) => {
if (result.start.isCertain('hour') &&
!result.start.isCertain('day') &&
result.start.date().getTime() < result.ref.getTime()
) {
result.start.imply('day', result.start.get('day') + 1);
}
});
return results;
};
// Examples: "Tuesday, 9th of July 2019. 19:00 GMT" and "tomorrow by 9pm"
// Both produce two results each: one with the date and one with the time
const combineDateAndTime = new chrono.Refiner();
combineDateAndTime.refine = (text, results, opt) => {
if (results.length < 2) {
// Our current data suggests this scenario only yields two results
return results;
}
const resultWithDate = results.find((result) => {
return result.start.isCertain('day') || result.start.isCertain('weekday');
});
const resultWithTime = results.find((result) => {
return result.start.isCertain('hour');
});
if (resultWithDate == undefined || resultWithTime == undefined) {
// Faulty thesis; bail.
return results;
}
'use strict';
const chrono = require('chrono-node');
// Customize the date/time parser.
// Remember: we're only working with dates in the future
// If we're in July, the string "in June" should refer to next year's June
const rollOverYearRefiner = new chrono.Refiner();
rollOverYearRefiner.refine = (text, results, opt) => {
results.forEach((result) => {
if (result.start.isCertain('month') &&
!result.start.isCertain('year') &&
result.start.date().getTime() < result.ref.getTime()
) {
result.start.imply('year', result.start.get('year') + 1);
}
});
return results;
};
const rollOverDayRefiner = new chrono.Refiner();
rollOverDayRefiner.refine = (text, results, opt) => {
results.forEach((result) => {
if (result.start.isCertain('hour') &&
var chrono = require('chrono-node');
var guessPMRefiner = new chrono.Refiner();
const logger = require("../utils/logger");
var moment = require('moment-timezone');
module.exports = {
parse: function(intent, timezone){
var parsedInfo = custom.parse(intent);
logger.debug('parse input timezone: ', timezone);
logger.debug('parsed intent', intent);
parsedInfo[0].start.assign('timezoneOffset', 0);
var rawParsedStartTime = parsedInfo[0].start.date().toISOString();
rawParsedStartTime = rawParsedStartTime.slice(0, -1);
var startDateTime = moment.tz(rawParsedStartTime, timezone).format();
var endDateTime = null;
if (parsedInfo[0].end) {
logger.debug('end time: ', parsedInfo[0]);
function evening(momentDate, eveningHour = Hours.Evening) {
return oclock(momentDate.hour(eveningHour))
}
function midnight(momentDate, midnightHour = Hours.Midnight) {
return oclock(momentDate.hour(midnightHour))
}
function isPastDate(inputDateObj, currentDate) {
const inputMoment = moment({...inputDateObj, month: inputDateObj.month - 1})
const currentMoment = moment(currentDate)
return inputMoment.isBefore(currentMoment)
}
const EnforceFutureDate = new chrono.Refiner();
EnforceFutureDate.refine = (text, results) => {
results.forEach((result) => {
const current = _.extend({}, result.start.knownValues, result.start.impliedValues);
if (result.start.isCertain('weekday') && !result.start.isCertain('day')) {
if (isPastDate(current, result.ref)) {
result.start.imply('day', result.start.impliedValues.day + 7);
}
}
if (result.start.isCertain('day') && !result.start.isCertain('month')) {
if (isPastDate(current, result.ref)) {
result.start.imply('month', result.start.impliedValues.month + 1);
}
}
if (result.start.isCertain('month') && !result.start.isCertain('year')) {
const refDay = ref.getDate();
const refMonth = ref.getMonth() + 1;
const refYear = ref.getFullYear();
if (refYear > year) {
return true;
}
if (refMonth > month) {
return true;
}
if (refDay > day) {
return true;
}
return false;
}
const EnforceFutureDate = new chrono.Refiner();
EnforceFutureDate.refine = (text, results)=> {
results.forEach((result)=> {
const current = _.extend({}, result.start.knownValues, result.start.impliedValues);
if (result.start.isCertain('weekday') && !result.start.isCertain('day')) {
if (isPastDate(current, result.ref)) {
result.start.imply('day', result.start.impliedValues.day + 7);
}
}
if (result.start.isCertain('day') && !result.start.isCertain('month')) {
if (isPastDate(current, result.ref)) {
result.start.imply('month', result.start.impliedValues.month + 1);
}
}
if (result.start.isCertain('month') && !result.start.isCertain('year')) {