33-- https://github.com/evandrolg
44-- License: MIT
55
6+ -- Helper function to check if value passed by parameter is a table
7+ -- @obj {table}
8+ -- @returns {boolean}
69local function is_table (obj )
710 return type (obj ) == ' table'
811end
912
13+ -- Raises error if @param is not an array
14+ -- @obj {table}
15+ -- @param {table}
16+ -- @method {string}
17+ -- @returns {void}
1018local function raises_error (obj , param , method )
1119 assert (obj .is_array (param ), string.format (' %s expects an array' , method ))
1220end
@@ -36,6 +44,9 @@ array = {
3644 SOFTWARE.
3745 ]] ,
3846
47+ -- Verify if table object works as an array
48+ -- @obj {table}
49+ -- @returns {boolean}
3950 is_array = function (obj )
4051 if not is_table (obj ) then return false end
4152
@@ -48,10 +59,18 @@ array = {
4859 return true
4960 end ,
5061
62+ -- Check if parameter is an empty table
63+ -- @obj {table}
64+ -- @returns {boolean}
5165 is_empty = function (obj )
5266 return array .is_array (obj ) and # obj == 0
5367 end ,
5468
69+ -- Return a shallow copy of a portion of a table into a new table
70+ -- @obj {table}
71+ -- @start {number} start value
72+ -- @finish {number} end value
73+ -- @returns {boolean}
5574 slice = function (obj , start , finish )
5675 raises_error (array , obj , ' slice' )
5776
@@ -65,6 +84,10 @@ array = {
6584 return output
6685 end ,
6786
87+ -- Return the index at which value can be found or -1 in case value is not present
88+ -- @obj {table}
89+ -- @value {*}
90+ -- @returns {boolean}
6891 index_of = function (obj , value )
6992 raises_error (array , obj , ' index_of' )
7093
@@ -77,6 +100,9 @@ array = {
77100 return - 1
78101 end ,
79102
103+ -- Create a new table with reverse values
104+ -- @obj {table}
105+ -- @returns {table}
80106 reverse = function (obj )
81107 raises_error (array , obj , ' reverse' )
82108
@@ -89,16 +115,25 @@ array = {
89115 return output
90116 end ,
91117
118+ -- Return first element from the table
119+ -- @obj {table}
120+ -- @returns {*}
92121 first = function (obj )
93122 raises_error (array , obj , ' first' )
94123 return obj [1 ]
95124 end ,
96125
126+ -- Return last element from the table
127+ -- @obj {table}
128+ -- @returns {*}
97129 last = function (obj )
98130 raises_error (array , obj , ' last' )
99131 return obj [# obj ]
100132 end ,
101133
134+ -- Return maximum value from the table
135+ -- @obj {table}
136+ -- @returns {*}
102137 max = function (obj )
103138 raises_error (array , obj , ' max' )
104139
@@ -113,6 +148,9 @@ array = {
113148 return max
114149 end ,
115150
151+ -- Return minimum value from the table
152+ -- @obj {table}
153+ -- @returns {*}
116154 min = function (obj )
117155 raises_error (array , obj , ' min' )
118156
@@ -127,6 +165,10 @@ array = {
127165 return min
128166 end ,
129167
168+ -- Create a new table of values by mapping each value in table through a transformation function
169+ -- @obj {table}
170+ -- @callback {function}
171+ -- @returns {*}
130172 map = function (obj , callback )
131173 raises_error (array , obj , ' map' )
132174
@@ -139,6 +181,10 @@ array = {
139181 return output
140182 end ,
141183
184+ -- Create a new table containing all elements that pass truth test
185+ -- @obj {table}
186+ -- @callback {function}
187+ -- @returns {*}
142188 filter = function (obj , callback )
143189 raises_error (array , obj , ' filter' )
144190
@@ -153,6 +199,11 @@ array = {
153199 return output
154200 end ,
155201
202+ -- Applies a function against an accumulator and each value of the table to reduce it to a single value
203+ -- @obj {table}
204+ -- @callback {function}
205+ -- @memo
206+ -- @returns {*}
156207 reduce = function (obj , callback , memo )
157208 raises_error (array , obj , ' reduce' )
158209
@@ -165,6 +216,10 @@ array = {
165216 return _memo
166217 end ,
167218
219+ -- Return a new table joining all values from the two tables passed by parameter
220+ -- @obj {table}
221+ -- @obj2 {table}
222+ -- @returns {table}
168223 concat = function (obj , obj2 )
169224 raises_error (array , obj , ' concat' )
170225 raises_error (array , obj2 , ' concat' )
@@ -182,6 +237,9 @@ array = {
182237 return output
183238 end ,
184239
240+ -- Create a new table, removing duplicates values
241+ -- @obj {table}
242+ -- @returns {table}
185243 uniq = function (obj )
186244 raises_error (array , obj , ' uniq' )
187245
@@ -199,6 +257,10 @@ array = {
199257 return output
200258 end ,
201259
260+ -- Return a copy of the table with all instances of the values removed
261+ -- @obj {table}
262+ -- @values {table}
263+ -- @returns {table}
202264 without = function (obj , values )
203265 raises_error (array , obj , ' without' )
204266
@@ -213,6 +275,10 @@ array = {
213275 return output
214276 end ,
215277
278+ -- Tests if at least one element in the table passes the test implemented by the callback
279+ -- @obj {table}
280+ -- @callback {function}
281+ -- @returns {boolean}
216282 some = function (obj , callback )
217283 raises_error (array , obj , ' some' )
218284
@@ -225,6 +291,10 @@ array = {
225291 return false
226292 end ,
227293
294+ -- Return a table of the two supplied by pairing up equally-positioned elements from both tables
295+ -- @obj {table}
296+ -- @obj2 {table}
297+ -- @returns {table}
228298 zip = function (obj1 , obj2 )
229299 raises_error (array , obj1 , ' zip' )
230300 raises_error (array , obj2 , ' zip' )
@@ -239,6 +309,10 @@ array = {
239309 return output
240310 end ,
241311
312+ -- Return a table of the two supplied by pairing up equally-positioned elements from both tables
313+ -- @obj {table}
314+ -- @obj2 {table}
315+ -- @returns {table}
242316 every = function (obj , callback )
243317 raises_error (array , obj , ' every' )
244318
@@ -251,6 +325,9 @@ array = {
251325 return true
252326 end ,
253327
328+ -- Returns a shallow copy of the table passed as parameter
329+ -- @obj {table}
330+ -- @returns {table}
254331 shallow_copy = function (obj )
255332 raises_error (array , obj , ' shallow_copy' )
256333
@@ -263,6 +340,9 @@ array = {
263340 return output
264341 end ,
265342
343+ -- Return a deep copy of the table passed as parameter
344+ -- @value {*}
345+ -- @returns {table}
266346 deep_copy = function (value )
267347 local output = value
268348
@@ -277,6 +357,10 @@ array = {
277357 return output
278358 end ,
279359
360+ -- Return a new table with the items which exist only in the first table
361+ -- @obj {table}
362+ -- @obj2 {table}
363+ -- @returns {table}
280364 diff = function (obj1 , obj2 )
281365 raises_error (array , obj1 , ' diff' )
282366 raises_error (array , obj2 , ' diff' )
@@ -301,6 +385,10 @@ array = {
301385 return output
302386 end ,
303387
388+ -- Create a new table with the sub-table elements concatenated into it
389+ -- @obj {table}
390+ -- @_memo {table}
391+ -- @returns {table}
304392 flat = function (obj , _memo )
305393 local output = _memo or {}
306394
@@ -317,6 +405,11 @@ array = {
317405 return output
318406 end ,
319407
408+ -- Creates a table filling all the elements from a start index (default one) to an end index with a default value
409+ -- @value {*}
410+ -- @start_or_finish {number}
411+ -- @finish {number}
412+ -- @returns table
320413 fill = function (value , start_or_finish , finish )
321414 local output = {}
322415 local item = value
0 commit comments