From 1351468197f67a4801c3aad9c1f29cee759f6a7d Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Thu, 8 Aug 2024 14:32:26 +0100 Subject: [PATCH 1/4] feat(plugins/dates): duration ranges --- .../src/api/parse/range/02-date-range.js | 22 +++++++++++ plugins/dates/tests/duration-range.test.js | 37 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 plugins/dates/tests/duration-range.test.js 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() +}) From 217871982283863bc3fe68bcb5f359fd05c36a9e Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Thu, 8 Aug 2024 14:57:10 +0100 Subject: [PATCH 2/4] fix(plugins/dates): make durations inclusive --- plugins/dates/src/api/parse/range/02-date-range.js | 7 ++++++- plugins/dates/tests/duration-range.test.js | 8 ++++---- 2 files changed, 10 insertions(+), 5 deletions(-) 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 7df1825e2..5c12013df 100644 --- a/plugins/dates/src/api/parse/range/02-date-range.js +++ b/plugins/dates/src/api/parse/range/02-date-range.js @@ -152,7 +152,12 @@ export default [ const duration = unit.text('implicit') start = start.applyShift({ [duration]: min.numbers().get()[0] }) - end = end.applyShift({ [duration]: max.numbers().get()[0] }).applyShift({ day: -1 }) + 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, diff --git a/plugins/dates/tests/duration-range.test.js b/plugins/dates/tests/duration-range.test.js index 3d5882c42..a7d7dc993 100644 --- a/plugins/dates/tests/duration-range.test.js +++ b/plugins/dates/tests/duration-range.test.js @@ -5,19 +5,19 @@ import nlp from './_lib.js' 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 }, + 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: 7, hours: 0, minutes: 0 }, + 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: 4, days: 0, hours: 0, minutes: 0 }, + 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: 2, months: 0, days: 0, hours: 0, minutes: 0 }, + duration: { years: 3, months: 0, days: 0, hours: 0, minutes: 0 }, }, ] From fd45122ca2056f911cc4adcec503d7ee855f1b18 Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Mon, 12 Aug 2024 15:08:28 +0100 Subject: [PATCH 3/4] fix(plugins/dates): match `^in` --- plugins/dates/src/api/parse/range/02-date-range.js | 6 +++--- plugins/dates/tests/duration-range.test.js | 8 +++++--- 2 files changed, 8 insertions(+), 6 deletions(-) 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 5c12013df..87a7810ce 100644 --- a/plugins/dates/src/api/parse/range/02-date-range.js +++ b/plugins/dates/src/api/parse/range/02-date-range.js @@ -141,9 +141,9 @@ export default [ }, { - // 2 to 4 weeks - match: '[#Value] to [#Value] [(day|days|week|weeks|month|months|year|years)]', - desc: '2 to 4 weeks', + // 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() diff --git a/plugins/dates/tests/duration-range.test.js b/plugins/dates/tests/duration-range.test.js index a7d7dc993..7d584cb93 100644 --- a/plugins/dates/tests/duration-range.test.js +++ b/plugins/dates/tests/duration-range.test.js @@ -25,12 +25,14 @@ const context = { today: '2024-01-01', } -test('duration-ranges', function (t) { +test('future duration-ranges', function (t) { durArr.forEach(obj => { obj.text.forEach(text => { - const doc = nlp(text) - const duration = doc.dates(context).get()[0].duration + 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() From bade69610efe11288842bf821ecadf7881415ea2 Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Mon, 12 Aug 2024 15:57:02 +0100 Subject: [PATCH 4/4] 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() +})