From 49909f1976ed469c0bb40218ccd8cbf2248a8de2 Mon Sep 17 00:00:00 2001 From: Evandro Leopoldino Goncalves Date: Mon, 25 Dec 2023 21:42:20 +0100 Subject: [PATCH 01/18] 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 02/18] 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 03/18] 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 04/18] 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 05/18] 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 06/18] 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 07/18] 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 08/18] 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 09/18] 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 10/18] 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 11/18] 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 12/18] 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 13/18] 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 14/18] 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 15/18] 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 16/18] 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 17/18] 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 18/18] 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)