diff --git a/plugins/dates/src/api/parse/range/02-date-range.js b/plugins/dates/src/api/parse/range/02-date-range.js index eb7bba40a..7d34a8285 100644 --- a/plugins/dates/src/api/parse/range/02-date-range.js +++ b/plugins/dates/src/api/parse/range/02-date-range.js @@ -1,5 +1,6 @@ import parseDate from '../one/index.js' import reverseMaybe from './_reverse.js' +import Unit from '../one/units/Unit.js' export default [ { @@ -138,4 +139,56 @@ export default [ return null }, }, + + { + // in 2 to 4 weeks + match: '^in [#Value] to [#Value] [(day|days|week|weeks|month|months|year|years)]', + desc: 'in 2 to 4 weeks', + parse: (m, context) => { + const { min, max, unit } = m.groups() + + let start = new Unit(context.today, null, context) + let end = start.clone() + + const duration = unit.text('implicit') + start = start.applyShift({ [duration]: min.numbers().get()[0] }) + end = end.applyShift({ [duration]: max.numbers().get()[0] }) + + // Ensure that the end date is inclusive + if (!['day', 'days'].includes(duration)) { + end = end.applyShift({ day: -1 }).applyShift({ [duration]: 1 }) + } + + return { + start: start, + end: end.end(), + } + }, + }, + { + // 2 to 4 weeks ago + match: + '[#Value] to [#Value] [(day|days|week|weeks|month|months|year|years)] (ago|before|earlier|prior)', + desc: '2 to 4 weeks ago', + parse: (m, context) => { + const { min, max, unit } = m.groups() + + let start = new Unit(context.today, null, context) + let end = start.clone() + + const duration = unit.text('implicit') + start = start.applyShift({ [duration]: -max.numbers().get()[0] }) + end = end.applyShift({ [duration]: -min.numbers().get()[0] }) + + // Ensure that the end date is inclusive + if (!['day', 'days'].includes(duration)) { + end = end.applyShift({ day: 1 }).applyShift({ [duration]: -1 }) + } + + return { + start: start, + end: end.end(), + } + }, + }, ] diff --git a/plugins/dates/tests/duration-range.test.js b/plugins/dates/tests/duration-range.test.js new file mode 100644 index 000000000..6ee452f5d --- /dev/null +++ b/plugins/dates/tests/duration-range.test.js @@ -0,0 +1,65 @@ +import test from 'tape' +import nlp from './_lib.js' + +// Duration ranges +const durArr = [ + { + text: ['2 to 4 days', '2-4 days', 'two to four days'], + duration: { years: 0, months: 0, days: 3, hours: 0, minutes: 0 }, + }, + { + text: ['1 to 2 weeks', '1-2 weeks', 'one to two weeks'], + duration: { years: 0, months: 0, days: 14, hours: 0, minutes: 0 }, + }, + { + text: ['1 to 5 months', '1-5 months', 'one to five months'], + duration: { years: 0, months: 5, days: 0, hours: 0, minutes: 0 }, + }, + { + text: ['2 to 4 years', '2-4 years', 'two to four years'], + duration: { years: 3, months: 0, days: 0, hours: 0, minutes: 0 }, + }, +] + +const context = { + today: '2024-01-01', +} + +test('future duration-ranges', function (t) { + durArr.forEach(obj => { + obj.text.forEach(text => { + const doc = nlp(`in ${text}`) + const { duration, start, end } = doc.dates(context).get()[0] + t.deepEqual(duration, obj.duration, text) + t.ok(start > context.today, 'start date') + t.ok(end > start, 'end date') + }) + }) + t.end() +}) + +test('past duration-ranges', function (t) { + durArr.forEach(obj => { + obj.text.forEach(text => { + const doc = nlp(`${text} ago`) + const { duration, start, end } = doc.dates(context).get()[0] + t.deepEqual(duration, obj.duration, text) + t.ok(start < context.today, 'start date') + t.ok(end > start, 'end date') + }) + }) + t.end() +}) + +test('past duration-ranges', function (t) { + durArr.forEach(obj => { + obj.text.forEach(text => { + const doc = nlp(`${text} ago`) + const { duration, start, end } = doc.dates(context).get()[0] + t.deepEqual(duration, obj.duration, text) + t.ok(start < context.today, 'start date') + t.ok(end > start, 'end date') + }) + }) + t.end() +})