From 230ef2765a339769edbf19d900a3df9882ec281f Mon Sep 17 00:00:00 2001 From: spencer kelly Date: Sun, 6 Oct 2024 11:13:28 -0400 Subject: [PATCH] fix between month-month template --- plugins/dates/scratch.js | 23 ++++++++------- plugins/dates/src/api/find/index.js | 4 --- .../src/api/parse/one/01-tokenize/03-time.js | 2 +- plugins/dates/src/api/parse/one/index.js | 2 +- .../src/api/parse/range/02-date-range.js | 29 +++++++++++++++++-- plugins/dates/tests/day-start.test.js | 2 +- plugins/dates/tests/equals.test.js | 4 +++ 7 files changed, 46 insertions(+), 20 deletions(-) diff --git a/plugins/dates/scratch.js b/plugins/dates/scratch.js index 7f5a226fa..97e532f3a 100644 --- a/plugins/dates/scratch.js +++ b/plugins/dates/scratch.js @@ -37,19 +37,20 @@ txt = `5th day of q1 2002` // txt = 'on april 22nd' txt = 'in basically one week from now' txt = 'go shopping with april' -txt = 'between Sept and Oct 2008' -txt = 'only in 2018 and 2020' -txt = '2024/02/05 and 2024/03/09' -txt = 'in 03/28' -txt = 'in 28/28' -txt = '1 to 5 weeks ago' -txt = 'in 2-4 years from now' -txt = 'in 1-2 weeks from now' -txt = 'in 1 to 2 months' -txt = `end of september` +txt = 'between Oct and Sept 2008' +// txt = 'sept 2008 to oct 2008' +// txt = 'only in 2018 and 2020' +// txt = '2024/02/05 and 2024/03/09' +// txt = 'in 03/28' +// txt = 'in 28/28' +// txt = '1 to 5 weeks ago' +// txt = 'in 2-4 years from now' +// txt = 'in 1-2 weeks from now' +// txt = 'in 1 to 2 months' +// txt = `end of september` // nlp.verbose('tagger') -let doc = nlp(txt).debug() +let doc = nlp(txt) // doc.debug('dates') // console.log(doc.dates().get()) // doc.times().format('24h') diff --git a/plugins/dates/src/api/find/index.js b/plugins/dates/src/api/find/index.js index b5ae5f1be..209e22912 100644 --- a/plugins/dates/src/api/find/index.js +++ b/plugins/dates/src/api/find/index.js @@ -1,10 +1,6 @@ import split from './split.js' const findDate = function (doc) { - // if (doc.world.isVerbose() === 'date') { - // doc.debug() - // console.log(' ---') - // } let dates = doc.match('#Date+') // ignore only-durations like '20 minutes' dates = dates.filter(m => { diff --git a/plugins/dates/src/api/parse/one/01-tokenize/03-time.js b/plugins/dates/src/api/parse/one/01-tokenize/03-time.js index 340974be2..0b8132545 100644 --- a/plugins/dates/src/api/parse/one/01-tokenize/03-time.js +++ b/plugins/dates/src/api/parse/one/01-tokenize/03-time.js @@ -66,7 +66,7 @@ const parseTime = function (doc, context) { time = time.not('^(at|by|for|before|this|after)') time = time.not('sharp') time = time.not('on the dot') - // time.debug() + let s = spacetime.now(context.timezone) let now = s.clone() // check for known-times (like 'today') diff --git a/plugins/dates/src/api/parse/one/index.js b/plugins/dates/src/api/parse/one/index.js index 60fbe8db1..e842c62f9 100644 --- a/plugins/dates/src/api/parse/one/index.js +++ b/plugins/dates/src/api/parse/one/index.js @@ -6,7 +6,7 @@ import transform from './03-transform/index.js' const env = typeof process === 'undefined' || !process.env ? self.env || {} : process.env const log = parts => { if (env.DEBUG_DATE) { - console.log(parts) + // console.log(parts)// eslint-disable-line console.log(`\n==== '${parts.doc.text()}' =====`) // eslint-disable-line Object.keys(parts).forEach(k => { if (k !== 'doc' && parts[k]) { 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 19b32e0a8..3da12de8b 100644 --- a/plugins/dates/src/api/parse/range/02-date-range.js +++ b/plugins/dates/src/api/parse/range/02-date-range.js @@ -1,9 +1,34 @@ import parseDate from '../one/index.js' import reverseMaybe from './_reverse.js' -import Unit from '../one/units/Unit.js' -import { Year, Month, CalendarDate } from '../one/units/index.js' +import { Month, CalendarDate } from '../one/units/index.js' export default [ + + + { + // month-ranges have some folksy rules + match: 'between [#Month] and [#Month] [#Year?]', + desc: 'between march and jan', + parse: (m, context) => { + let { start, end, year } = m.groups() + let y = year && year.found ? Number(year.text('reduced')) : context.today.year() + let obj = { month: start.text('reduced'), year: y } + let left = new Month(obj, null, context).start() + obj = { month: end.text('reduced'), year: y } + let right = new Month(obj, null, context).start() + if (left.d.isAfter(right.d)) { + return { + start: right, + end: left, + } + } + return { + start: left, + end: right, + } + }, + }, + { // two explicit dates - 'between friday and sunday' match: 'between [.+] and [.+]', diff --git a/plugins/dates/tests/day-start.test.js b/plugins/dates/tests/day-start.test.js index 7a08d0f20..ad1ef5642 100644 --- a/plugins/dates/tests/day-start.test.js +++ b/plugins/dates/tests/day-start.test.js @@ -17,7 +17,7 @@ let arr = [ 'in august', 'tomorrow', 'q2 1999', - 'between june and july', + // 'between june and july', 'between tuesday and wednesday', 'june 2nd to 5th 2020', 'the 5th of august', diff --git a/plugins/dates/tests/equals.test.js b/plugins/dates/tests/equals.test.js index ecafa1f9a..b87caa354 100644 --- a/plugins/dates/tests/equals.test.js +++ b/plugins/dates/tests/equals.test.js @@ -70,6 +70,10 @@ const arr = [ ['until feb 3 2024', '2020-01-21 to 2024-02-03'], ['first half of march', '2020-03-01 to 2020-03-16'], ['second half of march', '2020-03-16 to 2020-03-30 '], + ['between Sept and Oct', 'sept 2020 to oct 2020'], + ['between Sept and Oct 2008', 'sept 2008 to oct 2008'], + ['between Oct and Sept', 'sept 2020 to oct 2020'], + ['between Oct and Sept 2008', 'sept 2008 to oct 2008'] ] test('date-variety', function (t) {