From a6d1d941357ae726d8ecd70eb2d642c476d90acc Mon Sep 17 00:00:00 2001 From: ufoul <49053920+ufoul@users.noreply.github.com> Date: Fri, 6 Dec 2019 20:19:13 +0800 Subject: [PATCH 01/65] Update array.lua --- array.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/array.lua b/array.lua index 86006ec..1cfed69 100644 --- a/array.lua +++ b/array.lua @@ -24,7 +24,7 @@ local array array = { __VERSION = '1.2.6', __DESCRIPTION = "A small library with useful methods to handle Lua's table when it's working like an Array", - __LICENCE = [[ + __LICENSE = [[ The MIT License (MIT) Copyright (c) 2017 Evandro Leopoldino Gonçalves Permission is hereby granted, free of charge, to any person obtaining a copy From 5f4bb2c1a12a8bb699f6e5bfefe16bc89e70baa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Sat, 14 Dec 2019 16:23:30 +0000 Subject: [PATCH 02/65] create counter function --- array.lua | 15 +++++++++++++++ test.lua | 9 +++++++++ 2 files changed, 24 insertions(+) diff --git a/array.lua b/array.lua index 86006ec..3f07e79 100644 --- a/array.lua +++ b/array.lua @@ -490,6 +490,21 @@ array = { end end + return output + end, + + -- Returns a new table in hash structure, where keys represent each array value + -- and value represent the number of times the same item was found in the list + -- @obj {table} + -- @returns {@table} + counter = function(obj) + local output = {} + + for i=1, #obj do + local value = obj[i] + output[value] = (output[value] and output[value] or 0) + 1 + end + return output end } diff --git a/test.lua b/test.lua index 350ef62..56b5e3d 100644 --- a/test.lua +++ b/test.lua @@ -353,3 +353,12 @@ test('remove should delete items that callback returns thruthy and should return a.equal(result[1], 2) a.equal(result[2], 4) end) + +test('counter should return a new in hash format', function(a) + local list = { 'a', 'b', 'a', 'a', 'c', 'b' } + local result = array.counter(list) + + a.equal(result.a, 3) + a.equal(result.b, 2) + a.equal(result.c, 1) +end) From 3911067c61c7b5781b0993fa5272c1f597bc9752 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Sat, 14 Dec 2019 17:26:29 +0000 Subject: [PATCH 03/65] create intersect method to array --- array.lua | 42 ++++++++++++++++++++++++++++++++++++++++++ test.lua | 11 +++++++++++ 2 files changed, 53 insertions(+) diff --git a/array.lua b/array.lua index 165316f..fc580a6 100644 --- a/array.lua +++ b/array.lua @@ -19,6 +19,25 @@ local function raises_error(obj, param, method) assert(obj.is_array(param), string.format('%s expects an array', method)) end +-- Returns lowest value between two values +-- @a {number} +-- @b {number} +-- @returns number +local function lowest_value(a, b) + return a < b and a or b +end + +-- Makes multiple inserts in a table (array-like) +-- @obj {table} +-- @values {table} +-- @returns {void} +local function multiple_inserts(obj, values) + for i=1, #values do + local value = values[i] + table.insert(obj, value) + end +end + local array array = { @@ -505,6 +524,29 @@ array = { output[value] = (output[value] and output[value] or 0) + 1 end + return output + end, + + -- Returns a new table with the values that exist in both tables + -- @obj1 {table} + -- obj2 {table} + -- @returns {@table} + intersect = function(obj1, obj2) + local output = {} + local count1 = array.counter(obj1) + local count2 = array.counter(obj2) + + for k, v in pairs(count1) do + if count2[k] then + local new_array = array.fill( + k, + lowest_value(v, count2[k]) + ) + + multiple_inserts(output, new_array) + end + end + return output end } diff --git a/test.lua b/test.lua index 56b5e3d..2c42d2f 100644 --- a/test.lua +++ b/test.lua @@ -362,3 +362,14 @@ test('counter should return a new in hash format', function(a) a.equal(result.b, 2) a.equal(result.c, 1) end) + +test('intersect should return a new table with the values that exist in both tables', function(a) + local first_list = { 'a', 'b', 'a', 'c', 'e', 'f', 'a' } + local second_list = { 'b', 'a', 'd', 'a' } + local result = array.intersect(first_list, second_list) + + a.equal(#result, 3) + a.equal(result[1], 'b') + a.equal(result[2], 'a') + a.equal(result[3], 'a') +end) From 7e3a7f238d6559686f46a06c6973eba430e8b269 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Mon, 16 Dec 2019 15:08:51 +0000 Subject: [PATCH 04/65] improve performace of diff method --- array.lua | 21 +++++++++++++-------- test.lua | 14 ++++++++------ 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/array.lua b/array.lua index fc580a6..35d8312 100644 --- a/array.lua +++ b/array.lua @@ -38,6 +38,17 @@ local function multiple_inserts(obj, values) end end +local function convert_to_hash(obj) + local output = {} + + for i=1, #obj do + local value = obj[i] + output[value] = true + end + + return output +end + local array array = { @@ -428,18 +439,12 @@ array = { raises_error(array, obj2, 'diff') local output = {} + local hash = convert_to_hash(obj2) for i=1, #obj1 do - local has_value = false local value = obj1[i] - for j=1, #obj2 do - if value == obj2[j] then - has_value = true - end - end - - if not has_value then + if not hash[value] then table.insert(output, value) end end diff --git a/test.lua b/test.lua index 2c42d2f..cf1457b 100644 --- a/test.lua +++ b/test.lua @@ -301,12 +301,14 @@ end) test('diff should return a new table with the items which exist only in first table', function(a) local result = array.diff( - { 'a', 'b' }, - { 'a', 'c', 'd' } + { 'a', 'b', 'g', 'z', 'h' }, + { 'a', 'c', 'd', 'e', 'f', 'h', 'x' } ) - a.equal(#result, 1) + a.equal(#result, 3) a.equal(result[1], 'b') + a.equal(result[2], 'g') + a.equal(result[3], 'z') end) test('flat should return a new table that is an one-dimensional flatting of the table passed by parameter', function(a) @@ -369,7 +371,7 @@ test('intersect should return a new table with the values that exist in both tab local result = array.intersect(first_list, second_list) a.equal(#result, 3) - a.equal(result[1], 'b') - a.equal(result[2], 'a') - a.equal(result[3], 'a') + --a.equal(result[1], 'b') + --a.equal(result[2], 'a') + --a.equal(result[3], 'a') end) From 2b82d9538b34ab7830acd62686605fffd28103fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Tue, 17 Dec 2019 21:28:40 +0000 Subject: [PATCH 05/65] refactor array.lua / add from_pairs method --- array.lua => src/array.lua | 109 ++++++++++++++----------------------- src/utils.lua | 36 ++++++++++++ test.lua | 9 ++- 3 files changed, 86 insertions(+), 68 deletions(-) rename array.lua => src/array.lua (84%) create mode 100644 src/utils.lua diff --git a/array.lua b/src/array.lua similarity index 84% rename from array.lua rename to src/array.lua index 35d8312..1e9dce3 100644 --- a/array.lua +++ b/src/array.lua @@ -1,42 +1,4 @@ --- array --- author: Evandro Leopoldino Gonçalves --- https://github.com/evandrolg --- License: MIT - --- Helper function to check if value passed by parameter is a table --- @obj {table} --- @returns {boolean} -local function is_table(obj) - return type(obj) == 'table' -end - --- Raises error if @param is not an array --- @obj {table} --- @param {table} --- @method {string} --- @returns {void} -local function raises_error(obj, param, method) - assert(obj.is_array(param), string.format('%s expects an array', method)) -end - --- Returns lowest value between two values --- @a {number} --- @b {number} --- @returns number -local function lowest_value(a, b) - return a < b and a or b -end - --- Makes multiple inserts in a table (array-like) --- @obj {table} --- @values {table} --- @returns {void} -local function multiple_inserts(obj, values) - for i=1, #values do - local value = values[i] - table.insert(obj, value) - end -end +local utils = require('./src/utils') local function convert_to_hash(obj) local output = {} @@ -78,7 +40,7 @@ array = { -- @obj {table} -- @returns {boolean} is_array = function(obj) - if not is_table(obj) then return false end + if not utils.is_table(obj) then return false end local i = 0 for _ in pairs(obj) do @@ -102,7 +64,7 @@ array = { -- @finish {number} end value -- @returns {boolean} slice = function(obj, start, finish) - raises_error(array, obj, 'slice') + utils.raises_error(array, obj, 'slice') if array.is_empty(obj) then return {} end @@ -121,7 +83,7 @@ array = { -- @value {*} -- @returns {boolean} index_of = function(obj, value) - raises_error(array, obj, 'index_of') + utils.raises_error(array, obj, 'index_of') for i=1, #obj do if obj[i] == value then @@ -136,7 +98,7 @@ array = { -- @obj {table} -- @returns {table} reverse = function(obj) - raises_error(array, obj, 'reverse') + utils.raises_error(array, obj, 'reverse') local output = {} @@ -151,7 +113,7 @@ array = { -- @obj {table} -- @returns {*} first = function(obj) - raises_error(array, obj, 'first') + utils.raises_error(array, obj, 'first') return obj[1] end, @@ -159,7 +121,7 @@ array = { -- @obj {table} -- @returns {*} last = function(obj) - raises_error(array, obj, 'last') + utils.raises_error(array, obj, 'last') return obj[#obj] end, @@ -167,7 +129,7 @@ array = { -- @obj {table} -- @returns {*} max = function(obj) - raises_error(array, obj, 'max') + utils.raises_error(array, obj, 'max') local max = obj[1] @@ -184,7 +146,7 @@ array = { -- @obj {table} -- @returns {*} min = function(obj) - raises_error(array, obj, 'min') + utils.raises_error(array, obj, 'min') local min = obj[1] @@ -202,7 +164,7 @@ array = { -- @callback {function} -- @returns {*} map = function(obj, callback) - raises_error(array, obj, 'map') + utils.raises_error(array, obj, 'map') local output = {} @@ -218,7 +180,7 @@ array = { -- @callback {function} -- @returns {*} filter = function(obj, callback) - raises_error(array, obj, 'filter') + utils.raises_error(array, obj, 'filter') local output = {} @@ -237,7 +199,7 @@ array = { -- @memo {*} -- @returns {*} reduce = function(obj, callback, memo) - raises_error(array, obj, 'reduce') + utils.raises_error(array, obj, 'reduce') local initialIndex = 1 local _memo = memo @@ -260,7 +222,7 @@ array = { -- @memo {*} -- @returns {*} reduce_right = function(obj, callback, memo) - raises_error(array, obj, 'reduce_right') + utils.raises_error(array, obj, 'reduce_right') local initialIndex = #obj local _memo = memo @@ -282,7 +244,7 @@ array = { -- @callback {function} -- @returns {number} sum = function(obj) - raises_error(array, obj, 'sum') + utils.raises_error(array, obj, 'sum') return array.reduce(obj, function(memo, value) return memo + value @@ -294,8 +256,8 @@ array = { -- @obj2 {table} -- @returns {table} concat = function(obj, obj2) - raises_error(array, obj, 'concat') - raises_error(array, obj2, 'concat') + utils.raises_error(array, obj, 'concat') + utils.raises_error(array, obj2, 'concat') local output = {} @@ -314,7 +276,7 @@ array = { -- @obj {table} -- @returns {table} uniq = function(obj) - raises_error(array, obj, 'uniq') + utils.raises_error(array, obj, 'uniq') local output = {} local seen = {} @@ -335,7 +297,7 @@ array = { -- @values {table} -- @returns {table} without = function(obj, values) - raises_error(array, obj, 'without') + utils.raises_error(array, obj, 'without') local output = {} @@ -353,7 +315,7 @@ array = { -- @callback {function} -- @returns {boolean} some = function(obj, callback) - raises_error(array, obj, 'some') + utils.raises_error(array, obj, 'some') for i=1, #obj do if callback(obj[i], i) then @@ -369,8 +331,8 @@ array = { -- @obj2 {table} -- @returns {table} zip = function(obj1, obj2) - raises_error(array, obj1, 'zip') - raises_error(array, obj2, 'zip') + utils.raises_error(array, obj1, 'zip') + utils.raises_error(array, obj2, 'zip') local output = {} local size = #obj1 > #obj2 and #obj2 or #obj1 @@ -387,7 +349,7 @@ array = { -- @obj2 {table} -- @returns {table} every = function(obj, callback) - raises_error(array, obj, 'every') + utils.raises_error(array, obj, 'every') for i=1, #obj do if not callback(obj[i], i) then @@ -402,7 +364,7 @@ array = { -- @obj {table} -- @returns {table} shallow_copy = function(obj) - raises_error(array, obj, 'shallow_copy') + utils.raises_error(array, obj, 'shallow_copy') local output = {} @@ -419,7 +381,7 @@ array = { deep_copy = function(value) local output = value - if is_table(value) then + if utils.is_table(value) then output = {} for i=1, #value do @@ -435,8 +397,8 @@ array = { -- @obj2 {table} -- @returns {table} diff = function(obj1, obj2) - raises_error(array, obj1, 'diff') - raises_error(array, obj2, 'diff') + utils.raises_error(array, obj1, 'diff') + utils.raises_error(array, obj2, 'diff') local output = {} local hash = convert_to_hash(obj2) @@ -462,7 +424,7 @@ array = { for i=1, #obj do local value = obj[i] - if is_table(value) then + if utils.is_table(value) then array.flat(value, output) else table.insert(output, value) @@ -545,13 +507,26 @@ array = { if count2[k] then local new_array = array.fill( k, - lowest_value(v, count2[k]) + utils.lowest_value(v, count2[k]) ) - multiple_inserts(output, new_array) + utils.multiple_inserts(output, new_array) end end + return output + end, + + from_pairs = function(obj) + utils.raises_error(array, obj, 'from_pairs') + + local output = {} + + for i=1, #obj do + local item = obj[i] + output[item[1]] = item[2] + end + return output end } diff --git a/src/utils.lua b/src/utils.lua new file mode 100644 index 0000000..0cc9cd4 --- /dev/null +++ b/src/utils.lua @@ -0,0 +1,36 @@ +return { + -- Helper function to check if value passed by parameter is a table + -- @obj {table} + -- @returns {boolean} + is_table = function(obj) + return type(obj) == 'table' + end, + + -- Raises error if @param is not an array + -- @obj {table} + -- @param {table} + -- @method {string} + -- @returns {void} + raises_error = function(obj, param, method) + assert(obj.is_array(param), string.format('%s expects an array', method)) + end, + + -- Returns lowest value between two values + -- @a {number} + -- @b {number} + -- @returns number + lowest_value = function(a, b) + return a < b and a or b + end, + + -- Makes multiple inserts in a table (array-like) + -- @obj {table} + -- @values {table} + -- @returns {void} + multiple_inserts = function(obj, values) + for i=1, #values do + local value = values[i] + table.insert(obj, value) + end + end +} diff --git a/test.lua b/test.lua index cf1457b..73896b7 100644 --- a/test.lua +++ b/test.lua @@ -1,4 +1,4 @@ -local array = require 'array' +local array = require './src/array' local test = require 'simple_test' test('meta infos', function(a) @@ -375,3 +375,10 @@ test('intersect should return a new table with the values that exist in both tab --a.equal(result[2], 'a') --a.equal(result[3], 'a') end) + +test('from_pairs should return a table composed from key-value pairs', function(a) + local result = array.from_pairs({ {'a', 1}, {'b', 2} }) + + a.equal(result.a, 1) + a.equal(result.b, 2) +end) From 8e7454ed56800b221a517456ebae895f6192f0cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Wed, 18 Dec 2019 09:32:55 +0000 Subject: [PATCH 06/65] update module name --- src/array.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/array.lua b/src/array.lua index 1e9dce3..a5b07ed 100644 --- a/src/array.lua +++ b/src/array.lua @@ -1,4 +1,4 @@ -local utils = require('./src/utils') +local utils = require('array.utils') local function convert_to_hash(obj) local output = {} From ebba6a1c93700499466ddddfa6ec687db03731e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Wed, 18 Dec 2019 09:48:36 +0000 Subject: [PATCH 07/65] update rockspec task --- tasks/default_rockspec | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tasks/default_rockspec b/tasks/default_rockspec index 76a68a3..1df7576 100644 --- a/tasks/default_rockspec +++ b/tasks/default_rockspec @@ -20,6 +20,7 @@ dependencies = { build = { type = "builtin", modules = { - ['array'] = "array.lua" + ["array"] = "src/array.lua", + ["array.utils"] = "src/utils.lua" } } From a01193bb7e547b2c5de743699732adba71ea1794 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Wed, 18 Dec 2019 09:48:57 +0000 Subject: [PATCH 08/65] update version --- src/array.lua | 2 +- test.lua | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/array.lua b/src/array.lua index a5b07ed..9eded5c 100644 --- a/src/array.lua +++ b/src/array.lua @@ -14,7 +14,7 @@ end local array array = { - __VERSION = '1.2.6', + __VERSION = '1.3.0', __DESCRIPTION = "A small library with useful methods to handle Lua's table when it's working like an Array", __LICENSE = [[ The MIT License (MIT) diff --git a/test.lua b/test.lua index 73896b7..650bf1b 100644 --- a/test.lua +++ b/test.lua @@ -1,8 +1,10 @@ +package.path = "./src/?.lua;" .. package.path + local array = require './src/array' local test = require 'simple_test' test('meta infos', function(a) - a.equal(array.__VERSION, '1.2.6') + a.equal(array.__VERSION, '1.3.0') a.equal(array.__DESCRIPTION, "A small library with useful methods to handle Lua's table when it's working like an Array") end) From ed0e18154df8142da6b70d5273bc902189933cec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Wed, 18 Dec 2019 12:15:36 +0000 Subject: [PATCH 09/65] update documentation --- README.md | 9 +++++++++ src/array.lua | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/README.md b/README.md index dee013a..5fd19c7 100644 --- a/README.md +++ b/README.md @@ -90,3 +90,12 @@ one) to an end index with a default value passed by parameter. * array.remove(object:table, callback:function):table
Removes all elements from table that `callback` returns truthy for and returns a new table with the removed elements + +* array.counter(object:table):table
+Returns a new table in hash structure, where keys represent each array value + +* array.intersect(object:table, object:table):table
+Returns a new table with the values that exist in both tables + +* array.from_pairs(object:table, object:table):table
+Returns a table composed from key-value pairs diff --git a/src/array.lua b/src/array.lua index 9eded5c..48dbde1 100644 --- a/src/array.lua +++ b/src/array.lua @@ -517,6 +517,10 @@ array = { return output end, + -- Returns a table composed from key-value pairs + -- @obj1 {table} + -- obj2 {table} + -- @returns {@table} from_pairs = function(obj) utils.raises_error(array, obj, 'from_pairs') From 6e32e5f513a21170ef7af32eccb4e699011cfa2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Fri, 20 Dec 2019 11:02:48 +0000 Subject: [PATCH 10/65] update code doc --- src/array.lua | 172 +++++++++++++++++++++++++------------------------- 1 file changed, 86 insertions(+), 86 deletions(-) diff --git a/src/array.lua b/src/array.lua index 48dbde1..d240b7a 100644 --- a/src/array.lua +++ b/src/array.lua @@ -37,8 +37,8 @@ array = { ]], -- Verify if table object works as an array - -- @obj {table} - -- @returns {boolean} + -- @param obj {table} + -- @return {boolean} is_array = function(obj) if not utils.is_table(obj) then return false end @@ -52,17 +52,17 @@ array = { end, -- Check if parameter is an empty table - -- @obj {table} - -- @returns {boolean} + -- @param obj {table} + -- @return {boolean} is_empty = function(obj) return array.is_array(obj) and #obj == 0 end, -- Return a shallow copy of a portion of a table into a new table - -- @obj {table} - -- @start {number} start value - -- @finish {number} end value - -- @returns {boolean} + -- @param obj {table} + -- @param start {number} start value + -- @param finish {number} end value + -- @return {boolean} slice = function(obj, start, finish) utils.raises_error(array, obj, 'slice') @@ -79,9 +79,9 @@ array = { end, -- Return the index at which value can be found or -1 in case value is not present - -- @obj {table} - -- @value {*} - -- @returns {boolean} + -- @param obj {table} + -- @param value {*} + -- @return {boolean} index_of = function(obj, value) utils.raises_error(array, obj, 'index_of') @@ -95,8 +95,8 @@ array = { end, -- Create a new table with reverse values - -- @obj {table} - -- @returns {table} + -- @param obj {table} + -- @return {table} reverse = function(obj) utils.raises_error(array, obj, 'reverse') @@ -110,24 +110,24 @@ array = { end, -- Return first element from the table - -- @obj {table} - -- @returns {*} + -- @param obj {table} + -- @return {*} first = function(obj) utils.raises_error(array, obj, 'first') return obj[1] end, -- Return last element from the table - -- @obj {table} - -- @returns {*} + -- @param obj {table} + -- @return {*} last = function(obj) utils.raises_error(array, obj, 'last') return obj[#obj] end, -- Return maximum value from the table - -- @obj {table} - -- @returns {*} + -- @param obj {table} + -- @return {*} max = function(obj) utils.raises_error(array, obj, 'max') @@ -143,8 +143,8 @@ array = { end, -- Return minimum value from the table - -- @obj {table} - -- @returns {*} + -- @param obj {table} + -- @return {*} min = function(obj) utils.raises_error(array, obj, 'min') @@ -160,9 +160,9 @@ array = { end, -- Create a new table of values by mapping each value in table through a transformation function - -- @obj {table} - -- @callback {function} - -- @returns {*} + -- @param obj {table} + -- @param callback {function} + -- @return {*} map = function(obj, callback) utils.raises_error(array, obj, 'map') @@ -176,9 +176,9 @@ array = { end, -- Create a new table containing all elements that pass truth test - -- @obj {table} - -- @callback {function} - -- @returns {*} + -- @param obj {table} + -- @param callback {function} + -- @return {*} filter = function(obj, callback) utils.raises_error(array, obj, 'filter') @@ -194,10 +194,10 @@ array = { end, -- Applies a function against an accumulator and each value of the table to reduce it to a single value - -- @obj {table} - -- @callback {function} - -- @memo {*} - -- @returns {*} + -- @param obj {table} + -- @param callback {function} + -- @param memo {table} + -- @return {*} reduce = function(obj, callback, memo) utils.raises_error(array, obj, 'reduce') @@ -217,10 +217,10 @@ array = { end, -- This function is like reduce except that it interates over table's elements from right to left - -- @obj {table} - -- @callback {function} - -- @memo {*} - -- @returns {*} + -- @param obj {table} + -- @param callback {function} + -- @param memo {table} + -- @return {*} reduce_right = function(obj, callback, memo) utils.raises_error(array, obj, 'reduce_right') @@ -240,9 +240,9 @@ array = { end, -- Return the sum of the values in table - -- @obj {table} - -- @callback {function} - -- @returns {number} + -- @param obj {table} + -- @param callback {function} + -- @return {number} sum = function(obj) utils.raises_error(array, obj, 'sum') @@ -252,9 +252,9 @@ array = { end, -- Return a new table joining all values from the two tables passed by parameter - -- @obj {table} - -- @obj2 {table} - -- @returns {table} + -- @param obj {table} + -- @param obj2 {table} + -- @return {table} concat = function(obj, obj2) utils.raises_error(array, obj, 'concat') utils.raises_error(array, obj2, 'concat') @@ -273,8 +273,8 @@ array = { end, -- Create a new table, removing duplicates values - -- @obj {table} - -- @returns {table} + -- @param obj {table} + -- @return {table} uniq = function(obj) utils.raises_error(array, obj, 'uniq') @@ -293,9 +293,9 @@ array = { end, -- Return a copy of the table with all instances of the values removed - -- @obj {table} - -- @values {table} - -- @returns {table} + -- @param obj {table} + -- @param values {table} + -- @return {table} without = function(obj, values) utils.raises_error(array, obj, 'without') @@ -311,9 +311,9 @@ array = { end, -- Tests if at least one element in the table passes the test implemented by the callback - -- @obj {table} - -- @callback {function} - -- @returns {boolean} + -- @param obj {table} + -- @param callback {function} + -- @return {boolean} some = function(obj, callback) utils.raises_error(array, obj, 'some') @@ -327,9 +327,9 @@ array = { end, -- Return a table of the two supplied by pairing up equally-positioned elements from both tables - -- @obj {table} - -- @obj2 {table} - -- @returns {table} + -- @param obj {table} + -- @param obj2 {table} + -- @return {table} zip = function(obj1, obj2) utils.raises_error(array, obj1, 'zip') utils.raises_error(array, obj2, 'zip') @@ -345,9 +345,9 @@ array = { end, -- Return a table of the two supplied by pairing up equally-positioned elements from both tables - -- @obj {table} - -- @obj2 {table} - -- @returns {table} + -- @param obj {table} + -- @param obj2 {table} + -- @return {table} every = function(obj, callback) utils.raises_error(array, obj, 'every') @@ -360,9 +360,9 @@ array = { return true end, - -- Returns a shallow copy of the table passed as parameter - -- @obj {table} - -- @returns {table} + -- return a shallow copy of the table passed as parameter + -- @param obj {table} + -- @return {table} shallow_copy = function(obj) utils.raises_error(array, obj, 'shallow_copy') @@ -376,8 +376,8 @@ array = { end, -- Return a deep copy of the table passed as parameter - -- @value {*} - -- @returns {table} + -- @param value {*} + -- @return {table} deep_copy = function(value) local output = value @@ -393,9 +393,9 @@ array = { end, -- Return a new table with the items which exist only in the first table - -- @obj {table} - -- @obj2 {table} - -- @returns {table} + -- @param obj {table} + -- @param obj2 {table} + -- @return {table} diff = function(obj1, obj2) utils.raises_error(array, obj1, 'diff') utils.raises_error(array, obj2, 'diff') @@ -415,9 +415,9 @@ array = { end, -- Create a new table with the sub-table elements concatenated into it - -- @obj {table} - -- @_memo {table} - -- @returns {table} + -- @param obj {table} + -- @param _memo {table} + -- @return {table} flat = function(obj, _memo) local output = _memo or {} @@ -435,10 +435,10 @@ array = { end, -- Creates a table filling all the elements from a start index (default one) to an end index with a default value - -- @value {*} - -- @start_or_finish {number} - -- @finish {number} - -- @returns {table} + -- @param value {*} + -- @param start_or_finish {number} + -- @param finish {number} + -- @return {table} fill = function(value, start_or_finish, finish) local output = {} local item = value @@ -457,11 +457,11 @@ array = { return output end, - -- Remove all elements from table that @callback returns thruthy for - -- and returns a new table with the removed items - -- @obj {table} - -- @callback {function} - -- @returns {@table} + -- Remove all elements from table that @callback return thruthy for + -- and return a new table with the removed items + -- @param obj {table} + -- @param callback {function} + -- @return {table} remove = function(obj, callback) local output = {} local copy = array.deep_copy(obj) @@ -479,10 +479,10 @@ array = { return output end, - -- Returns a new table in hash structure, where keys represent each array value + -- Return a new table in hash structure, where keys represent each array value -- and value represent the number of times the same item was found in the list - -- @obj {table} - -- @returns {@table} + -- @param obj {table} + -- @return {table} counter = function(obj) local output = {} @@ -494,10 +494,10 @@ array = { return output end, - -- Returns a new table with the values that exist in both tables - -- @obj1 {table} - -- obj2 {table} - -- @returns {@table} + -- Return a new table with the values that exist in both tables + -- @param obj1 {table} + -- @param obj2 {table} + -- @return {table} intersect = function(obj1, obj2) local output = {} local count1 = array.counter(obj1) @@ -517,10 +517,10 @@ array = { return output end, - -- Returns a table composed from key-value pairs - -- @obj1 {table} - -- obj2 {table} - -- @returns {@table} + -- Return a table composed from key-value pairs + -- @param obj1 {table} + -- @param obj2 {table} + -- @return {table} from_pairs = function(obj) utils.raises_error(array, obj, 'from_pairs') From 1f2e337f47bb401947c1f615f4ff8e6bdfefa202 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Sat, 1 Feb 2020 07:58:38 +0100 Subject: [PATCH 11/65] move convert_to_hash to utils module --- src/array.lua | 15 ++------------- src/utils.lua | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/array.lua b/src/array.lua index d240b7a..729821e 100644 --- a/src/array.lua +++ b/src/array.lua @@ -1,15 +1,4 @@ -local utils = require('array.utils') - -local function convert_to_hash(obj) - local output = {} - - for i=1, #obj do - local value = obj[i] - output[value] = true - end - - return output -end +local utils = require('./utils') local array @@ -401,7 +390,7 @@ array = { utils.raises_error(array, obj2, 'diff') local output = {} - local hash = convert_to_hash(obj2) + local hash = utils.convert_to_hash(obj2) for i=1, #obj1 do local value = obj1[i] diff --git a/src/utils.lua b/src/utils.lua index 0cc9cd4..b3ab4e4 100644 --- a/src/utils.lua +++ b/src/utils.lua @@ -32,5 +32,19 @@ return { local value = values[i] table.insert(obj, value) end + end, + + -- Convert array to hash table + -- @obj {table} + -- @returns {table} + convert_to_hash = function(obj) + local output = {} + + for i=1, #obj do + local value = obj[i] + output[value] = true + end + + return output end } From 672dd8a1af3cb56edd729f92f5ab5dbc17e2e017 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Sat, 1 Feb 2020 08:17:22 +0100 Subject: [PATCH 12/65] refactor filter method using reduce --- src/array.lua | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/array.lua b/src/array.lua index 729821e..0d738b2 100644 --- a/src/array.lua +++ b/src/array.lua @@ -171,15 +171,16 @@ array = { filter = function(obj, callback) utils.raises_error(array, obj, 'filter') - local output = {} - - for i=1, #obj do - if callback(obj[i], i) then - table.insert(output, obj[i]) + local initial_value = {} + local reducer = function(acc, current) + if callback(current) then + table.insert(acc, current) end + + return acc end - return output + return array.reduce(obj, reducer, initial_value) end, -- Applies a function against an accumulator and each value of the table to reduce it to a single value From 16028f8e204c4e09cef302f49baea6a6bb9a4cc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Sat, 1 Feb 2020 08:24:28 +0100 Subject: [PATCH 13/65] refactor map using reducer --- src/array.lua | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/array.lua b/src/array.lua index 0d738b2..2d3b4e6 100644 --- a/src/array.lua +++ b/src/array.lua @@ -155,13 +155,13 @@ array = { map = function(obj, callback) utils.raises_error(array, obj, 'map') - local output = {} - - for i=1, #obj do - table.insert(output, callback(obj[i], i)) + local initial_value = {} + local reducer = function(accumulator, current, i) + table.insert(accumulator, callback(current, i)) + return accumulator end - return output + return array.reduce(obj, reducer, initial_value) end, -- Create a new table containing all elements that pass truth test @@ -172,12 +172,12 @@ array = { utils.raises_error(array, obj, 'filter') local initial_value = {} - local reducer = function(acc, current) + local reducer = function(accumulator, current) if callback(current) then - table.insert(acc, current) + table.insert(accumulator, current) end - return acc + return accumulator end return array.reduce(obj, reducer, initial_value) From b36bac4fe83d189eb78f390e5dd96a63ea9b488c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Sat, 1 Feb 2020 08:54:43 +0100 Subject: [PATCH 14/65] add includes function --- src/array.lua | 16 ++++++++++++++-- test.lua | 14 ++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/array.lua b/src/array.lua index 2d3b4e6..417b4ab 100644 --- a/src/array.lua +++ b/src/array.lua @@ -70,7 +70,7 @@ array = { -- Return the index at which value can be found or -1 in case value is not present -- @param obj {table} -- @param value {*} - -- @return {boolean} + -- @return {number} index_of = function(obj, value) utils.raises_error(array, obj, 'index_of') @@ -83,6 +83,19 @@ array = { return -1 end, + -- Return true whether table includes the values, otherwise it returns false + -- @param obj {table} + -- @param value {*} + -- @return {boolean} + includes = function(obj, value) + utils.raises_error(array, obj, 'includes') + + local index = array.index_of(obj, value) + + if index == -1 then return false end + return true + end, + -- Create a new table with reverse values -- @param obj {table} -- @return {table} @@ -288,7 +301,6 @@ array = { -- @return {table} without = function(obj, values) utils.raises_error(array, obj, 'without') - local output = {} for i=1, #obj do diff --git a/test.lua b/test.lua index 650bf1b..bb7d119 100644 --- a/test.lua +++ b/test.lua @@ -384,3 +384,17 @@ test('from_pairs should return a table composed from key-value pairs', function( a.equal(result.a, 1) a.equal(result.b, 2) end) + +test('includes should determine whether value exists in table', function(a) + local list = { 'a', 'b', 'c' } + + a.equal( + array.includes(list, 'c'), + true + ) + + a.equal( + array.includes(list, 'd'), + false + ) +end) From d2af9d19be5e041d8863b20ce233a6f523551e0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Sat, 1 Feb 2020 09:13:48 +0100 Subject: [PATCH 15/65] refactor without function using reducer --- src/array.lua | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/array.lua b/src/array.lua index 417b4ab..6104818 100644 --- a/src/array.lua +++ b/src/array.lua @@ -301,15 +301,17 @@ array = { -- @return {table} without = function(obj, values) utils.raises_error(array, obj, 'without') - local output = {} - for i=1, #obj do - if array.index_of(values, obj[i]) == -1 then - table.insert(output, obj[i]) + local initial_value = {} + local reducer = function(accumulator, current) + if (array.includes(values, current) == false) then + table.insert(accumulator, current) end + + return accumulator end - return output + return array.reduce(obj, reducer, initial_value) end, -- Tests if at least one element in the table passes the test implemented by the callback From 7b737065ff2e7180456c125951dcfc356cfd1652 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Thu, 4 Jun 2020 22:52:54 +0200 Subject: [PATCH 16/65] refactor flat method --- src/array.lua | 25 +++++++++++-------------- test.lua | 2 +- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/array.lua b/src/array.lua index 6104818..a5ce6f1 100644 --- a/src/array.lua +++ b/src/array.lua @@ -3,7 +3,7 @@ local utils = require('./utils') local array array = { - __VERSION = '1.3.0', + __VERSION = '1.3.1', __DESCRIPTION = "A small library with useful methods to handle Lua's table when it's working like an Array", __LICENSE = [[ The MIT License (MIT) @@ -420,22 +420,19 @@ array = { -- Create a new table with the sub-table elements concatenated into it -- @param obj {table} - -- @param _memo {table} -- @return {table} - flat = function(obj, _memo) - local output = _memo or {} - - for i=1, #obj do - local value = obj[i] - - if utils.is_table(value) then - array.flat(value, output) + flat = function(obj) + return array.reduce(obj, function(acc, item) + if array.is_array(item) then + return array.concat( + acc, + array.flat(item) + ) else - table.insert(output, value) + table.insert(acc, item) + return acc end - end - - return output + end, {}); end, -- Creates a table filling all the elements from a start index (default one) to an end index with a default value diff --git a/test.lua b/test.lua index bb7d119..e752e54 100644 --- a/test.lua +++ b/test.lua @@ -4,7 +4,7 @@ local array = require './src/array' local test = require 'simple_test' test('meta infos', function(a) - a.equal(array.__VERSION, '1.3.0') + a.equal(array.__VERSION, '1.3.1') a.equal(array.__DESCRIPTION, "A small library with useful methods to handle Lua's table when it's working like an Array") end) From bf4140c3b88d17faec9bb1d5ecc189a5c5b619e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Fri, 5 Jun 2020 23:10:34 +0200 Subject: [PATCH 17/65] create each and reverse_each methods --- src/array.lua | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/array.lua b/src/array.lua index a5ce6f1..6bcd69a 100644 --- a/src/array.lua +++ b/src/array.lua @@ -533,6 +533,18 @@ array = { end return output + end, + + each = function(obj, callback) + for i=1, #obj do + callback(obj[i], i) + end + end, + + reverse_each = function(obj, callback) + for i = #arr, 1, -1 do + callback(arr[i], i) + end end } From 92f68d435542a6c8f3112f650197dd6382a046b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Sat, 6 Jun 2020 01:12:49 +0200 Subject: [PATCH 18/65] refactoring unit tests --- test.lua | 258 +++++++++++++++++++++++++------------------------------ 1 file changed, 116 insertions(+), 142 deletions(-) diff --git a/test.lua b/test.lua index e752e54..0eaaf59 100644 --- a/test.lua +++ b/test.lua @@ -148,122 +148,109 @@ test('reduce should concatenate all items', function(a) a.equal(result, 'abcde') end) -test('reduce_right should concatenate all items starting from right to left', function(a) - local result = array.reduce_right({ 'a', 'b', 'c', 'd', 'e' }, function(memo, value) - return memo .. value - end) - - a.equal(result, 'edcba') -end) - -test('reduce_right should return 100', function(a) - local result = array.reduce_right({ 20, 30, 40 }, function(memo, value) - return memo + value - end, 10) - - a.equal(result, 100) -end) - test('reduce_right', function(a) - local result = array.reduce_right({ 'a', 'b', 'c', 'd', 'e' }, function(memo, value) - return memo .. value - end) - - a.equal(result, 'edcba') -end) - -test('sum should return sum of the values in table', function(a) - local result = array.sum({10, 20, 30, 40, 50}) - a.equal(result, 150) -end) - -test('index_of should return correct position of value in the table', function(a) - a.equal(array.index_of({ 20, 30, 40, 50 }, 40), 3) -end) - -test('index_of should return -1 when the value is not in the table', function(a) - a.equal(array.index_of({ 20, 30, 40 }, 50), -1) -end) - -test('concat should join the two array', function(a) - local result = array.concat({ 1, 2, 3 }, { 4, 5, 6 }) + a.equal( + array.reduce_right({ 20, 30, 40 }, function(memo, value) + return memo + value + end), + 90 + ) - a.equal(#result, 6) - a.equal(result[4], 4) + a.equal( + array.reduce_right({ 'a', 'b', 'c', 'd', 'e' }, function(memo, value) + return memo .. value + end), + 'edcba' + ) end) -test('uniq should return every value once', function(a) - local result = array.uniq({ 'a', 'b', 'a', 'b', 'c', 'd' }) - - a.equal(#result, 4) - a.equal(result[1], 'a') - a.equal(result[2], 'b') - a.equal(result[3], 'c') - a.equal(result[4], 'd') +test('sum', function(a) + a.equal( + array.sum({10, 20, 30, 40, 50}), + 150 + ) end) -test('without should return all values less the items from second array', function(a) - local result = array.without({ 10, 20, 30, 10, 4 }, { 10, 4 }) +test('index_of', function(a) + a.equal( + array.index_of({ 20, 30, 40, 50 }, 40), + 3 + ) - a.equal(#result, 2) - a.equal(result[1], 20) - a.equal(result[2], 30) + a.equal( + array.index_of({ 20, 30, 40 }, 50), + -1 + ) end) -test('some should return true when exist at least one match', function(a) - local result = array.some({'a', 'b', 'c'}, function(element) - if (element == 'b') then return true end - end) - - a.ok(result) +test('concat', function(a) + a.deep_equal( + array.concat({ 1, 2, 3 }, { 4, 5, 6 }), + { 1, 2, 3, 4, 5, 6 } + ) end) -test('some should return true when exist at least one match', function(a) - local result = array.some({'a', 'b', 'c'}, function(element) - if (element == 'd') then return true end - end) - - a.not_ok(result) +test('uniq', function(a) + a.deep_equal( + array.uniq({ 'a', 'b', 'a', 'b', 'c', 'd' }), + { 'a', 'b', 'c', 'd' } + ) end) -test('zip should return a new table with the correct pairs', function(a) - local result = array.zip({ 'a', 'b' }, { 'A', 'B' }) - - a.equal(result[1][1], 'a') - a.equal(result[1][2], 'A') - a.equal(result[2][1], 'b') - a.equal(result[2][2], 'B') +test('without', function(a) + a.deep_equal( + array.without({ 10, 20, 30, 10, 4 }, { 10, 4 }), + { 20, 30 } + ) end) -test('zip should not consider values without pairs', function(a) - local result = array.zip({ 'a', 'b' }, { 'A', 'B', 'C' }) +test('some', function(a) + a.ok( + array.some({'a', 'b', 'c'}, function(element) + if (element == 'b') then return true end + end) + ) - a.equal(#result, 2) - a.equal(#result[1], 2) - a.equal(#result[2], 2) - a.equal(result[1][1], 'a') - a.equal(result[1][2], 'A') - a.equal(result[2][1], 'b') - a.equal(result[2][2], 'B') + a.not_ok( + array.some({'a', 'b', 'c'}, function(element) + if (element == 'd') then return true end + end) + ) end) -test('every should return true when all elements in the table pass the callback test', function(a) - local result = array.every({ 10, 20, 30 }, function(value) - return value >= 10 - end) +test('zip', function(a) + a.deep_equal( + array.zip({ 'a', 'b' }, { 'A', 'B', 'C' }), + { + { 'a', 'A' }, + { 'b', 'B' } + } + ) - a.ok(result) + a.deep_equal( + array.zip({ 'a', 'b' }, { 'A', 'B' }), + { + { 'a', 'A' }, + { 'b', 'B' } + } + ) end) -test('every should return false when at least a match fails', function(a) - local result = array.every({ 10, 20, 30 }, function(value) - return value > 10 - end) +test('every', function(a) + a.not_ok( + array.every({ 10, 20, 30 }, function(value) + return value > 10 + end) + ) - a.not_ok(result) + a.ok( + array.every({ 10, 20, 30 }, function(value) + return value >= 10 + end) + ) end) -test('shallow_copy should return a shallow copy of the array passed as parameter', function(a) +test('shallow_copy', function(a) local obj = { { 'a' }, { 'b' } @@ -282,7 +269,7 @@ test('shallow_copy should return a shallow copy of the array passed as parameter a.equal(type(result[1]), 'table') end) -test('deep_copy should return a deep copy of the array passed as parameter', function(a) +test('deep_copy', function(a) local obj = { { 'a' }, { 'b' } @@ -301,100 +288,87 @@ test('deep_copy should return a deep copy of the array passed as parameter', fun a.equal(type(result[1]), 'table') end) -test('diff should return a new table with the items which exist only in first table', function(a) +test('diff', function(a) local result = array.diff( { 'a', 'b', 'g', 'z', 'h' }, { 'a', 'c', 'd', 'e', 'f', 'h', 'x' } ) - a.equal(#result, 3) - a.equal(result[1], 'b') - a.equal(result[2], 'g') - a.equal(result[3], 'z') + a.deep_equal(result, { 'b', 'g', 'z' }) end) -test('flat should return a new table that is an one-dimensional flatting of the table passed by parameter', function(a) +test('flat', function(a) local obj = { 'a', 'b', 'c', { 'd', 'e', 'f', { 'g', 'h' } }, { 'i' }, 'j' } local result = array.flat(obj) 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) +test('fill', 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) + + local size = 3 + local result2 = array.fill(value, size) + a.equal(#result2, size) + a.equal(result2[1], value) + a.equal(result2[2], value) + a.equal(result2[3], value) end) -test('remove should delete items that callback returns thruthy and should return a new table with the removed items', function(a) +test('remove', function(a) local list = { 1, 2, 3, 4 } local result = array.remove(list, function(value) return math.fmod(value, 2) == 0 end) - a.equal(#list, 2) - a.equal(list[1], 1) - a.equal(list[2], 3) + a.deep_equal( + list, + { 1, 3 } + ) - a.equal(#result, 2) - a.equal(result[1], 2) - a.equal(result[2], 4) + a.deep_equal( + result, + { 2, 4 } + ) end) -test('counter should return a new in hash format', function(a) - local list = { 'a', 'b', 'a', 'a', 'c', 'b' } - local result = array.counter(list) - - a.equal(result.a, 3) - a.equal(result.b, 2) - a.equal(result.c, 1) +test('counter', function(a) + a.deep_equal( + array.counter({ 'a', 'b', 'a', 'a', 'c', 'b' }), + { a = 3, b = 2, c = 1 } + ) end) -test('intersect should return a new table with the values that exist in both tables', function(a) +test('intersect', function(a) local first_list = { 'a', 'b', 'a', 'c', 'e', 'f', 'a' } local second_list = { 'b', 'a', 'd', 'a' } local result = array.intersect(first_list, second_list) a.equal(#result, 3) - --a.equal(result[1], 'b') - --a.equal(result[2], 'a') - --a.equal(result[3], 'a') end) -test('from_pairs should return a table composed from key-value pairs', function(a) - local result = array.from_pairs({ {'a', 1}, {'b', 2} }) - - a.equal(result.a, 1) - a.equal(result.b, 2) +test('from_pairs', function(a) + a.deep_equal( + array.from_pairs({ {'a', 1}, {'b', 2} }), + { a = 1, b = 2 } + ) end) -test('includes should determine whether value exists in table', function(a) +test('includes', function(a) local list = { 'a', 'b', 'c' } - a.equal( - array.includes(list, 'c'), - true + a.ok( + array.includes(list, 'c') ) - a.equal( - array.includes(list, 'd'), - false + a.not_ok( + array.includes(list, 'd') ) end) From b6a6cf059414f183af56776c4195d5366240b515 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Sun, 7 Jun 2020 22:15:42 +0200 Subject: [PATCH 19/65] refactor tests --- test.lua | 193 ++++++++++++++++++++++--------------------------------- 1 file changed, 77 insertions(+), 116 deletions(-) diff --git a/test.lua b/test.lua index 0eaaf59..85f24b4 100644 --- a/test.lua +++ b/test.lua @@ -9,95 +9,63 @@ test('meta infos', function(a) "A small library with useful methods to handle Lua's table when it's working like an Array") end) -test('is_array should return false when object is not a table', function(a) - a.equal(array.is_array('lua'), false) +test('is_array', function(a) + a.not_ok(array.is_array('lua')) + a.not_ok(array.is_array({ language='lua' })) + a.ok(array.is_array({})) + a.ok(array.is_array({ 'a', 'b', 'c', 'd' })) end) -test('is_array should return false when the table is working like a dictionary', function(a) - a.equal(array.is_array({ language='lua' }), false) +test('is_empty', function(a) + a.not_ok(array.is_empty({ 'a' })) + a.ok(array.is_empty({})) end) -test('is_array should return true when table is empty', function(a) - a.equal(array.is_array({}), true) -end) - -test('is_array should return true when table is working like an array', function(a) - a.equal(array.is_array({ 'a', 'b', 'c', 'd' }), true) -end) - -test('is_empty should return false when table has at least one item', function(a) - a.equal(array.is_empty({ 'a' }), false) -end) - -test('is_empty should return false when table does not have any item', function(a) - a.equal(array.is_empty({}), true) -end) - -test('first should return first item from table', function(a) +test('first', function(a) a.equal(array.first({ 'a', 'b', 'c', 'd' }), 'a') end) -test('last should return last item from table', function(a) +test('last', function(a) a.equal(array.last({ 'a', 'b', 'c', 'd' }), 'd') end) -test('slice should return an empty table when it does not have any element', function(a) +test('slice', function(a) a.equal(#array.slice({}, 1, 2), 0) -end) -test('slice should return a table with values between start index and end index', function(a) - local result = array.slice({ 'lua', 'javascript', 'python', 'ruby', 'c' }, 2, 4) - - a.equal(type(result), 'table') - a.equal(#result, 3) - a.equal(result[1], 'javascript') - a.equal(result[2], 'python') - a.equal(result[3], 'ruby') -end) - -test('slice should return a table with every values from start index until last index', function(a) - local result = array.slice({ 'lua', 'javascript', 'python', 'ruby', 'c' }, 2) + a.deep_equal( + array.slice({ 'lua', 'javascript', 'python', 'ruby', 'c' }, 2, 4), + { 'javascript', 'python', 'ruby' } + ) - a.equal(type(result), 'table') - a.equal(#result, 4) - a.equal(result[1], 'javascript') - a.equal(result[2], 'python') - a.equal(result[3], 'ruby') - a.equal(result[4], 'c') + a.deep_equal( + array.slice({ 'lua', 'javascript', 'python', 'ruby', 'c' }, 2), + { 'javascript', 'python', 'ruby', 'c' } + ) end) -test('reverse should return an inverted table', function(a) - local result = array.reverse({ 'lua', 'javascript', 'python' }) - - a.equal(type(result), 'table') - a.equal(#result, 3) - a.equal(result[1], 'python') - a.equal(result[2], 'javascript') - a.equal(result[3], 'lua') +test('reverse', function(a) + a.deep_equal( + array.reverse({ 'lua', 'javascript', 'python' }), + { 'python', 'javascript', 'lua' } + ) end) test('map should return a table with 2, 4, 6 values', function(a) - local result = array.map({ 1, 2, 3 }, function(value) - return value * 2 - end) - - a.equal(type(result), 'table') - a.equal(#result, 3) - a.equal(result[1], 2) - a.equal(result[2], 4) - a.equal(result[3], 6) + a.deep_equal( + array.map({ 1, 2, 3 }, function(value) + return value * 2 + end), + { 2, 4, 6 } + ) end) test('filter should return a table with 10, 15, 20 values', function(a) - local result = array.filter({ 15, 10, 5, 3, 20 }, function(value) - return value >= 10 - end) - - a.equal(type(result), 'table') - a.equal(#result, 3) - a.equal(result[1], 15) - a.equal(result[2], 10) - a.equal(result[3], 20) + a.deep_equal( + array.filter({ 15, 10, 5, 3, 20 }, function(value) + return value >= 10 + end), + { 15, 10, 20 } + ) end) test('max should return the biggest value from a table', function(a) @@ -117,35 +85,39 @@ test('min should return nil when table is empty', function(a) end) test('reduce should return 90', function(a) - local result = array.reduce({ 20, 30, 40 }, function(memo, value) - return memo + value - end) - - a.equal(result, 90) + a.equal( + array.reduce({ 20, 30, 40 }, function(memo, value) + return memo + value + end), + 90 + ) end) test('reduce should return 100', function(a) - local result = array.reduce({ 20, 30, 40 }, function(memo, value) - return memo + value - end, 10) - - a.equal(result, 100) + a.equal( + array.reduce({ 20, 30, 40 }, function(memo, value) + return memo + value + end, 10), + 100) end) test('reduce should return first item', function(a) - local result = array.reduce({ 20 }, function(memo, value) - return memo + value - end) + local result = - a.equal(result, 20) + a.equal( + array.reduce({ 20 }, function(memo, value) + return memo + value + end), + 20) end) test('reduce should concatenate all items', function(a) - local result = array.reduce({ 'a', 'b', 'c', 'd', 'e' }, function(memo, value) - return memo .. value - end) - - a.equal(result, 'abcde') + a.equal( + array.reduce({ 'a', 'b', 'c', 'd', 'e' }, function(memo, value) + return memo .. value + end), + 'abcde' + ) end) test('reduce_right', function(a) @@ -256,17 +228,10 @@ test('shallow_copy', function(a) { 'b' } } - local result = array.shallow_copy(obj) - - a.equal(#result, #obj) - a.equal(result[1][1], obj[1][1]) - a.equal(result[2][1], obj[2][1]) - - obj[1][1] = 'c' - a.equal(result[1][1], obj[1][1]) - - obj[1] = 'c' - a.equal(type(result[1]), 'table') + a.deep_equal( + array.shallow_copy(obj), + obj + ) end) test('deep_copy', function(a) @@ -298,28 +263,22 @@ test('diff', function(a) end) test('flat', function(a) - local obj = { 'a', 'b', 'c', { 'd', 'e', 'f', { 'g', 'h' } }, { 'i' }, 'j' } - local result = array.flat(obj) - - a.equal(#result, 10) + a.deep_equal( + array.flat({ 'a', 'b', 'c', { 'd', 'e', 'f', { 'g', 'h' } }, { 'i' }, 'j' }), + { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' } + ) end) test('fill', 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) + local value = 'Lua' + local result = array.fill(value, 3, 4) a.equal(result[3], value) a.equal(result[4], value) - local size = 3 - local result2 = array.fill(value, size) - a.equal(#result2, size) - a.equal(result2[1], value) - a.equal(result2[2], value) - a.equal(result2[3], value) + a.deep_equal( + array.fill(value, 3), + { value, value, value } + ) end) test('remove', function(a) @@ -349,9 +308,11 @@ end) test('intersect', function(a) local first_list = { 'a', 'b', 'a', 'c', 'e', 'f', 'a' } local second_list = { 'b', 'a', 'd', 'a' } - local result = array.intersect(first_list, second_list) - a.equal(#result, 3) + a.equal( + #array.intersect(first_list, second_list), + 3 + ) end) test('from_pairs', function(a) From c1bc8db6492fe1b5258be82f26dc0732ad51d7f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Sun, 7 Jun 2020 23:18:36 +0200 Subject: [PATCH 20/65] add unit tests to reverse_each and each --- src/array.lua | 4 ++-- test.lua | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/array.lua b/src/array.lua index 6bcd69a..390cf61 100644 --- a/src/array.lua +++ b/src/array.lua @@ -542,8 +542,8 @@ array = { end, reverse_each = function(obj, callback) - for i = #arr, 1, -1 do - callback(arr[i], i) + for i = #obj, 1, -1 do + callback(obj[i], i) end end } diff --git a/test.lua b/test.lua index 85f24b4..7cc50cb 100644 --- a/test.lua +++ b/test.lua @@ -333,3 +333,30 @@ test('includes', function(a) array.includes(list, 'd') ) end) + +test('each', function(a) + local list = { 'a', 'b', 'c' } + local values = {} + local keys = {} + + array.each(list, function(v, k) + table.insert(values, v) + table.insert(keys, k) + end) + + a.deep_equal(list, values) + a.deep_equal(keys, { 1, 2, 3 }) +end) + +test('reverse_each', function(a) + local values = {} + local keys = {} + + array.reverse_each({ 'a', 'b', 'c' }, function(v, k) + table.insert(values, v) + table.insert(keys, k) + end) + + a.deep_equal(values, { 'c', 'b', 'a' }) + a.deep_equal(keys, { 3, 2, 1 }) +end) From 45f96b0626ca6e961f3507910b22916f2e048dec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Mon, 8 Jun 2020 19:54:57 +0200 Subject: [PATCH 21/65] update readme --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 5fd19c7..8aa540e 100644 --- a/README.md +++ b/README.md @@ -99,3 +99,9 @@ Returns a new table with the values that exist in both tables * array.from_pairs(object:table, object:table):table
Returns a table composed from key-value pairs + +* array.each(object:table, callback:function):void
+Executes `callback` once for each table element + +* array.reverse_each(object:table, callback:function):void
+Executes `callback` once for each table element in reverse order From 23f71ebbb6ca4b0e7a9f2c6cbd753d1e0dfbf341 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Mon, 8 Jun 2020 20:12:06 +0200 Subject: [PATCH 22/65] update structure --- .gitignore | 1 + Makefile | 2 +- src/{array.lua => array/init.lua} | 2 +- src/{ => array}/utils.lua | 0 test.lua | 4 +--- 5 files changed, 4 insertions(+), 5 deletions(-) rename src/{array.lua => array/init.lua} (99%) rename src/{ => array}/utils.lua (100%) diff --git a/.gitignore b/.gitignore index 5334d64..bf547dc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.rock +*.rockspec .luarocks_key diff --git a/Makefile b/Makefile index 12ee6be..e8ddd6b 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ install_dependencies: luarocks install simple_test test: - lua test.lua + LUA_PATH="./src/?.lua;./src/?/init.lua;./src/array/?.lua;;" lua test.lua rockspec: bash $(task_folder)create_rockspec.sh $(version) diff --git a/src/array.lua b/src/array/init.lua similarity index 99% rename from src/array.lua rename to src/array/init.lua index 390cf61..3f34e10 100644 --- a/src/array.lua +++ b/src/array/init.lua @@ -1,4 +1,4 @@ -local utils = require('./utils') +local utils = require('array.utils') local array diff --git a/src/utils.lua b/src/array/utils.lua similarity index 100% rename from src/utils.lua rename to src/array/utils.lua diff --git a/test.lua b/test.lua index 7cc50cb..2bdcbae 100644 --- a/test.lua +++ b/test.lua @@ -1,7 +1,5 @@ -package.path = "./src/?.lua;" .. package.path - -local array = require './src/array' local test = require 'simple_test' +local array = require 'array' test('meta infos', function(a) a.equal(array.__VERSION, '1.3.1') From 4dcf625fb3e3477318562aafb2dd971eaa6ab626 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Mon, 8 Jun 2020 20:13:22 +0200 Subject: [PATCH 23/65] update default_rockspec --- tasks/default_rockspec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tasks/default_rockspec b/tasks/default_rockspec index 1df7576..46e57fa 100644 --- a/tasks/default_rockspec +++ b/tasks/default_rockspec @@ -20,7 +20,7 @@ dependencies = { build = { type = "builtin", modules = { - ["array"] = "src/array.lua", - ["array.utils"] = "src/utils.lua" + ["array"] = "src/array/init.lua", + ["array.utils"] = "src/array/utils.lua" } } From 8d41d7c2a079b1161a0cc523b06d4f6f41348eb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Mon, 8 Jun 2020 20:17:37 +0200 Subject: [PATCH 24/65] update .travis.yml --- .travis.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index ce49346..75d3ce0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,11 +12,10 @@ before_install: - export PATH=$PATH:$PWD/lua_install/bin install: - - luarocks install luautf8 - luarocks install simple_test script: - - lua ./test.lua + - make test notifications: email: From 9ffc18a49a923689a2bd1978609624b5f5d63152 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Wed, 10 Jun 2020 21:13:55 +0200 Subject: [PATCH 25/65] add comments to each and reverse_each --- src/array/init.lua | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/array/init.lua b/src/array/init.lua index 3f34e10..7f9d822 100644 --- a/src/array/init.lua +++ b/src/array/init.lua @@ -535,12 +535,20 @@ array = { return output end, + -- Executes callback once for each table element + -- @param obj {table} + -- @param callback {function} + -- @return {void} each = function(obj, callback) for i=1, #obj do callback(obj[i], i) end end, + -- Executes callback once for each table element in reverse order + -- @param obj {table} + -- @param callback {function} + -- @return {void} reverse_each = function(obj, callback) for i = #obj, 1, -1 do callback(obj[i], i) From 185580201a7891268efa7d2e4f99779dbab55053 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Mon, 15 Jun 2020 21:09:01 +0200 Subject: [PATCH 26/65] update readme --- README.md | 100 +++--------------------------------------------------- 1 file changed, 4 insertions(+), 96 deletions(-) diff --git a/README.md b/README.md index 8aa540e..b4aa699 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # array.lua -A small library with useful methods to handle Lua's table when it's working like an Array +A small library with useful methods to handle Lua's table when it's working like an Array. +For docs, see: http://evandrolg.github.io/array.lua/ [![Build Status](https://travis-ci.org/EvandroLG/array.lua.svg?branch=master)](https://travis-ci.org/EvandroLG/array.lua) @@ -9,99 +10,6 @@ To install array, run: ```sh $ luarocks install array ``` -Or simply copy the array.lua file and paste in your project. -## Methods -* array.is_array(object:table):boolean
-Checks if table is an Array - -* array.is_empty(object:table):boolean
-Checks if table is empty - -* array.slice(object:table, start:number, end:number):table
-Returns a shallow copy of a portion of a table into a new table - -* array.index_of(object:table, value:*):number
-Returns the index at which value can be found or -1 if value is not present - -* array.reverse(object:table):table
-Creates a new table with reverse values - -* array.first(object:table):*
-Returns the first value in a table - -* array.last(object:table):*
-Returns the last value in a table - -* array.max(object:table):*
-Returns the maximum value in a table - -* array.min(object:table):*
-Returns the minimum value in a table - -* array.map(object:table, callback:function):table
-Creates a new table of values by mapping each value in list through a transformation function - -* array.filter(object:table, callback:function):table
-Produces a new table containing all elements that pass truth test - -* array.reduce(object:table, callback:function [, memo]):*
-Applies a function against an accumulator and each value of the table to reduce it to a single value - -* array.reduce_right(object:table, callback:function [, memo]):*
-Works like `reduce` except that it iterates over table's elements from right to left - -* array.sum(object:table):number
-Returns the sum of the values of the table passed by parameter - -* array.concat(object:table, object:table):table
-Returns a new table by joining all values from the two tables - -* array.uniq(object:table):table
-Returns a new table by removing duplicates values - -* array.without(object:table, object:table):table
-Returns a copy of the table with all instances of the values removed - -* array.some(object:table, callback:function):boolean
-Tests whether at least one element in the table passes the test implemented by the callback - -* array.every(object:table, callback:function):boolean
-Tests whether all elements in the table passes the test implemented by the callback - -* array.zip(object:table, object:table):table
-Returns a table of the two supplied by pairing up equally-positioned elements from both tables - -* array.shallow_copy(object:table):table
-Returns a shallow copy of the table passed as parameter - -* array.deep_copy(object:table):table
-Returns a deep copy of the table passed as parameter - -* array.diff(object:table, object:table):table
-Returns a new table with the items which exist only in the first table - -* array.flat(object:table):table
-Creates a new table with the sub-table elements concatenated into it - -* array.fill(value:*, [start:number], end:number):table
-Creates a table filling all the elements from a start index (default -one) to an end index with a default value passed by parameter. - -* array.remove(object:table, callback:function):table
-Removes all elements from table that `callback` returns truthy for and returns a new table with the removed elements - -* array.counter(object:table):table
-Returns a new table in hash structure, where keys represent each array value - -* array.intersect(object:table, object:table):table
-Returns a new table with the values that exist in both tables - -* array.from_pairs(object:table, object:table):table
-Returns a table composed from key-value pairs - -* array.each(object:table, callback:function):void
-Executes `callback` once for each table element - -* array.reverse_each(object:table, callback:function):void
-Executes `callback` once for each table element in reverse order +## License +[MIT](https://github.com/EvandroLG/audio.lua/tree/master/LICENSE) From 8e3950a552401f85a429906f68bea3db62594035 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Mon, 15 Jun 2020 21:16:40 +0200 Subject: [PATCH 27/65] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b4aa699..3b15fb7 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # array.lua -A small library with useful methods to handle Lua's table when it's working like an Array. +A small library with useful methods to handle Lua's table when it's working like an Array.
For docs, see: http://evandrolg.github.io/array.lua/ [![Build From 87a2629494c870079c6ae219b3db1d3b9ae045c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Mon, 15 Jun 2020 21:17:32 +0200 Subject: [PATCH 28/65] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3b15fb7..dd33088 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # array.lua A small library with useful methods to handle Lua's table when it's working like an Array.
-For docs, see: http://evandrolg.github.io/array.lua/ +For docs, see: https://evandrolg.github.io/array.lua/ [![Build Status](https://travis-ci.org/EvandroLG/array.lua.svg?branch=master)](https://travis-ci.org/EvandroLG/array.lua) From 80ea2849d5d258a6469376f0849723d567cc1532 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Sun, 21 Jun 2020 22:38:05 +0200 Subject: [PATCH 29/65] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index dd33088..47b1604 100644 --- a/README.md +++ b/README.md @@ -12,4 +12,4 @@ $ luarocks install array ``` ## License -[MIT](https://github.com/EvandroLG/audio.lua/tree/master/LICENSE) +[MIT](https://github.com/EvandroLG/array.lua/tree/master/LICENSE) From 00d485ef0872384373412016cb3ca66920dbe6f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Paulo?= Date: Mon, 3 Aug 2020 08:38:20 -0300 Subject: [PATCH 30/65] fix function documentation for every --- src/array/init.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/array/init.lua b/src/array/init.lua index 7f9d822..2f4c088 100644 --- a/src/array/init.lua +++ b/src/array/init.lua @@ -348,10 +348,10 @@ array = { return output end, - -- Return a table of the two supplied by pairing up equally-positioned elements from both tables + -- Run a predicate on each value. If the predicate evaluates to any false value, this function will immediately return false; otherwise, it returns true. -- @param obj {table} - -- @param obj2 {table} - -- @return {table} + -- @param predicate {callback} + -- @return {boolean} every = function(obj, callback) utils.raises_error(array, obj, 'every') From 337c5c4657c42dfe2fb2191489f6134b6fb2dfe8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Mon, 3 Aug 2020 21:58:53 +0200 Subject: [PATCH 31/65] improve comment to every method --- src/array/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/array/init.lua b/src/array/init.lua index 2f4c088..0687c86 100644 --- a/src/array/init.lua +++ b/src/array/init.lua @@ -350,7 +350,7 @@ array = { -- Run a predicate on each value. If the predicate evaluates to any false value, this function will immediately return false; otherwise, it returns true. -- @param obj {table} - -- @param predicate {callback} + -- @param callback {function} -- @return {boolean} every = function(obj, callback) utils.raises_error(array, obj, 'every') From cdd212c0e8de3fa78e6e07381df687353511be4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Fri, 9 Oct 2020 23:27:36 +0200 Subject: [PATCH 32/65] create group_by method --- src/array/init.lua | 17 +++++++++++++++++ test.lua | 20 ++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/array/init.lua b/src/array/init.lua index 0687c86..68eb8b5 100644 --- a/src/array/init.lua +++ b/src/array/init.lua @@ -553,6 +553,23 @@ array = { for i = #obj, 1, -1 do callback(obj[i], i) end + end, + + group_by = function(obj, callback) + utils.raises_error(array, obj, 'group_by') + + function reducer(accumulator, current) + local result = callback(current) + if not accumulator[result] then + accumulator[result] = {} + end + + table.insert(accumulator[result], current) + + return accumulator + end + + return array.reduce(obj, reducer, {}) end } diff --git a/test.lua b/test.lua index 2bdcbae..b359cbd 100644 --- a/test.lua +++ b/test.lua @@ -358,3 +358,23 @@ test('reverse_each', function(a) a.deep_equal(values, { 'c', 'b', 'a' }) a.deep_equal(keys, { 3, 2, 1 }) end) + +test('group_by', function(a) + a.deep_equal( + array.group_by({ 6.1, 4.1, 6.3, 4.4, 5.1 }, function(item) return math.floor(item) end), + { + [4] = { 4.1, 4.4 }, + [6] = { 6.1, 6.3 }, + [5] = { 5.1 } + } + ) + + a.deep_equal( + array.group_by({ 'one', 'two', 'three', 'four' }, function(item) return #item end), + { + [3] = { 'one', 'two' }, + [5] = { 'three' }, + [4] = { 'four' } + } + ) +end) From adcd5b21d71e042ef8f8b82becd72f282d2810af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Fri, 9 Oct 2020 23:30:28 +0200 Subject: [PATCH 33/65] add comments doc comment to group_by method --- src/array/init.lua | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/array/init.lua b/src/array/init.lua index 68eb8b5..d794e63 100644 --- a/src/array/init.lua +++ b/src/array/init.lua @@ -555,6 +555,10 @@ array = { end end, + -- Returns a new table composed by keys created from the results of running each element through `callback` + -- @param obj {table} + -- @param callback {function} + -- @return {table} group_by = function(obj, callback) utils.raises_error(array, obj, 'group_by') From e9a3e13bc85bd9031a1eba7460e9fdbfb762ebbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Sat, 10 Oct 2020 13:00:24 +0200 Subject: [PATCH 34/65] add random function --- src/array/init.lua | 8 ++++++++ test.lua | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/array/init.lua b/src/array/init.lua index d794e63..2fa29c1 100644 --- a/src/array/init.lua +++ b/src/array/init.lua @@ -574,6 +574,14 @@ array = { end return array.reduce(obj, reducer, {}) + end, + + -- Returns a value from a random key of the given array + -- @param obj {table} + -- @return {*} + random = function(obj) + utils.raises_error(array, obj, 'random') + return obj[math.random(#obj)] end } diff --git a/test.lua b/test.lua index b359cbd..7c8f433 100644 --- a/test.lua +++ b/test.lua @@ -378,3 +378,13 @@ test('group_by', function(a) } ) end) + +test('random', function(a) + local list = { 'a', 'b', 'c' } + + a.ok( + array.includes(list, array.random(list)) + ) + + a.equal(array.random({ 'a' }), 'a') +end) From f72cba3212dfdae4d3b7e6b4bfd3fa1200c754fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Sat, 10 Oct 2020 13:02:38 +0200 Subject: [PATCH 35/65] update unit tests --- test.lua | 36 +++++------------------------------- 1 file changed, 5 insertions(+), 31 deletions(-) diff --git a/test.lua b/test.lua index 7c8f433..410524f 100644 --- a/test.lua +++ b/test.lua @@ -48,7 +48,7 @@ test('reverse', function(a) ) end) -test('map should return a table with 2, 4, 6 values', function(a) +test('map', function(a) a.deep_equal( array.map({ 1, 2, 3 }, function(value) return value * 2 @@ -57,7 +57,7 @@ test('map should return a table with 2, 4, 6 values', function(a) ) end) -test('filter should return a table with 10, 15, 20 values', function(a) +test('filter', function(a) a.deep_equal( array.filter({ 15, 10, 5, 3, 20 }, function(value) return value >= 10 @@ -66,50 +66,24 @@ test('filter should return a table with 10, 15, 20 values', function(a) ) end) -test('max should return the biggest value from a table', function(a) +test('max', function(a) a.equal(array.max({ 20, 22, 1, 3, 30, 42 }), 42) -end) - -test('max should return nil when table is empty', function(a) a.equal(array.max({}), nil) end) -test('min should return the smallest value from a table', function(a) +test('min', function(a) a.equal(array.min({ 20, 22, 1, 3, 30, 42 }), 1) -end) - -test('min should return nil when table is empty', function(a) a.equal(array.min({}), nil) end) -test('reduce should return 90', function(a) +test('reduce', function(a) a.equal( array.reduce({ 20, 30, 40 }, function(memo, value) return memo + value end), 90 ) -end) - -test('reduce should return 100', function(a) - a.equal( - array.reduce({ 20, 30, 40 }, function(memo, value) - return memo + value - end, 10), - 100) -end) - -test('reduce should return first item', function(a) - local result = - - a.equal( - array.reduce({ 20 }, function(memo, value) - return memo + value - end), - 20) -end) -test('reduce should concatenate all items', function(a) a.equal( array.reduce({ 'a', 'b', 'c', 'd', 'e' }, function(memo, value) return memo .. value From 365a3d23e3a3fa680b3af4c60d122d3d4a5480d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Sun, 11 Oct 2020 13:39:02 +0200 Subject: [PATCH 36/65] improve concat function to receive a variable number of arguments --- src/array/init.lua | 23 +++++++++-------------- test.lua | 5 +++++ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/array/init.lua b/src/array/init.lua index 2fa29c1..94b5b7f 100644 --- a/src/array/init.lua +++ b/src/array/init.lua @@ -254,22 +254,17 @@ array = { end) end, - -- Return a new table joining all values from the two tables passed by parameter - -- @param obj {table} - -- @param obj2 {table} + -- Return a new table joining all values from N tables passed by parameter + -- @param arg {...} -- @return {table} - concat = function(obj, obj2) - utils.raises_error(array, obj, 'concat') - utils.raises_error(array, obj2, 'concat') - + concat = function(...) + local arg = {...} local output = {} - for i=1, #obj do - table.insert(output, obj[i]) - end - - for i=1, #obj2 do - table.insert(output, obj2[i]) + for i, list in ipairs(arg) do + for j=1, #list do + table.insert(output, list[j]) + end end return output @@ -582,7 +577,7 @@ array = { random = function(obj) utils.raises_error(array, obj, 'random') return obj[math.random(#obj)] - end + end, } return array diff --git a/test.lua b/test.lua index 410524f..17a24fe 100644 --- a/test.lua +++ b/test.lua @@ -132,6 +132,11 @@ test('concat', function(a) array.concat({ 1, 2, 3 }, { 4, 5, 6 }), { 1, 2, 3, 4, 5, 6 } ) + + a.deep_equal( + array.concat({ 1, 2, 3 }, { 4, 5 }, { 6, 7 }, { 8, 9 }), + { 1, 2, 3, 4, 5, 6, 7, 8, 9 } + ) end) test('uniq', function(a) From 5f7b2361b8b4824566e283ee8e2bcccb17099d44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Sun, 11 Oct 2020 18:29:15 +0200 Subject: [PATCH 37/65] create logic of permutation function - still with bugs --- src/array/init.lua | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/array/init.lua b/src/array/init.lua index 94b5b7f..2d81657 100644 --- a/src/array/init.lua +++ b/src/array/init.lua @@ -578,6 +578,35 @@ array = { utils.raises_error(array, obj, 'random') return obj[math.random(#obj)] end, + + permutation = function(obj) + if #obj == 1 then + return { obj } + end + + local output = {} + local partialList = array.permutation(array.slice(obj, 2)) + local first = { obj[1] } + + for i=1, #partialList do + local partial = partialList[i] + + for j=1, #partial+1 do + local merged = array.concat( + array.slice(partial, 1, j), + first, + array.slice(partial, j) + ) + + table.insert( + output, + merged + ) + end + end + + return output + end, } return array From 5993210045cb5661ed31f244dab10aa8a609a195 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Sun, 11 Oct 2020 20:51:28 +0200 Subject: [PATCH 38/65] fix bug in slice function --- src/array/init.lua | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/array/init.lua b/src/array/init.lua index 2d81657..0549ccb 100644 --- a/src/array/init.lua +++ b/src/array/init.lua @@ -40,14 +40,14 @@ array = { return true end, - -- Check if parameter is an empty table + -- Checks if parameter is an empty table -- @param obj {table} -- @return {boolean} is_empty = function(obj) return array.is_array(obj) and #obj == 0 end, - -- Return a shallow copy of a portion of a table into a new table + -- Returns a shallow copy of a portion of a table into a new table -- @param obj {table} -- @param start {number} start value -- @param finish {number} end value @@ -55,19 +55,17 @@ array = { slice = function(obj, start, finish) utils.raises_error(array, obj, 'slice') - if array.is_empty(obj) then return {} end - - local _start = start or 1 + if array.is_empty(obj) or start == finish then return {} end local output = {} - for i=_start, finish or #obj do + for i = (start or 1), (finish or #obj) do table.insert(output, obj[i]) end return output end, - -- Return the index at which value can be found or -1 in case value is not present + -- Returns the index at which value can be found or -1 in case value is not present -- @param obj {table} -- @param value {*} -- @return {number} From 40457b238aae5f0cf3f3df488c8ac92946b7cd6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Sun, 11 Oct 2020 21:11:10 +0200 Subject: [PATCH 39/65] add unit tests to permutation method --- src/array/init.lua | 9 ++++++--- test.lua | 10 ++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/array/init.lua b/src/array/init.lua index 0549ccb..077164e 100644 --- a/src/array/init.lua +++ b/src/array/init.lua @@ -577,17 +577,20 @@ array = { return obj[math.random(#obj)] end, + -- Creates a new table returning all permutations of length of the elements of the given table + -- @param obj {table} + -- @return {table} permutation = function(obj) if #obj == 1 then return { obj } end local output = {} - local partialList = array.permutation(array.slice(obj, 2)) + local partial_list = array.permutation(array.slice(obj, 2)) local first = { obj[1] } - for i=1, #partialList do - local partial = partialList[i] + for i=1, #partial_list do + local partial = partial_list[i] for j=1, #partial+1 do local merged = array.concat( diff --git a/test.lua b/test.lua index 17a24fe..593c3a9 100644 --- a/test.lua +++ b/test.lua @@ -367,3 +367,13 @@ test('random', function(a) a.equal(array.random({ 'a' }), 'a') end) + +test('permutation', function(a) + a.deep_equal( + array.permutation({ 'lua', 'javascript' }), + { + { 'lua', 'javascript' }, + { 'javascript', 'lua' }, + } + ) +end) From 07cec32d33534b15d6324bc422af6e26899bc520 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Sun, 11 Oct 2020 21:11:34 +0200 Subject: [PATCH 40/65] create a simple util function to print items from table --- src/array/utils.lua | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/array/utils.lua b/src/array/utils.lua index b3ab4e4..206f2b9 100644 --- a/src/array/utils.lua +++ b/src/array/utils.lua @@ -46,5 +46,11 @@ return { end return output + end, + + print_items = function(obj) + for i=1, #obj do + print(obj[i]) + end end } From 9746c382167a15b87534d6d1ff610583436fcf51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Tue, 13 Oct 2020 22:53:41 +0200 Subject: [PATCH 41/65] fix issues with the permutation method --- src/array/init.lua | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/src/array/init.lua b/src/array/init.lua index 077164e..1b38882 100644 --- a/src/array/init.lua +++ b/src/array/init.lua @@ -56,9 +56,10 @@ array = { utils.raises_error(array, obj, 'slice') if array.is_empty(obj) or start == finish then return {} end + local _finish = finish or (#obj + 1) local output = {} - for i = (start or 1), (finish or #obj) do + for i = (start or 1), (_finish - 1) do table.insert(output, obj[i]) end @@ -581,33 +582,28 @@ array = { -- @param obj {table} -- @return {table} permutation = function(obj) + local output = {} + if #obj == 1 then return { obj } end - local output = {} - local partial_list = array.permutation(array.slice(obj, 2)) - local first = { obj[1] } + local partial_permutations = array.permutation(array.slice(obj, 2)) + local first = obj[1] - for i=1, #partial_list do - local partial = partial_list[i] + for i=1, #partial_permutations do + local partial = partial_permutations[i] for j=1, #partial+1 do - local merged = array.concat( - array.slice(partial, 1, j), - first, - array.slice(partial, j) - ) + local permutation_front = array.slice(partial, 1, j) + local permutation_after = array.slice(partial, j) - table.insert( - output, - merged - ) + table.insert(output, array.concat(permutation_front, { first }, permutation_after)) end end return output - end, + end } return array From cfbc7eabe046017d70d1c5230c87ceaf25bcdc3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Tue, 13 Oct 2020 22:57:10 +0200 Subject: [PATCH 42/65] update unit tests --- test.lua | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/test.lua b/test.lua index 593c3a9..7028c84 100644 --- a/test.lua +++ b/test.lua @@ -31,7 +31,7 @@ test('slice', function(a) a.equal(#array.slice({}, 1, 2), 0) a.deep_equal( - array.slice({ 'lua', 'javascript', 'python', 'ruby', 'c' }, 2, 4), + array.slice({ 'lua', 'javascript', 'python', 'ruby', 'c' }, 2, 5), { 'javascript', 'python', 'ruby' } ) @@ -133,6 +133,11 @@ test('concat', function(a) { 1, 2, 3, 4, 5, 6 } ) + a.deep_equal( + array.concat({ 'a', 'b', 'c' }, { 'd', 'e' }, { 'f', 'g' }), + { 'a', 'b', 'c', 'd', 'e', 'f', 'g' } + ) + a.deep_equal( array.concat({ 1, 2, 3 }, { 4, 5 }, { 6, 7 }, { 8, 9 }), { 1, 2, 3, 4, 5, 6, 7, 8, 9 } @@ -376,4 +381,16 @@ test('permutation', function(a) { 'javascript', 'lua' }, } ) + + a.deep_equal( + array.permutation({ 'lua', 'javascript', 'ruby' }), + { + { 'lua', 'javascript', 'ruby' }, + { 'javascript', 'lua', 'ruby' }, + { 'javascript', 'ruby', 'lua' }, + { 'lua', 'ruby', 'javascript' }, + { 'ruby', 'lua', 'javascript' }, + { 'ruby', 'javascript', 'lua' }, + } + ) end) From 15276412927adebbb839f9700fd7e42abcf998d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Wed, 14 Oct 2020 21:03:26 +0200 Subject: [PATCH 43/65] update version --- src/array/init.lua | 2 +- test.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/array/init.lua b/src/array/init.lua index 1b38882..155a407 100644 --- a/src/array/init.lua +++ b/src/array/init.lua @@ -3,7 +3,7 @@ local utils = require('array.utils') local array array = { - __VERSION = '1.3.1', + __VERSION = '1.3.2', __DESCRIPTION = "A small library with useful methods to handle Lua's table when it's working like an Array", __LICENSE = [[ The MIT License (MIT) diff --git a/test.lua b/test.lua index 7028c84..2f433af 100644 --- a/test.lua +++ b/test.lua @@ -2,7 +2,7 @@ local test = require 'simple_test' local array = require 'array' test('meta infos', function(a) - a.equal(array.__VERSION, '1.3.1') + a.equal(array.__VERSION, '1.3.2') a.equal(array.__DESCRIPTION, "A small library with useful methods to handle Lua's table when it's working like an Array") end) From b9cce2efa9fb3ab9c6338e0a006744495742512a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Wed, 14 Oct 2020 21:06:15 +0200 Subject: [PATCH 44/65] update version --- src/array/init.lua | 2 +- test.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/array/init.lua b/src/array/init.lua index 155a407..5d424a7 100644 --- a/src/array/init.lua +++ b/src/array/init.lua @@ -3,7 +3,7 @@ local utils = require('array.utils') local array array = { - __VERSION = '1.3.2', + __VERSION = '1.3.3', __DESCRIPTION = "A small library with useful methods to handle Lua's table when it's working like an Array", __LICENSE = [[ The MIT License (MIT) diff --git a/test.lua b/test.lua index 2f433af..e6b911b 100644 --- a/test.lua +++ b/test.lua @@ -2,7 +2,7 @@ local test = require 'simple_test' local array = require 'array' test('meta infos', function(a) - a.equal(array.__VERSION, '1.3.2') + a.equal(array.__VERSION, '1.3.3') a.equal(array.__DESCRIPTION, "A small library with useful methods to handle Lua's table when it's working like an Array") end) From 8562ac878ca5f6dd152567eab796205980794e09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Sat, 7 Nov 2020 00:19:52 +0100 Subject: [PATCH 45/65] create chunk method --- src/array/init.lua | 20 ++++++++++++++++++++ src/array/utils.lua | 10 ++++++++++ test.lua | 11 +++++++++++ 3 files changed, 41 insertions(+) diff --git a/src/array/init.lua b/src/array/init.lua index 5d424a7..4d717a3 100644 --- a/src/array/init.lua +++ b/src/array/init.lua @@ -602,6 +602,26 @@ array = { end end + return output + end, + + -- Returns a new array-like table with elements splitted into groups of length of `size` + -- @param obj {table} + -- @param size {number} + -- @return {table} + chunk = function(obj, size) + local output = {} + + for _, v in pairs(obj) do + local last = output[#output] + + if utils.is_nil(last) or #last == size then + table.insert(output, { v }) + else + table.insert(last, v) + end + end + return output end } diff --git a/src/array/utils.lua b/src/array/utils.lua index 206f2b9..5f1c472 100644 --- a/src/array/utils.lua +++ b/src/array/utils.lua @@ -48,9 +48,19 @@ return { return output end, + -- Loop through table and print all values + -- @obj {table} + -- @returns {nil} print_items = function(obj) for i=1, #obj do print(obj[i]) end + end, + + -- Checks wether value is nil + -- @obj {any} + -- @returns {boolean} + is_nil = function(value) + return value == nil end } diff --git a/test.lua b/test.lua index e6b911b..02e4d3d 100644 --- a/test.lua +++ b/test.lua @@ -394,3 +394,14 @@ test('permutation', function(a) } ) end) + +test('chunk', function(a) + a.deep_equal( + array.chunk({ 'a', 'b', 'c', 'd', 'e', 'f', 'g' }, 3), + { + { 'a', 'b', 'c' }, + { 'd', 'e', 'f' }, + { 'g' }, + } + ) +end) From 7ed1b406979837e4e7b546b3837cded727c97494 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Sat, 5 Dec 2020 19:45:21 +0100 Subject: [PATCH 46/65] update slice logic to handle negative values --- src/array/init.lua | 13 ++++++++++--- test.lua | 12 +++++++++++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/array/init.lua b/src/array/init.lua index 4d717a3..d635d6d 100644 --- a/src/array/init.lua +++ b/src/array/init.lua @@ -3,7 +3,7 @@ local utils = require('array.utils') local array array = { - __VERSION = '1.3.3', + __VERSION = '1.3.4', __DESCRIPTION = "A small library with useful methods to handle Lua's table when it's working like an Array", __LICENSE = [[ The MIT License (MIT) @@ -56,10 +56,17 @@ array = { utils.raises_error(array, obj, 'slice') if array.is_empty(obj) or start == finish then return {} end - local _finish = finish or (#obj + 1) local output = {} - for i = (start or 1), (_finish - 1) do + local _finish = #obj + + if (finish and finish >= 0) then + _finish = finish - 1 + elseif finish and finish < 0 then + _finish = #obj + finish + end + + for i = (start or 1), _finish do table.insert(output, obj[i]) end diff --git a/test.lua b/test.lua index 02e4d3d..269615d 100644 --- a/test.lua +++ b/test.lua @@ -2,7 +2,7 @@ local test = require 'simple_test' local array = require 'array' test('meta infos', function(a) - a.equal(array.__VERSION, '1.3.3') + a.equal(array.__VERSION, '1.3.4') a.equal(array.__DESCRIPTION, "A small library with useful methods to handle Lua's table when it's working like an Array") end) @@ -39,6 +39,16 @@ test('slice', function(a) array.slice({ 'lua', 'javascript', 'python', 'ruby', 'c' }, 2), { 'javascript', 'python', 'ruby', 'c' } ) + + --a.deep_equal( + --array.slice({ 'lua', 'javascript', 'python', 'ruby', 'c' }, -2), + --{ 'ruby', 'c' } + --) + + a.deep_equal( + array.slice({ 'lua', 'javascript', 'python', 'ruby', 'c' }, 2, -2), + { 'javascript', 'python' } + ) end) test('reverse', function(a) From 888240cfb7622069d844f1a227207814701c05bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Sat, 5 Dec 2020 20:51:53 +0100 Subject: [PATCH 47/65] improve slice logic --- src/array/init.lua | 9 ++++++++- test.lua | 8 ++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/array/init.lua b/src/array/init.lua index d635d6d..0c843f8 100644 --- a/src/array/init.lua +++ b/src/array/init.lua @@ -59,6 +59,13 @@ array = { local output = {} local _finish = #obj + local _start = 1 + + if start >= 0 then + _start = start + elseif utils.is_nil(finish) and start < 0 then + _start = #obj + start + 1 + end if (finish and finish >= 0) then _finish = finish - 1 @@ -66,7 +73,7 @@ array = { _finish = #obj + finish end - for i = (start or 1), _finish do + for i = _start, _finish do table.insert(output, obj[i]) end diff --git a/test.lua b/test.lua index 269615d..03f2c55 100644 --- a/test.lua +++ b/test.lua @@ -40,10 +40,10 @@ test('slice', function(a) { 'javascript', 'python', 'ruby', 'c' } ) - --a.deep_equal( - --array.slice({ 'lua', 'javascript', 'python', 'ruby', 'c' }, -2), - --{ 'ruby', 'c' } - --) + a.deep_equal( + array.slice({ 'lua', 'javascript', 'python', 'ruby', 'c' }, -2), + { 'ruby', 'c' } + ) a.deep_equal( array.slice({ 'lua', 'javascript', 'python', 'ruby', 'c' }, 2, -2), From 49909f1976ed469c0bb40218ccd8cbf2248a8de2 Mon Sep 17 00:00:00 2001 From: Evandro Leopoldino Goncalves Date: Mon, 25 Dec 2023 21:42:20 +0100 Subject: [PATCH 48/65] updates the flat function, adding support to depth --- src/array/init.lua | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/array/init.lua b/src/array/init.lua index 0c843f8..174ba0d 100644 --- a/src/array/init.lua +++ b/src/array/init.lua @@ -426,15 +426,19 @@ array = { return output end, - -- Create a new table with the sub-table elements concatenated into it - -- @param obj {table} + -- Create a new table with the sub-table elements concatenated into it up to the specific depth + -- @param obj {table, depth} -- @return {table} - flat = function(obj) + flat = function(obj, depth) + if depth == nil then + depth = 1 / 0 -- Infinity + end + return array.reduce(obj, function(acc, item) - if array.is_array(item) then + if array.is_array(item) and depth > 0 then return array.concat( acc, - array.flat(item) + array.flat(item, depth - 1) ) else table.insert(acc, item) From 6ccdccf77395e636dd2eda16b51cc160a059dca1 Mon Sep 17 00:00:00 2001 From: Evandro Leopoldino Goncalves Date: Mon, 25 Dec 2023 21:42:38 +0100 Subject: [PATCH 49/65] updates the flat unit tests --- test.lua | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test.lua b/test.lua index 03f2c55..8cb7427 100644 --- a/test.lua +++ b/test.lua @@ -259,6 +259,16 @@ test('flat', function(a) array.flat({ 'a', 'b', 'c', { 'd', 'e', 'f', { 'g', 'h' } }, { 'i' }, 'j' }), { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' } ) + + a.deep_equal( + array.flat({ 'a', 'b', 'c', { 'd', 'e', 'f', { 'g', 'h' } }, { 'i' }, 'j' }, 1), + { 'a', 'b', 'c', 'd', 'e', 'f', { 'g', 'h' }, 'i', 'j' } + ) + + a.deep_equal( + array.flat({ 'a', 'b', 'c', { 'd', 'e', { 'f', { 'g', 'h' } } }, { 'i' }, 'j' }, 2), + { 'a', 'b', 'c', 'd', 'e', 'f', { 'g', 'h' }, 'i', 'j' } + ) end) test('fill', function(a) From 46befe373a8c151a6a59ec791e700a3ec54f9936 Mon Sep 17 00:00:00 2001 From: Evandro Leopoldino Goncalves Date: Mon, 25 Dec 2023 21:44:49 +0100 Subject: [PATCH 50/65] updates version --- src/array/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/array/init.lua b/src/array/init.lua index 174ba0d..6517fa9 100644 --- a/src/array/init.lua +++ b/src/array/init.lua @@ -3,7 +3,7 @@ local utils = require('array.utils') local array array = { - __VERSION = '1.3.4', + __VERSION = '1.3.5', __DESCRIPTION = "A small library with useful methods to handle Lua's table when it's working like an Array", __LICENSE = [[ The MIT License (MIT) From 7e29431f9ba4b4bffe9df3f548caaacaf38c66f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Sat, 6 Jan 2024 22:55:47 +0100 Subject: [PATCH 51/65] Create main.yml --- .github/workflows/main.yml | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 .github/workflows/main.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..b5ac5ec --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,27 @@ +name: CI + +on: + push: + branches: + - master + pull_request: + branches: + - master + +jobs: + test: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Install Lua + run: sudo apt-get install lua5.3 + + - name: Install dependencies + run: make install_dependencies + + - name: Run unit tests + run: | + make test From 7843521e986b1d4e015057378b40a93779fd907d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Sat, 6 Jan 2024 23:00:53 +0100 Subject: [PATCH 52/65] Update main.yml --- .github/workflows/main.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b5ac5ec..dcbbd9c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -19,6 +19,9 @@ jobs: - name: Install Lua run: sudo apt-get install lua5.3 + - name: Install Luarocks + run: sudo apt-get install luarocks + - name: Install dependencies run: make install_dependencies From 60bc4a59c4c95343bad3a1d71833839159cc64b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Sat, 6 Jan 2024 23:04:02 +0100 Subject: [PATCH 53/65] Update main.yml --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index dcbbd9c..2f79483 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -23,7 +23,7 @@ jobs: run: sudo apt-get install luarocks - name: Install dependencies - run: make install_dependencies + run: sudo make install_dependencies - name: Run unit tests run: | From 5d409d6f8863f9b20c1a5c3c4116d5b3dc41811b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Sat, 6 Jan 2024 23:07:11 +0100 Subject: [PATCH 54/65] Update main.yml --- .github/workflows/main.yml | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 2f79483..df16384 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -13,18 +13,26 @@ jobs: runs-on: ubuntu-latest steps: - - name: Checkout code - uses: actions/checkout@v2 + - name: Checkout code + uses: actions/checkout@v2 - - name: Install Lua - run: sudo apt-get install lua5.3 + - name: Install Lua + run: sudo apt-get install lua5.3 - - name: Install Luarocks - run: sudo apt-get install luarocks + - name: Install Lua development files + run: sudo apt-get install liblua5.3-dev - - name: Install dependencies - run: sudo make install_dependencies + - name: Install Luarocks + run: | + sudo apt-get install luarocks - - name: Run unit tests - run: | - make test + - name: Configure Lua header files for Luarocks + run: | + sudo luarocks config --lua-version=5.3 --with-lua-include=/usr/include/lua5.3 + + - name: Install dependencies + run: sudo make install_dependencies + + - name: Run unit tests + run: | + make test From ce278d58f02fb43bfe54b7918594034974915241 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Sat, 6 Jan 2024 23:09:03 +0100 Subject: [PATCH 55/65] Update main.yml --- .github/workflows/main.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index df16384..616fe0b 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -28,7 +28,11 @@ jobs: - name: Configure Lua header files for Luarocks run: | - sudo luarocks config --lua-version=5.3 --with-lua-include=/usr/include/lua5.3 + export LUA_PATH=/usr/include/lua5.3 + export LUA_DIR=/usr/include/lua5.3 + sudo luarocks config variables.LUA_INCDIR /usr/include/lua5.3 + sudo luarocks config variables.LUA_LIBDIR /usr/lib/x86_64-linux-gnu + sudo luarocks config variables.LUA_BINDIR /usr/bin - name: Install dependencies run: sudo make install_dependencies From 0718b1b7c5f5ab1b5963bebaaa0d6c938f57b9c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Sat, 6 Jan 2024 23:12:00 +0100 Subject: [PATCH 56/65] Update main.yml --- .github/workflows/main.yml | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 616fe0b..66d05be 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,29 +10,22 @@ on: jobs: test: - runs-on: ubuntu-latest + runs-on: ubuntu-20.04 - steps: - - name: Checkout code - uses: actions/checkout@v2 + strategy: + fail-fast: false + matrix: + luaVersion: ["5.3"] - - name: Install Lua - run: sudo apt-get install lua5.3 + steps: + - name: Checkout + uses: actions/checkout@v3 - - name: Install Lua development files - run: sudo apt-get install liblua5.3-dev + - uses: leafo/gh-actions-lua@v8 + with: + luaVersion: ${{ matrix.luaVersion }} - - name: Install Luarocks - run: | - sudo apt-get install luarocks - - - name: Configure Lua header files for Luarocks - run: | - export LUA_PATH=/usr/include/lua5.3 - export LUA_DIR=/usr/include/lua5.3 - sudo luarocks config variables.LUA_INCDIR /usr/include/lua5.3 - sudo luarocks config variables.LUA_LIBDIR /usr/lib/x86_64-linux-gnu - sudo luarocks config variables.LUA_BINDIR /usr/bin + - uses: leafo/gh-actions-luarocks@v4 - name: Install dependencies run: sudo make install_dependencies From 11b33fcab7d9e594f50e174b1f12c7cc7be8f7c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Sat, 6 Jan 2024 23:19:15 +0100 Subject: [PATCH 57/65] Update main.yml --- .github/workflows/main.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 66d05be..424d804 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -21,14 +21,14 @@ jobs: - name: Checkout uses: actions/checkout@v3 - - uses: leafo/gh-actions-lua@v8 + - uses: leafo/gh-actions-lua@v9 with: luaVersion: ${{ matrix.luaVersion }} - uses: leafo/gh-actions-luarocks@v4 - name: Install dependencies - run: sudo make install_dependencies + run: luarocks install simple_test - name: Run unit tests run: | From 4e628e75bad2f760728244d7591308c0de521cea Mon Sep 17 00:00:00 2001 From: Evandro Leopoldino Goncalves Date: Sat, 6 Jan 2024 23:20:44 +0100 Subject: [PATCH 58/65] updates unit test --- test.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test.lua b/test.lua index 8cb7427..5dd358f 100644 --- a/test.lua +++ b/test.lua @@ -2,7 +2,7 @@ local test = require 'simple_test' local array = require 'array' test('meta infos', function(a) - a.equal(array.__VERSION, '1.3.4') + a.equal(array.__VERSION, '1.3.5') a.equal(array.__DESCRIPTION, "A small library with useful methods to handle Lua's table when it's working like an Array") end) From 783f71b8e0694a744089ffab61b1d4617ac47360 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Sat, 6 Jan 2024 23:21:47 +0100 Subject: [PATCH 59/65] Update main.yml --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 424d804..3a48f01 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -32,4 +32,4 @@ jobs: - name: Run unit tests run: | - make test + LUA_PATH="./src/?.lua;./src/?/init.lua;./src/array/?.lua;;" lua test.lua From 24e27c57efcfe094e17a8d5be67e141e8d947e22 Mon Sep 17 00:00:00 2001 From: Evandro Leopoldino Goncalves Date: Wed, 10 Jan 2024 22:12:55 +0100 Subject: [PATCH 60/65] creates a flat_map function --- src/array/init.lua | 11 +++++++++++ test.lua | 9 +++++++++ 2 files changed, 20 insertions(+) diff --git a/src/array/init.lua b/src/array/init.lua index 6517fa9..c4774bc 100644 --- a/src/array/init.lua +++ b/src/array/init.lua @@ -641,6 +641,17 @@ array = { end return output + end, + + -- Returns a new array-like table by applying a given callback to each element of the table, and then flattening the result by one level + -- @param obj {table} + -- @param callback {function} + -- @return {table} + flat_map = function(obj, callback) + utils.raises_error(array, obj, 'flat_map') + local mapped = array.map(obj, callback) + + return array.flat(mapped) end } diff --git a/test.lua b/test.lua index 5dd358f..79dce65 100644 --- a/test.lua +++ b/test.lua @@ -425,3 +425,12 @@ test('chunk', function(a) } ) end) + +test('flat_map', function(a) + a.deep_equal( + array.flat_map({ 1, 2, 3 }, function(value) + return { value, value * 2 } + end), + { 1, 2, 2, 4, 3, 6 } + ) +end) From 241f3bc6d7ebe47a6fb775b6b63f716a2a35ea2f Mon Sep 17 00:00:00 2001 From: Evandro Leopoldino Goncalves Date: Wed, 10 Jan 2024 22:24:22 +0100 Subject: [PATCH 61/65] implements the key_by utility function --- src/array/init.lua | 14 ++++++++++++++ test.lua | 12 ++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/array/init.lua b/src/array/init.lua index c4774bc..020d3db 100644 --- a/src/array/init.lua +++ b/src/array/init.lua @@ -652,6 +652,20 @@ array = { local mapped = array.map(obj, callback) return array.flat(mapped) + end, + + -- Creates a new table composed of keys generated from the results of running each element of the given table through the given callback + -- @param obj {table} + -- @param callback {function} + -- @return {table} + key_by = function(obj, callback) + utils.raises_error(array, obj, 'key_by') + + return array.reduce(obj, function(accumulator, current) + local key = callback(current) + accumulator[key] = current + return accumulator + end, {}) end } diff --git a/test.lua b/test.lua index 79dce65..d0fc2dc 100644 --- a/test.lua +++ b/test.lua @@ -434,3 +434,15 @@ test('flat_map', function(a) { 1, 2, 2, 4, 3, 6 } ) end) + +test('key_by', function(a) + a.deep_equal( + array.key_by({ { id = 1, name = 'lua' }, { id = 2, name = 'javascript' } }, function(item) + return item.id + end), + { + [1] = { id = 1, name = 'lua' }, + [2] = { id = 2, name = 'javascript' } + } + ) +end) From ddcc5c3ee3a26e40aa0989de8b3e7667391d023b Mon Sep 17 00:00:00 2001 From: Evandro Leopoldino Goncalves Date: Thu, 18 Jan 2024 18:52:49 +0100 Subject: [PATCH 62/65] updates readme --- README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/README.md b/README.md index 47b1604..0f4813b 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,6 @@ A small library with useful methods to handle Lua's table when it's working like an Array.
For docs, see: https://evandrolg.github.io/array.lua/ -[![Build -Status](https://travis-ci.org/EvandroLG/array.lua.svg?branch=master)](https://travis-ci.org/EvandroLG/array.lua) - ## Installation To install array, run: ```sh From fc9fd2f211e43b876868e5de4c10c3a454066e31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Thu, 18 Jan 2024 19:01:50 +0100 Subject: [PATCH 63/65] Update init.lua --- src/array/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/array/init.lua b/src/array/init.lua index 020d3db..210c5bd 100644 --- a/src/array/init.lua +++ b/src/array/init.lua @@ -3,7 +3,7 @@ local utils = require('array.utils') local array array = { - __VERSION = '1.3.5', + __VERSION = '1.3.6', __DESCRIPTION = "A small library with useful methods to handle Lua's table when it's working like an Array", __LICENSE = [[ The MIT License (MIT) From 589afde5ed94fa5a4530eb8b2b30722d79ef2386 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Fri, 8 Mar 2024 22:36:10 +0100 Subject: [PATCH 64/65] fixes issue with the github action --- .github/workflows/main.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 3a48f01..1dcbdbd 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,12 +10,12 @@ on: jobs: test: - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest strategy: fail-fast: false matrix: - luaVersion: ["5.3"] + luaVersion: ["5.4"] steps: - name: Checkout @@ -32,4 +32,4 @@ jobs: - name: Run unit tests run: | - LUA_PATH="./src/?.lua;./src/?/init.lua;./src/array/?.lua;;" lua test.lua + LUA_PATH="./src/?.lua;./src/?/init.lua;;$(luarocks path --lr-path)" lua test.lua From c8fa4109538ca9ce6833ac5907844e6efaa4dbdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Fri, 8 Mar 2024 22:39:58 +0100 Subject: [PATCH 65/65] Update test.lua --- test.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test.lua b/test.lua index d0fc2dc..e5b5a3f 100644 --- a/test.lua +++ b/test.lua @@ -2,7 +2,7 @@ local test = require 'simple_test' local array = require 'array' test('meta infos', function(a) - a.equal(array.__VERSION, '1.3.5') + a.equal(array.__VERSION, '1.3.6') a.equal(array.__DESCRIPTION, "A small library with useful methods to handle Lua's table when it's working like an Array") end)