Skip to content

Commit

Permalink
feat(plugins/dates): duration ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
Fdawgs committed Aug 8, 2024
1 parent 8565e74 commit 1351468
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
22 changes: 22 additions & 0 deletions plugins/dates/src/api/parse/range/02-date-range.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import parseDate from '../one/index.js'
import reverseMaybe from './_reverse.js'
import Unit from '../one/units/Unit.js'

export default [
{
Expand Down Expand Up @@ -138,4 +139,25 @@ export default [
return null
},
},

{
// 2 to 4 weeks
match: '[<min>#Value] to [<max>#Value] [<unit>(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(),
}
},
},
]
37 changes: 37 additions & 0 deletions plugins/dates/tests/duration-range.test.js
Original file line number Diff line number Diff line change
@@ -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()
})

0 comments on commit 1351468

Please sign in to comment.