diff --git a/lua/pending/parse.lua b/lua/pending/parse.lua index a0160f1..e8fdfab 100644 --- a/lua/pending/parse.lua +++ b/lua/pending/parse.lua @@ -667,4 +667,29 @@ function M.is_today(due) return time_part >= current_time end +---@param s? string +---@return integer? +function M.parse_duration_to_days(s) + if s == nil or s == '' then + return nil + end + local n = s:match('^(%d+)d$') + if n then + return tonumber(n) --[[@as integer]] + end + n = s:match('^(%d+)w$') + if n then + return tonumber(n) --[[@as integer]] * 7 + end + n = s:match('^(%d+)m$') + if n then + return tonumber(n) --[[@as integer]] * 30 + end + n = s:match('^(%d+)$') + if n then + return tonumber(n) --[[@as integer]] + end + return nil +end + return M diff --git a/spec/parse_spec.lua b/spec/parse_spec.lua index 0e6ac19..0820356 100644 --- a/spec/parse_spec.lua +++ b/spec/parse_spec.lua @@ -416,6 +416,48 @@ describe('parse', function() end) end) + describe('parse_duration_to_days', function() + it('parses days suffix', function() + assert.are.equal(7, parse.parse_duration_to_days('7d')) + end) + + it('parses weeks suffix', function() + assert.are.equal(21, parse.parse_duration_to_days('3w')) + end) + + it('parses months suffix (approximated as 30 days)', function() + assert.are.equal(60, parse.parse_duration_to_days('2m')) + end) + + it('parses bare integer as days', function() + assert.are.equal(30, parse.parse_duration_to_days('30')) + end) + + it('returns nil for nil input', function() + assert.is_nil(parse.parse_duration_to_days(nil)) + end) + + it('returns nil for empty string', function() + assert.is_nil(parse.parse_duration_to_days('')) + end) + + it('returns nil for unrecognized input', function() + assert.is_nil(parse.parse_duration_to_days('xyz')) + end) + + it('returns nil for negative numbers', function() + assert.is_nil(parse.parse_duration_to_days('-7d')) + end) + + it('handles single digit', function() + assert.are.equal(1, parse.parse_duration_to_days('1d')) + end) + + it('handles large numbers', function() + assert.are.equal(365, parse.parse_duration_to_days('365d')) + end) + end) + describe('input_date_formats', function() before_each(function() config.reset()