diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..1dcbdbd --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,35 @@ +name: CI + +on: + push: + branches: + - master + pull_request: + branches: + - master + +jobs: + test: + runs-on: ubuntu-latest + + strategy: + fail-fast: false + matrix: + luaVersion: ["5.4"] + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - uses: leafo/gh-actions-lua@v9 + with: + luaVersion: ${{ matrix.luaVersion }} + + - uses: leafo/gh-actions-luarocks@v4 + + - name: Install dependencies + run: luarocks install simple_test + + - name: Run unit tests + run: | + LUA_PATH="./src/?.lua;./src/?/init.lua;;$(luarocks path --lr-path)" lua test.lua 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 diff --git a/src/array/init.lua b/src/array/init.lua index 0c843f8..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.4', + __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) @@ -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) @@ -637,6 +641,31 @@ 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, + + -- 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 03f2c55..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.4') + 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) @@ -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) @@ -415,3 +425,24 @@ 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) + +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)