From 3a3c66d232b6e91e3baf8cf6c9c64b48d63e57a8 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 31 Oct 2023 15:18:22 +0000 Subject: [PATCH] [Minor] Add selector transformer `filter_method` --- lualib/lua_selectors/transforms.lua | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/lualib/lua_selectors/transforms.lua b/lualib/lua_selectors/transforms.lua index 4b839de28..6c6bc7117 100644 --- a/lualib/lua_selectors/transforms.lua +++ b/lualib/lua_selectors/transforms.lua @@ -542,7 +542,28 @@ Empty string comes the first argument or 'true', non-empty string comes nil]], return res, 'string_list' end, ['description'] = 'Apply a list of method calls to the userdata object', - } + }, + -- Apply method to list of userdata and use it as a filter, excluding elements for which method returns false/nil + ['filter_method'] = { + ['types'] = { + ['userdata_list'] = true + }, + ['process'] = function(inp, t, args) + local meth = args[1] + + if not meth then + logger.errx('invalid method name: %s', args[1]) + return nil + end + + return fun.filter(function(val) + return val[meth](val) + end, inp), 'userdata_list' + end, + ['description'] = 'Apply method to list of userdata and use it as a filter,' .. + ' excluding elements for which method returns false/nil', + ['args_schema'] = { ts.string } + }, } transform_function.match = transform_function.regexp -- 2.39.5