From a0604613f5887ba14722d710e2b9290b3894e8db Mon Sep 17 00:00:00 2001 From: William Boman Date: Tue, 18 Apr 2023 01:16:39 +0200 Subject: refactor(expr): remove redundant core filters and always stringify reduced value (#1227) The removed filters are not used by mason-registry and are too Lua-specific in nature. --- lua/mason-core/functional/init.lua | 1 + lua/mason-core/functional/relation.lua | 4 ++++ lua/mason-core/installer/registry/expr.lua | 32 ++++++++++++++++++++---------- 3 files changed, 27 insertions(+), 10 deletions(-) (limited to 'lua') diff --git a/lua/mason-core/functional/init.lua b/lua/mason-core/functional/init.lua index 353c5cc1..87a5f325 100644 --- a/lua/mason-core/functional/init.lua +++ b/lua/mason-core/functional/init.lua @@ -70,6 +70,7 @@ _.index_by = list.index_by ---@module "mason-core.functional.relation" local relation = lazy_require "mason-core.functional.relation" _.equals = relation.equals +_.not_equals = relation.not_equals _.prop_eq = relation.prop_eq _.prop_satisfies = relation.prop_satisfies _.path_satisfies = relation.path_satisfies diff --git a/lua/mason-core/functional/relation.lua b/lua/mason-core/functional/relation.lua index 1561b030..5a062c88 100644 --- a/lua/mason-core/functional/relation.lua +++ b/lua/mason-core/functional/relation.lua @@ -6,6 +6,10 @@ _.equals = fun.curryN(function(expected, value) return value == expected end, 2) +_.not_equals = fun.curryN(function(expected, value) + return value ~= expected +end, 2) + _.prop_eq = fun.curryN(function(property, value, tbl) return tbl[property] == value end, 3) diff --git a/lua/mason-core/installer/registry/expr.lua b/lua/mason-core/installer/registry/expr.lua index 5900f576..5dc016eb 100644 --- a/lua/mason-core/installer/registry/expr.lua +++ b/lua/mason-core/installer/registry/expr.lua @@ -13,17 +13,27 @@ local parse_expr = _.compose( _.split "|" ) +---@param predicate fun(value: string): boolean +---@param value string +local take_if = _.curryN(function(predicate, value) + return predicate(value) and value or nil +end, 2) + +---@param predicate fun(value: string): boolean +---@param value string +local take_if_not = _.curryN(function(predicate, value) + return (not predicate(value)) and value or nil +end, 2) + local FILTERS = { - format = _.format, - gsub = _.gsub, + equals = _.equals, + not_equals = _.not_equals, + strip_prefix = _.trim_start_matches, + strip_suffix = _.trim_end_matches, + take_if = take_if, + take_if_not = take_if_not, to_lower = _.to_lower, to_upper = _.to_upper, - trim = _.trim, - trim_start = _.trim_start, - trim_end = _.trim_end, - strip_prefix = _.strip_prefix, - strip_suffix = _.strip_suffix, - tostring = tostring, } ---@generic T : table @@ -60,7 +70,9 @@ function M.interpolate(str, ctx) return filter end, components.filters) - return _.reduce(_.apply_to, value, filters) or "" + local reduced_value = _.reduce(_.apply_to, value, filters) + + return reduced_value ~= nil and tostring(reduced_value) or "" end, str) end) end @@ -68,7 +80,7 @@ end ---@generic T : table ---@param tbl T ---@param ctx table ----@return T +---@return Result # Result function M.tbl_interpolate(tbl, ctx) return Result.try(function(try) local interpolated = {} -- cgit v1.2.3-70-g09d2