Skip to content

Commit

Permalink
feat(functional): add trim_start and assoc (#779)
Browse files Browse the repository at this point in the history
  • Loading branch information
williamboman committed Dec 20, 2022
1 parent ca77c84 commit 2531376
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 6 deletions.
2 changes: 2 additions & 0 deletions lua/mason-core/functional/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion lua/mason-core/functional/relation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions lua/mason-core/functional/string.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 _
23 changes: 18 additions & 5 deletions lua/mason-core/functional/table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions tests/mason-core/functional/list_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
8 changes: 8 additions & 0 deletions tests/mason-core/functional/string_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
32 changes: 32 additions & 0 deletions tests/mason-core/functional/table_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 2531376

Please sign in to comment.