Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(functional): strip_prefix and strip_suffix should not use patterns #1352

Merged
merged 1 commit into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions lua/mason-core/functional/string.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,27 @@ _.trim_end_matches = fun.curryN(function(pattern, str)
end, 2)

_.strip_prefix = fun.curryN(function(prefix_pattern, str)
local _, start = string.find(str, "^" .. prefix_pattern)
if start then
return str:sub(start + 1)
else
if #prefix_pattern > #str then
return str
end
for i = 1, #prefix_pattern do
if str:sub(i, i) ~= prefix_pattern:sub(i, i) then
return str
end
end
return str:sub(#prefix_pattern + 1)
end, 2)

_.strip_suffix = fun.curryN(function(suffix_pattern, str)
local stop = string.find(str, suffix_pattern .. "$")
if stop then
return str:sub(1, stop - 1)
else
if #suffix_pattern > #str then
return str
end
for i = 1, #suffix_pattern do
if str:sub(-i, -i) ~= suffix_pattern:sub(-i, -i) then
return str
end
end
return str:sub(1, -#suffix_pattern - 1)
end, 2)

return _
13 changes: 11 additions & 2 deletions tests/mason-core/functional/string_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,20 @@ Ipsum
it("should strip_prefix", function()
assert.equals("withthewind", _.strip_prefix("gone", "gonewiththewind"))
assert.equals("1.3.0", _.strip_prefix("v", "v1.3.0"))
assert.equals("-30", _.strip_prefix("2023-05", "2023-05-30"))
assert.equals("The same", _.strip_prefix("Not Equals", "The same"))
assert.equals("long_pattern", _.strip_prefix("long_pattern_here", "long_pattern"))
assert.equals("", _.strip_prefix("pattern_here", "pattern_here"))
assert.equals("s", _.strip_prefix("pattern_here", "pattern_heres"))
end)

it("should strip_suffix", function()
assert.equals("gone", _.strip_suffix("withtthewind", "gonewithtthewind"))
assert.equals("name", _.strip_suffix("%.tar%.gz", "name.tar.gz"))
assert.equals("name", _.strip_suffix(".tar.*", "name.tar.gz"))
assert.equals("name", _.strip_suffix(".tar.gz", "name.tar.gz"))
assert.equals("2023", _.strip_suffix("-05-30", "2023-05-30"))
assert.equals("The same", _.strip_suffix("Not Equals", "The same"))
assert.equals("pattern_here", _.strip_suffix("long_pattern_here", "pattern_here"))
assert.equals("", _.strip_suffix("pattern_here", "pattern_here"))
assert.equals("s", _.strip_suffix("pattern_here", "spattern_here"))
end)
end)