From bade69610efe11288842bf821ecadf7881415ea2 Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Mon, 12 Aug 2024 15:57:02 +0100 Subject: [PATCH] feat(plugins/dates): past duration ranges --- .../src/api/parse/range/02-date-range.js | 26 +++++++++++++++++++ plugins/dates/tests/duration-range.test.js | 26 +++++++++++++++++++ 2 files changed, 52 insertions(+) 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 87a7810ce..7d34a8285 100644 --- a/plugins/dates/src/api/parse/range/02-date-range.js +++ b/plugins/dates/src/api/parse/range/02-date-range.js @@ -159,6 +159,32 @@ export default [ 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 index 7d584cb93..6ee452f5d 100644 --- a/plugins/dates/tests/duration-range.test.js +++ b/plugins/dates/tests/duration-range.test.js @@ -37,3 +37,29 @@ test('future duration-ranges', function (t) { }) 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() +})