From: Vsevolod Stakhov Date: Mon, 24 Jun 2019 12:07:51 +0000 (+0100) Subject: [Feature] Lua_selectors: Add sort and uniq transform functions X-Git-Tag: 2.0~746 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7a0b99449428fc397d7c9f7d28cb1de34c3ecee7;p=rspamd.git [Feature] Lua_selectors: Add sort and uniq transform functions --- diff --git a/lualib/lua_selectors.lua b/lualib/lua_selectors.lua index b678ef179..15777fc11 100644 --- a/lualib/lua_selectors.lua +++ b/lualib/lua_selectors.lua @@ -423,6 +423,32 @@ local transform_function = { ['description'] = 'Joins strings into a single string using separator in the argument', ['args_schema'] = {ts.string:is_optional()} }, + -- Sort strings + ['sort'] = { + ['types'] = { + ['list'] = true + }, + ['process'] = function(inp, t, _) + table.sort(inp) + return inp, t + end, + ['description'] = 'Sort strings lexicographically', + }, + -- Return unique elements based on hashing (can work without sorting) + ['uniq'] = { + ['types'] = { + ['list'] = true + }, + ['process'] = function(inp, t, _) + local tmp = {} + for _,val in ipairs(inp) do + tmp[val] = true + end + + return fun.map(function(k, _) return k end, tmp), t + end, + ['description'] = 'Returns a list of unique elements (using a hash table)', + }, -- Create a digest from string or a list of strings ['digest'] = { ['types'] = {