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..7df1825e2 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,25 @@ export default [ return null }, }, + + { + // 2 to 4 weeks + match: '[#Value] to [#Value] [(day|days|week|weeks|month|months|year|years)]', + desc: '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] }).applyShift({ day: -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..3d5882c42 --- /dev/null +++ b/plugins/dates/tests/duration-range.test.js @@ -0,0 +1,37 @@ +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: 2, hours: 0, minutes: 0 }, + }, + { + text: ['1 to 2 weeks', '1-2 weeks', 'one to two weeks'], + duration: { years: 0, months: 0, days: 7, hours: 0, minutes: 0 }, + }, + { + text: ['1 to 5 months', '1-5 months', 'one to five months'], + duration: { years: 0, months: 4, days: 0, hours: 0, minutes: 0 }, + }, + { + text: ['2 to 4 years', '2-4 years', 'two to four years'], + duration: { years: 2, months: 0, days: 0, hours: 0, minutes: 0 }, + }, +] + +const context = { + today: '2024-01-01', +} + +test('duration-ranges', function (t) { + durArr.forEach(obj => { + obj.text.forEach(text => { + const doc = nlp(text) + const duration = doc.dates(context).get()[0].duration + t.deepEqual(duration, obj.duration, text) + }) + }) + t.end() +})