From 7a0b99449428fc397d7c9f7d28cb1de34c3ecee7 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 24 Jun 2019 13:07:51 +0100 Subject: [PATCH] [Feature] Lua_selectors: Add sort and uniq transform functions --- lualib/lua_selectors.lua | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) 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'] = { -- 2.39.5