Skip to content

Commit 888240c

Browse files
committed
improve slice logic
1 parent 7ed1b40 commit 888240c

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

src/array/init.lua

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,21 @@ array = {
5959

6060
local output = {}
6161
local _finish = #obj
62+
local _start = 1
63+
64+
if start >= 0 then
65+
_start = start
66+
elseif utils.is_nil(finish) and start < 0 then
67+
_start = #obj + start + 1
68+
end
6269

6370
if (finish and finish >= 0) then
6471
_finish = finish - 1
6572
elseif finish and finish < 0 then
6673
_finish = #obj + finish
6774
end
6875

69-
for i = (start or 1), _finish do
76+
for i = _start, _finish do
7077
table.insert(output, obj[i])
7178
end
7279

test.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ test('slice', function(a)
4040
{ 'javascript', 'python', 'ruby', 'c' }
4141
)
4242

43-
--a.deep_equal(
44-
--array.slice({ 'lua', 'javascript', 'python', 'ruby', 'c' }, -2),
45-
--{ 'ruby', 'c' }
46-
--)
43+
a.deep_equal(
44+
array.slice({ 'lua', 'javascript', 'python', 'ruby', 'c' }, -2),
45+
{ 'ruby', 'c' }
46+
)
4747

4848
a.deep_equal(
4949
array.slice({ 'lua', 'javascript', 'python', 'ruby', 'c' }, 2, -2),

0 commit comments

Comments
 (0)