Skip to content

Commit

Permalink
Merge pull request #39 from blueyetisoftware/feature/negative-slice
Browse files Browse the repository at this point in the history
Add support for array slicing with negative index
  • Loading branch information
somesocks authored Jan 11, 2024
2 parents 19e955b + adf8bea commit 35b6009
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lockbox/util/array.lua
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,21 @@ end
Array.slice = function(input, start, stop)
local out = {};

if start == nil then
start = 1
elseif start < 0 then
start = #input + start + 1
end
if stop == nil then
stop = #input
elseif stop < 0 then
stop = #input + stop + 1
end

for i = start, stop do
out[i - start + 1] = input[i];
table.insert(out, input[i])
end

return out;
end

Expand Down

0 comments on commit 35b6009

Please sign in to comment.