diff options
| author | William Boman <william@redwill.se> | 2022-12-20 08:42:58 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-20 07:42:58 +0000 |
| commit | 25313762aac8fc313b769f20298bd4e6473cc6dd (patch) | |
| tree | e46fee667753f4fd7d16b2eac085348d3ecf821f | |
| parent | feat: add expr module (#775) (diff) | |
| download | mason-25313762aac8fc313b769f20298bd4e6473cc6dd.tar mason-25313762aac8fc313b769f20298bd4e6473cc6dd.tar.gz mason-25313762aac8fc313b769f20298bd4e6473cc6dd.tar.bz2 mason-25313762aac8fc313b769f20298bd4e6473cc6dd.tar.lz mason-25313762aac8fc313b769f20298bd4e6473cc6dd.tar.xz mason-25313762aac8fc313b769f20298bd4e6473cc6dd.tar.zst mason-25313762aac8fc313b769f20298bd4e6473cc6dd.zip | |
feat(functional): add trim_start and assoc (#779)
| -rw-r--r-- | lua/mason-core/functional/init.lua | 2 | ||||
| -rw-r--r-- | lua/mason-core/functional/relation.lua | 4 | ||||
| -rw-r--r-- | lua/mason-core/functional/string.lua | 11 | ||||
| -rw-r--r-- | lua/mason-core/functional/table.lua | 23 | ||||
| -rw-r--r-- | tests/mason-core/functional/list_spec.lua | 36 | ||||
| -rw-r--r-- | tests/mason-core/functional/string_spec.lua | 8 | ||||
| -rw-r--r-- | tests/mason-core/functional/table_spec.lua | 32 |
7 files changed, 110 insertions, 6 deletions
diff --git a/lua/mason-core/functional/init.lua b/lua/mason-core/functional/init.lua index ee185e8b..837f9558 100644 --- a/lua/mason-core/functional/init.lua +++ b/lua/mason-core/functional/init.lua @@ -99,6 +99,7 @@ _.format = string.format _.split = string.split _.gsub = string.gsub _.trim = string.trim +_.trim_start = string.trim_start _.dedent = string.dedent _.starts_with = string.starts_with _.to_upper = string.to_upper @@ -117,6 +118,7 @@ _.invert = tbl.invert _.evolve = tbl.evolve _.merge_left = tbl.merge_left _.dissoc = tbl.dissoc +_.assoc = tbl.assoc ---@module "mason-core.functional.type" local typ = lazy_require "mason-core.functional.type" diff --git a/lua/mason-core/functional/relation.lua b/lua/mason-core/functional/relation.lua index b6ba92b7..1561b030 100644 --- a/lua/mason-core/functional/relation.lua +++ b/lua/mason-core/functional/relation.lua @@ -18,7 +18,9 @@ end, 3) ---@param path any[] ---@param tbl table _.path_satisfies = fun.curryN(function(predicate, path, tbl) - return predicate(vim.tbl_get(tbl, unpack(path))) + -- see https://github.com/neovim/neovim/pull/21426 + local value = vim.tbl_get(tbl, unpack(path)) + return predicate(value) end, 3) ---@param a number diff --git a/lua/mason-core/functional/string.lua b/lua/mason-core/functional/string.lua index 8c66dbc0..2d53a5b6 100644 --- a/lua/mason-core/functional/string.lua +++ b/lua/mason-core/functional/string.lua @@ -85,4 +85,15 @@ _.to_lower = function(str) return str:lower() end +---@param pattern string +---@param str string +_.trim_start = fun.curryN(function(pattern, str) + for i = 1, #str do + if not str:sub(i, i):match(pattern) then + return str:sub(i) + end + end + return str +end, 2) + return _ diff --git a/lua/mason-core/functional/table.lua b/lua/mason-core/functional/table.lua index 276a3180..26db162b 100644 --- a/lua/mason-core/functional/table.lua +++ b/lua/mason-core/functional/table.lua @@ -24,7 +24,9 @@ end, 2) ---@param path any[] ---@param tbl table _.path = fun.curryN(function(path, tbl) - return vim.tbl_get(tbl, unpack(path)) + -- see https://github.com/neovim/neovim/pull/21426 + local value = vim.tbl_get(tbl, unpack(path)) + return value end, 2) ---@generic T, U @@ -97,10 +99,21 @@ _.merge_left = fun.curryN(function(left, right) return vim.tbl_extend("force", right, left) end, 2) ----@generic T : table ----@param key any ----@param tbl T ----@return T +---@generic K, V +---@param key K +---@param value V +---@param tbl table<K, V> +---@return table<K, V> +_.assoc = fun.curryN(function(key, value, tbl) + local res = shallow_clone(tbl) + res[key] = value + return res +end, 3) + +---@generic K, V +---@param key K +---@param tbl table<K, V> +---@return table<K, V> _.dissoc = fun.curryN(function(key, tbl) local res = shallow_clone(tbl) res[key] = nil diff --git a/tests/mason-core/functional/list_spec.lua b/tests/mason-core/functional/list_spec.lua index 7c3d8cfb..6c14e5a9 100644 --- a/tests/mason-core/functional/list_spec.lua +++ b/tests/mason-core/functional/list_spec.lua @@ -265,3 +265,39 @@ describe("functional: list", function() assert.spy(add).was_called_with(10, 5) end) end) + +describe("list immutability", function() + it("should not mutate lists", function() + local og_list = setmetatable({ "a", "b", "c" }, { + __newindex = function() + error "Tried to newindex" + end, + }) + + _.reverse(og_list) + _.list_copy(og_list) + _.filter(_.F, og_list) + _.map(_.to_upper, og_list) + _.filter_map(_.always(Optional.empty()), og_list) + _.each(_.length, og_list) + _.concat(og_list, { "d", "e" }) + _.append("d", og_list) + _.prepend("0", og_list) + _.zip_table({ "first", "second", "third" }, og_list) + _.nth(1, og_list) + _.head(og_list) + _.last(og_list) + _.length(og_list) + _.flatten(og_list) + _.sort_by(_.identity, og_list) + _.uniq_by(_.identity, og_list) + _.join(".", og_list) + _.partition(_.equals "a", og_list) + _.take(2, og_list) + _.drop(2, og_list) + _.drop_last(2, og_list) + _.reduce(_.concat, "", og_list) + + assert.same({ "a", "b", "c" }, og_list) + end) +end) diff --git a/tests/mason-core/functional/string_spec.lua b/tests/mason-core/functional/string_spec.lua index 7bbe06c5..02062f4c 100644 --- a/tests/mason-core/functional/string_spec.lua +++ b/tests/mason-core/functional/string_spec.lua @@ -50,4 +50,12 @@ Ipsum assert.equals("HELLO!", _.to_upper "Hello!") assert.equals("hello!", _.to_lower "Hello!") end) + + it("trim strings", function() + assert.equals("HELLO!", _.trim " HELLO! ") + end) + + it("trim_starts strings", function() + assert.equals("HELLO! ", _.trim_start("%s", " HELLO! ")) + end) end) diff --git a/tests/mason-core/functional/table_spec.lua b/tests/mason-core/functional/table_spec.lua index 357f7120..8ffedeb1 100644 --- a/tests/mason-core/functional/table_spec.lua +++ b/tests/mason-core/functional/table_spec.lua @@ -111,4 +111,36 @@ describe("functional: table", function() c = "c", }, _.dissoc("b", { a = "a", b = "b", c = "c" })) end) + + it("should assoc keys", function() + assert.same({ + a = "a", + b = "b", + c = "c", + }, _.assoc("b", "b", { a = "a", c = "c" })) + end) +end) + +describe("table immutability", function() + it("should not mutate tables", function() + local og_table = setmetatable({ key = "value", imagination = "poor", hotel = "trivago" }, { + __newindex = function() + error "Tried to newindex" + end, + }) + + _.prop("hotel", og_table) + _.path({ "hotel" }, og_table) + _.pick({ "hotel" }, og_table) + _.keys(og_table) + _.size(og_table) + _.from_pairs(_.to_pairs(og_table)) + _.invert(og_table) + _.evolve({ hotel = _.to_upper }, og_table) + _.merge_left(og_table, {}) + _.assoc("new", "value", og_table) + _.dissoc("hotel", og_table) + + assert.same({ key = "value", imagination = "poor", hotel = "trivago" }, og_table) + end) end) |
