Skip to content

Commit

Permalink
core/util: add table_values() helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
vsergeev committed Apr 5, 2021
1 parent d956f2e commit 5194388
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
17 changes: 16 additions & 1 deletion radio/core/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ local function table_keys(table)
return keys
end

---
-- Get the values of a table.
--
-- @internal
-- @function table_values
-- @tparam table table Table
-- @treturn array values of table
local function table_values(table)
local values = {}
for _, v in pairs(table) do
values[#values + 1] = v
end
return values
end

---
-- Make a shallow clone of a table.
--
Expand Down Expand Up @@ -296,4 +311,4 @@ local function format_options(options)
return table.concat(lines, "\n")
end

return {table_length = table_length, table_keys = table_keys, table_copy = table_copy, table_extend = table_extend, array_concat = array_concat, array_flatten = array_flatten, array_map = array_map, array_exists = array_exists, array_search = array_search, array_all = array_all, array_equals = array_equals, array_find = array_find, parse_args = parse_args, format_options = format_options}
return {table_length = table_length, table_keys = table_keys, table_values = table_values, table_copy = table_copy, table_extend = table_extend, array_concat = array_concat, array_flatten = array_flatten, array_map = array_map, array_exists = array_exists, array_search = array_search, array_all = array_all, array_equals = array_equals, array_find = array_find, parse_args = parse_args, format_options = format_options}
12 changes: 12 additions & 0 deletions tests/core/util_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ describe("table and array utilities", function ()
assert.is.same(keys, {"a", "b", "c"})
end)

it("table_values()", function ()
assert.is.same(util.table_values({}), {})

local values = util.table_values({[4] = "abc", [5] = "def", [6] = "ghi"})
table.sort(values)
assert.is.same(values, {"abc", "def", "ghi"})

local values = util.table_values({a = 4, b = 5, c = 6})
table.sort(values)
assert.is.same(values, {4, 5, 6})
end)

it("table_copy()", function ()
local x = {foo = 'bar', bar = nil, abc = true, def = 1}
local y = util.table_copy(x)
Expand Down

0 comments on commit 5194388

Please sign in to comment.