mirror of
https://github.com/bjc/prosody.git
synced 2025-04-01 20:27:39 +03:00
util.human.io: Don't accept ambiguous durations by default
The new method parse_duration_lax() exports the old behaviour, mainly for compatibility purposes.
This commit is contained in:
parent
56d45091bd
commit
a669ffb5a2
2 changed files with 49 additions and 4 deletions
|
@ -47,6 +47,37 @@ describe("util.human.io", function ()
|
|||
local function test(expected, duration)
|
||||
return assert.equal(expected, human_io.parse_duration(duration), ("%q -> %d"):format(duration, expected));
|
||||
end
|
||||
local function should_fail(duration)
|
||||
assert.is_nil(human_io.parse_duration(duration), "invalid duration should fail: %q");
|
||||
end
|
||||
it("works", function ()
|
||||
test(1, "1s");
|
||||
test(60, "1min");
|
||||
test(60, "1 min");
|
||||
test(60, "1 minute");
|
||||
test(120, "2min");
|
||||
test(7200, "2h");
|
||||
test(7200, "2 hours");
|
||||
test(86400, "1d");
|
||||
test(604800, "1w");
|
||||
test(604800, "1week");
|
||||
test(1814400, "3 weeks");
|
||||
test(2678400, "1month");
|
||||
test(2678400, "1 month");
|
||||
test(31536000, "365 days");
|
||||
test(31556952, "1 year");
|
||||
|
||||
should_fail("two weeks");
|
||||
should_fail("1m");
|
||||
should_fail("1mi");
|
||||
should_fail("1mo");
|
||||
end);
|
||||
end);
|
||||
|
||||
describe("parse_duration_lax", function ()
|
||||
local function test(expected, duration)
|
||||
return assert.equal(expected, human_io.parse_duration_lax(duration), ("%q -> %d"):format(duration, expected));
|
||||
end
|
||||
it("works", function ()
|
||||
test(1, "1s");
|
||||
test(60, "1mi");
|
||||
|
@ -66,7 +97,7 @@ describe("util.human.io", function ()
|
|||
test(2678400, "1 month");
|
||||
test(31536000, "365 days");
|
||||
test(31556952, "1 year");
|
||||
return assert.is_nil(human_io.parse_duration("two weeks"), "\"2 weeks\" -> nil");
|
||||
return assert.is_nil(human_io.parse_duration_lax("two weeks"), "\"2 weeks\" -> nil");
|
||||
end);
|
||||
end);
|
||||
end);
|
||||
|
|
|
@ -200,13 +200,26 @@ end
|
|||
|
||||
local day = 86400;
|
||||
local multipliers = {
|
||||
d = day, w = day * 7, m = 31 * day, mo = 31 * day, y = 365.2425 * day;
|
||||
s = 1, mi = 60, h = 3600, ho = 3600
|
||||
d = day, w = day * 7, mon = 31 * day, y = 365.2425 * day;
|
||||
s = 1, min = 60, h = 3600, ho = 3600
|
||||
};
|
||||
|
||||
local function parse_duration(duration_string)
|
||||
local n, m = duration_string:lower():match("(%d+)%s*([smhdwy]?[io]?n?)");
|
||||
if not n or not multipliers[m] then return nil; end
|
||||
return tonumber(n) * ( multipliers[m] or 1 );
|
||||
end
|
||||
|
||||
local multipliers_lax = setmetatable({
|
||||
m = multipliers.mon;
|
||||
mo = multipliers.mon;
|
||||
mi = multipliers.min;
|
||||
}, { __index = multipliers });
|
||||
|
||||
local function parse_duration_lax(duration_string)
|
||||
local n, m = duration_string:lower():match("(%d+)%s*([smhdwy]?[io]?)");
|
||||
if not n then return nil; end
|
||||
return tonumber(n) * ( multipliers[m] or 1 );
|
||||
return tonumber(n) * ( multipliers_lax[m] or 1 );
|
||||
end
|
||||
|
||||
return {
|
||||
|
@ -223,4 +236,5 @@ return {
|
|||
ellipsis = ellipsis;
|
||||
table = new_table;
|
||||
parse_duration = parse_duration;
|
||||
parse_duration_lax = parse_duration_lax;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue