Skip to content

Commit

Permalink
fix(list): add option to ignore empty entries
Browse files Browse the repository at this point in the history
closes: #15
  • Loading branch information
pysan3 committed Jan 17, 2024
1 parent 44163c0 commit c4334dc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lua/pathlib/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function Path:_init(...)
self:copy_all_from(s)
else
assert(not s:is_absolute(), ("new: invalid root path object in %sth argument: %s"):format(i, s))
self._raw_paths:extend(s._raw_paths)
self._raw_paths:extend(s._raw_paths, true)
end
elseif type(s) == "string" then
local path = fs.normalize(s, { expand_env = true }):gsub([[^%./]], ""):gsub([[/%./]], "/"):gsub([[//]], "/")
Expand All @@ -46,7 +46,7 @@ function Path:_init(...)
elseif vim.tbl_contains(splits, "..") then -- deal with '../' later in `self:resolve()`
run_resolve = true
end
self._raw_paths:extend(splits)
self._raw_paths:extend(splits, true)
else
error("PathlibPath(new): ValueError: Invalid type as argument: " .. ("%s (%s: %s)"):format(type(s), i, s))
end
Expand Down
16 changes: 13 additions & 3 deletions lua/pathlib/utils/lists.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,25 @@ function str_list.new()
end

---@param value string
function str_list:append(value)
---@param ignore_empties boolean? # If true, does not add entries that are empty.
function str_list:append(value, ignore_empties)
if ignore_empties and value == "" then
return
end
self[#self + 1] = value
end

---@param list PathlibStrList
function str_list:extend(list)
---@param ignore_empties boolean? # If true, does not add entries that are empty.
function str_list:extend(list, ignore_empties)
local start_from = #self
local ignores = 0
for index, value in ipairs(list) do
self[start_from + index] = value
if ignore_empties and value == "" then
ignores = ignores + 1
else
self[start_from + index - ignores] = value
end
end
end

Expand Down

0 comments on commit c4334dc

Please sign in to comment.