From c4334dc68d57858ed56dcc290d68e05c9ac57eda Mon Sep 17 00:00:00 2001 From: pysan3 Date: Thu, 18 Jan 2024 00:09:00 +0900 Subject: [PATCH] fix(list): add option to ignore empty entries closes: #15 --- lua/pathlib/base.lua | 4 ++-- lua/pathlib/utils/lists.lua | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lua/pathlib/base.lua b/lua/pathlib/base.lua index 728c444..402aeb5 100644 --- a/lua/pathlib/base.lua +++ b/lua/pathlib/base.lua @@ -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([[//]], "/") @@ -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 diff --git a/lua/pathlib/utils/lists.lua b/lua/pathlib/utils/lists.lua index 199fbc8..457fb1c 100644 --- a/lua/pathlib/utils/lists.lua +++ b/lua/pathlib/utils/lists.lua @@ -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