util.human.io: Add parse_duration() method to parse a duration string

Similar logic occurs throughout various modules in the codebase. We might even
want a module:get_option_duration()??
This commit is contained in:
Matthew Wild 2023-04-07 14:14:53 +01:00
parent 2fa6a01018
commit 7dc9f9ab2a
2 changed files with 30 additions and 0 deletions

View file

@ -42,6 +42,24 @@ describe("util.human.io", function ()
assert.equal("räksmörgås", human_io.ellipsis("räksmörgås", 10));
end);
end);
describe("parse_duration", function ()
local function test(expected, duration)
assert.equal(expected, human_io.parse_duration(duration));
end
it("works", function ()
test(1, "1s");
test(60, "1mi");
test(60, "1min");
test(60, "1 min");
test(60, "1 minute");
test(120, "2min");
test(86400, "1d");
test(2678400, "1m");
test(2678400, "1month");
test(2678400, "1 month");
end);
end);
end);