commit 65e9bfb: [Minor] Add universal keys and values functions

Vsevolod Stakhov vsevolod at highsecure.ru
Tue Jul 2 15:56:03 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-07-02 12:49:42 +0100
URL: https://github.com/rspamd/rspamd/commit/65e9bfb52e614fd83ab3dbbe7124d8f2d18761e7

[Minor] Add universal keys and values functions

---
 lualib/lua_util.lua | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)

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


More information about the Commits mailing list