1- -- array
2- -- author: Evandro Leopoldino Gonçalves <[email protected] >3- -- https://github.com/evandrolg
4- -- License: MIT
5-
6- -- Helper function to check if value passed by parameter is a table
7- -- @obj {table}
8- -- @returns {boolean}
9- local function is_table (obj )
10- return type (obj ) == ' table'
11- end
12-
13- -- Raises error if @param is not an array
14- -- @obj {table}
15- -- @param {table}
16- -- @method {string}
17- -- @returns {void}
18- local function raises_error (obj , param , method )
19- assert (obj .is_array (param ), string.format (' %s expects an array' , method ))
20- end
21-
22- -- Returns lowest value between two values
23- -- @a {number}
24- -- @b {number}
25- -- @returns number
26- local function lowest_value (a , b )
27- return a < b and a or b
28- end
29-
30- -- Makes multiple inserts in a table (array-like)
31- -- @obj {table}
32- -- @values {table}
33- -- @returns {void}
34- local function multiple_inserts (obj , values )
35- for i = 1 , # values do
36- local value = values [i ]
37- table.insert (obj , value )
38- end
39- end
1+ local utils = require (' ./src/utils' )
402
413local function convert_to_hash (obj )
424 local output = {}
@@ -78,7 +40,7 @@ array = {
7840 -- @obj {table}
7941 -- @returns {boolean}
8042 is_array = function (obj )
81- if not is_table (obj ) then return false end
43+ if not utils . is_table (obj ) then return false end
8244
8345 local i = 0
8446 for _ in pairs (obj ) do
@@ -102,7 +64,7 @@ array = {
10264 -- @finish {number} end value
10365 -- @returns {boolean}
10466 slice = function (obj , start , finish )
105- raises_error (array , obj , ' slice' )
67+ utils . raises_error (array , obj , ' slice' )
10668
10769 if array .is_empty (obj ) then return {} end
10870
@@ -121,7 +83,7 @@ array = {
12183 -- @value {*}
12284 -- @returns {boolean}
12385 index_of = function (obj , value )
124- raises_error (array , obj , ' index_of' )
86+ utils . raises_error (array , obj , ' index_of' )
12587
12688 for i = 1 , # obj do
12789 if obj [i ] == value then
@@ -136,7 +98,7 @@ array = {
13698 -- @obj {table}
13799 -- @returns {table}
138100 reverse = function (obj )
139- raises_error (array , obj , ' reverse' )
101+ utils . raises_error (array , obj , ' reverse' )
140102
141103 local output = {}
142104
@@ -151,23 +113,23 @@ array = {
151113 -- @obj {table}
152114 -- @returns {*}
153115 first = function (obj )
154- raises_error (array , obj , ' first' )
116+ utils . raises_error (array , obj , ' first' )
155117 return obj [1 ]
156118 end ,
157119
158120 -- Return last element from the table
159121 -- @obj {table}
160122 -- @returns {*}
161123 last = function (obj )
162- raises_error (array , obj , ' last' )
124+ utils . raises_error (array , obj , ' last' )
163125 return obj [# obj ]
164126 end ,
165127
166128 -- Return maximum value from the table
167129 -- @obj {table}
168130 -- @returns {*}
169131 max = function (obj )
170- raises_error (array , obj , ' max' )
132+ utils . raises_error (array , obj , ' max' )
171133
172134 local max = obj [1 ]
173135
@@ -184,7 +146,7 @@ array = {
184146 -- @obj {table}
185147 -- @returns {*}
186148 min = function (obj )
187- raises_error (array , obj , ' min' )
149+ utils . raises_error (array , obj , ' min' )
188150
189151 local min = obj [1 ]
190152
@@ -202,7 +164,7 @@ array = {
202164 -- @callback {function}
203165 -- @returns {*}
204166 map = function (obj , callback )
205- raises_error (array , obj , ' map' )
167+ utils . raises_error (array , obj , ' map' )
206168
207169 local output = {}
208170
@@ -218,7 +180,7 @@ array = {
218180 -- @callback {function}
219181 -- @returns {*}
220182 filter = function (obj , callback )
221- raises_error (array , obj , ' filter' )
183+ utils . raises_error (array , obj , ' filter' )
222184
223185 local output = {}
224186
@@ -237,7 +199,7 @@ array = {
237199 -- @memo {*}
238200 -- @returns {*}
239201 reduce = function (obj , callback , memo )
240- raises_error (array , obj , ' reduce' )
202+ utils . raises_error (array , obj , ' reduce' )
241203
242204 local initialIndex = 1
243205 local _memo = memo
@@ -260,7 +222,7 @@ array = {
260222 -- @memo {*}
261223 -- @returns {*}
262224 reduce_right = function (obj , callback , memo )
263- raises_error (array , obj , ' reduce_right' )
225+ utils . raises_error (array , obj , ' reduce_right' )
264226
265227 local initialIndex = # obj
266228 local _memo = memo
@@ -282,7 +244,7 @@ array = {
282244 -- @callback {function}
283245 -- @returns {number}
284246 sum = function (obj )
285- raises_error (array , obj , ' sum' )
247+ utils . raises_error (array , obj , ' sum' )
286248
287249 return array .reduce (obj , function (memo , value )
288250 return memo + value
@@ -294,8 +256,8 @@ array = {
294256 -- @obj2 {table}
295257 -- @returns {table}
296258 concat = function (obj , obj2 )
297- raises_error (array , obj , ' concat' )
298- raises_error (array , obj2 , ' concat' )
259+ utils . raises_error (array , obj , ' concat' )
260+ utils . raises_error (array , obj2 , ' concat' )
299261
300262 local output = {}
301263
@@ -314,7 +276,7 @@ array = {
314276 -- @obj {table}
315277 -- @returns {table}
316278 uniq = function (obj )
317- raises_error (array , obj , ' uniq' )
279+ utils . raises_error (array , obj , ' uniq' )
318280
319281 local output = {}
320282 local seen = {}
@@ -335,7 +297,7 @@ array = {
335297 -- @values {table}
336298 -- @returns {table}
337299 without = function (obj , values )
338- raises_error (array , obj , ' without' )
300+ utils . raises_error (array , obj , ' without' )
339301
340302 local output = {}
341303
@@ -353,7 +315,7 @@ array = {
353315 -- @callback {function}
354316 -- @returns {boolean}
355317 some = function (obj , callback )
356- raises_error (array , obj , ' some' )
318+ utils . raises_error (array , obj , ' some' )
357319
358320 for i = 1 , # obj do
359321 if callback (obj [i ], i ) then
@@ -369,8 +331,8 @@ array = {
369331 -- @obj2 {table}
370332 -- @returns {table}
371333 zip = function (obj1 , obj2 )
372- raises_error (array , obj1 , ' zip' )
373- raises_error (array , obj2 , ' zip' )
334+ utils . raises_error (array , obj1 , ' zip' )
335+ utils . raises_error (array , obj2 , ' zip' )
374336
375337 local output = {}
376338 local size = # obj1 > # obj2 and # obj2 or # obj1
@@ -387,7 +349,7 @@ array = {
387349 -- @obj2 {table}
388350 -- @returns {table}
389351 every = function (obj , callback )
390- raises_error (array , obj , ' every' )
352+ utils . raises_error (array , obj , ' every' )
391353
392354 for i = 1 , # obj do
393355 if not callback (obj [i ], i ) then
@@ -402,7 +364,7 @@ array = {
402364 -- @obj {table}
403365 -- @returns {table}
404366 shallow_copy = function (obj )
405- raises_error (array , obj , ' shallow_copy' )
367+ utils . raises_error (array , obj , ' shallow_copy' )
406368
407369 local output = {}
408370
@@ -419,7 +381,7 @@ array = {
419381 deep_copy = function (value )
420382 local output = value
421383
422- if is_table (value ) then
384+ if utils . is_table (value ) then
423385 output = {}
424386
425387 for i = 1 , # value do
@@ -435,8 +397,8 @@ array = {
435397 -- @obj2 {table}
436398 -- @returns {table}
437399 diff = function (obj1 , obj2 )
438- raises_error (array , obj1 , ' diff' )
439- raises_error (array , obj2 , ' diff' )
400+ utils . raises_error (array , obj1 , ' diff' )
401+ utils . raises_error (array , obj2 , ' diff' )
440402
441403 local output = {}
442404 local hash = convert_to_hash (obj2 )
@@ -462,7 +424,7 @@ array = {
462424 for i = 1 , # obj do
463425 local value = obj [i ]
464426
465- if is_table (value ) then
427+ if utils . is_table (value ) then
466428 array .flat (value , output )
467429 else
468430 table.insert (output , value )
@@ -545,13 +507,26 @@ array = {
545507 if count2 [k ] then
546508 local new_array = array .fill (
547509 k ,
548- lowest_value (v , count2 [k ])
510+ utils . lowest_value (v , count2 [k ])
549511 )
550512
551- multiple_inserts (output , new_array )
513+ utils . multiple_inserts (output , new_array )
552514 end
553515 end
554516
517+ return output
518+ end ,
519+
520+ from_pairs = function (obj )
521+ utils .raises_error (array , obj , ' from_pairs' )
522+
523+ local output = {}
524+
525+ for i = 1 , # obj do
526+ local item = obj [i ]
527+ output [item [1 ]] = item [2 ]
528+ end
529+
555530 return output
556531 end
557532}
0 commit comments