diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2019-11-26 19:23:58 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2019-11-26 19:23:58 +0000 |
commit | a6faa5b82d08a1894c5be15d15531497476ea5cb (patch) | |
tree | 28c61e25fb197a5bcdc19f4da67c838fab50f99d /lualib/lua_util.lua | |
parent | 4310c8af2bb6b4fedce8da920459adf251321064 (diff) | |
download | rspamd-a6faa5b82d08a1894c5be15d15531497476ea5cb.tar.gz rspamd-a6faa5b82d08a1894c5be15d15531497476ea5cb.zip |
[Minor] Add `lua_util.nkeys` utility
Diffstat (limited to 'lualib/lua_util.lua')
-rw-r--r-- | lualib/lua_util.lua | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/lualib/lua_util.lua b/lualib/lua_util.lua index bda8b0c02..207bdc0dc 100644 --- a/lualib/lua_util.lua +++ b/lualib/lua_util.lua @@ -423,6 +423,30 @@ end exports.list_to_hash = list_to_hash --[[[ +-- @function lua_util.nkeys(table|gen, param, state) +-- Returns number of keys in a table (i.e. from both the array and hash parts combined) +-- @param {table} list numerically-indexed table or string, which is treated as a one-element list +-- @return {number} number of keys +-- @example +-- print(lua_util.nkeys({})) -- 0 +-- print(lua_util.nkeys({ "a", nil, "b" })) -- 2 +-- print(lua_util.nkeys({ dog = 3, cat = 4, bird = nil })) -- 2 +-- print(lua_util.nkeys({ "a", dog = 3, cat = 4 })) -- 3 +-- +--]] +local function nkeys(gen, param, state) + local n = 0 + if not param then + for _,_ in pairs(gen) do n = n + 1 end + else + for _,_ in fun.iter(gen, param, state) do n = n + 1 end + end + return n +end + +exports.nkeys = nkeys + +--[[[ -- @function lua_util.parse_time_interval(str) -- Parses human readable time interval -- Accepts 's' for seconds, 'm' for minutes, 'h' for hours, 'd' for days, |