Skip to content
This repository has been archived by the owner on Dec 1, 2017. It is now read-only.

Commit

Permalink
support hasMany option for splice
Browse files Browse the repository at this point in the history
  • Loading branch information
jtarchie committed Nov 29, 2012
1 parent 99e279b commit 0eaf016
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/underscore.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ function _.splice(list, index, howMany, ...)
local elements = {...}
local removed = {}

howMany = howMany or #list - index + 1

for i = 1, #elements, 1 do
table.insert(list, i + index + howMany - 1, elements[i])
end
Expand Down
7 changes: 7 additions & 0 deletions spec/arrays_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -378,4 +378,11 @@ describe("#splice", function()
assert.same(array, {"parrot", "blue", "grape", "mandarin", "surgeon"})
assert.same(removed, {'angel', 'clown'})
end)

it("removes all elements when no hasMany is ", function()
local array = {"angel", "clown", "mandarin", "surgeon"}
local removed = _.splice(array, 3)
assert.same(removed, {"mandarin", "surgeon"})
assert.same(array, {'angel', 'clown'})
end)
end)

0 comments on commit 0eaf016

Please sign in to comment.