Skip to content

Commit

Permalink
add fill method in array.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
EvandroLG committed Jun 30, 2019
1 parent 9c58fbc commit 18f17d6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
18 changes: 18 additions & 0 deletions array.lua
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,24 @@ array = {
end
end

return output
end,

fill = function(value, start_or_finish, finish)
local output = {}
local item = value
local start = start_or_finish
local size = finish

if finish == nil then
start = 1
size = start_or_finish
end

for i=start, size do
output[i] = item
end

return output
end
}
Expand Down
25 changes: 24 additions & 1 deletion test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -268,5 +268,28 @@ test('flat should return a new table that is an one-dimensional flatting of the
local obj = { 'a', 'b', 'c', { 'd', 'e', 'f', { 'g', 'h' } }, { 'i' }, 'j' }
local result = array.flat(obj)

a.equal(10, #result)
a.equal(#result, 10)
end)

test('fill should create a table with the size passed by parameter and fill every item using the value passed as argument', function(a)
local value = 1
local size = 3
local result = array.fill(value, size)

a.equal(#result, size)
a.equal(result[1], value)
a.equal(result[2], value)
a.equal(result[3], value)
end)

test('fill should create a table using the value passed by argument from start to end', function(a)
local value = 'a'
local start = 3
local finish = 4
local result = array.fill(value, start, finish)

a.equal(result[1], nil)
a.equal(result[2], nil)
a.equal(result[3], value)
a.equal(result[4], value)
end)

0 comments on commit 18f17d6

Please sign in to comment.