diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2019-07-02 12:49:42 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2019-07-02 16:51:09 +0100 |
commit | 65e9bfb52e614fd83ab3dbbe7124d8f2d18761e7 (patch) | |
tree | 30abe86e8f28729787759533ba7ed21df0772c4d /lualib | |
parent | 51235f510cd7d55448f50501c3a5549b8aaedc06 (diff) | |
download | rspamd-65e9bfb52e614fd83ab3dbbe7124d8f2d18761e7.tar.gz rspamd-65e9bfb52e614fd83ab3dbbe7124d8f2d18761e7.zip |
[Minor] Add universal keys and values functions
Diffstat (limited to 'lualib')
-rw-r--r-- | lualib/lua_util.lua | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/lualib/lua_util.lua b/lualib/lua_util.lua index e8dc57429..fc4f33c83 100644 --- a/lualib/lua_util.lua +++ b/lualib/lua_util.lua @@ -994,4 +994,54 @@ exports.callback_from_string = function(str) return res_or_err end +---[[[ +-- @function lua_util.keys(t) +-- Returns all keys from a specific table +-- @param {table} t input table (or iterator triplet) +-- @return array of keys +--]]] +exports.keys = function(gen, param, state) + local keys = {} + local i = 1 + + if param then + for k,_ in fun.iter(gen, param, state) do + rawset(keys, i, k) + i = i + 1 + end + else + for k,_ in pairs(gen) do + rawset(keys, i, k) + i = i + 1 + end + end + + return keys +end + +---[[[ +-- @function lua_util.values(t) +-- Returns all values from a specific table +-- @param {table} t input table +-- @return array of values +--]]] +exports.values = function(gen, param, state) + local values = {} + local i = 1 + + if param then + for _,v in fun.iter(gen, param, state) do + rawset(values, i, v) + i = i + 1 + end + else + for _,v in pairs(gen) do + rawset(values, i, v) + i = i + 1 + end + end + + return values +end + return exports |