Skip to content

Commit 18f17d6

Browse files
committed
add fill method in array.lua
1 parent 9c58fbc commit 18f17d6

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

array.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,24 @@ array = {
314314
end
315315
end
316316

317+
return output
318+
end,
319+
320+
fill = function(value, start_or_finish, finish)
321+
local output = {}
322+
local item = value
323+
local start = start_or_finish
324+
local size = finish
325+
326+
if finish == nil then
327+
start = 1
328+
size = start_or_finish
329+
end
330+
331+
for i=start, size do
332+
output[i] = item
333+
end
334+
317335
return output
318336
end
319337
}

test.lua

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,5 +268,28 @@ test('flat should return a new table that is an one-dimensional flatting of the
268268
local obj = { 'a', 'b', 'c', { 'd', 'e', 'f', { 'g', 'h' } }, { 'i' }, 'j' }
269269
local result = array.flat(obj)
270270

271-
a.equal(10, #result)
271+
a.equal(#result, 10)
272+
end)
273+
274+
test('fill should create a table with the size passed by parameter and fill every item using the value passed as argument', function(a)
275+
local value = 1
276+
local size = 3
277+
local result = array.fill(value, size)
278+
279+
a.equal(#result, size)
280+
a.equal(result[1], value)
281+
a.equal(result[2], value)
282+
a.equal(result[3], value)
283+
end)
284+
285+
test('fill should create a table using the value passed by argument from start to end', function(a)
286+
local value = 'a'
287+
local start = 3
288+
local finish = 4
289+
local result = array.fill(value, start, finish)
290+
291+
a.equal(result[1], nil)
292+
a.equal(result[2], nil)
293+
a.equal(result[3], value)
294+
a.equal(result[4], value)
272295
end)

0 commit comments

Comments
 (0)