Skip to content

Commit

Permalink
feat(plugins/dates): past duration ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
Fdawgs committed Aug 12, 2024
1 parent fd45122 commit bade696
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
26 changes: 26 additions & 0 deletions plugins/dates/src/api/parse/range/02-date-range.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
'[<min>#Value] to [<max>#Value] [<unit>(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(),
Expand Down
26 changes: 26 additions & 0 deletions plugins/dates/tests/duration-range.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

0 comments on commit bade696

Please sign in to comment.