mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-22 22:24:35 +00:00
Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
93e6073128
26
tablex.lua
26
tablex.lua
@ -209,7 +209,7 @@ else
|
|||||||
--useful when multiple references are being held
|
--useful when multiple references are being held
|
||||||
--so you cannot just create a new table
|
--so you cannot just create a new table
|
||||||
function tablex.clear(t)
|
function tablex.clear(t)
|
||||||
assert(type(t) == "table", "table.clear - argument 't' must be a table")
|
assert(type(t) == "table", "tablex.clear - argument 't' must be a table")
|
||||||
local k = next(t)
|
local k = next(t)
|
||||||
while k ~= nil do
|
while k ~= nil do
|
||||||
t[k] = nil
|
t[k] = nil
|
||||||
@ -240,7 +240,7 @@ end
|
|||||||
-- but doesn't clear anything out
|
-- but doesn't clear anything out
|
||||||
-- (useful for deep overlays and avoiding garbage)
|
-- (useful for deep overlays and avoiding garbage)
|
||||||
function tablex.copy(t, deep_or_into)
|
function tablex.copy(t, deep_or_into)
|
||||||
assert(type(t) == "table", "table.copy - argument 't' must be a table")
|
assert(type(t) == "table", "tablex.copy - argument 't' must be a table")
|
||||||
local is_bool = type(deep_or_into) == "boolean"
|
local is_bool = type(deep_or_into) == "boolean"
|
||||||
local is_table = type(deep_or_into) == "table"
|
local is_table = type(deep_or_into) == "table"
|
||||||
|
|
||||||
@ -259,14 +259,20 @@ function tablex.copy(t, deep_or_into)
|
|||||||
return into
|
return into
|
||||||
end
|
end
|
||||||
|
|
||||||
--overlay one table directly onto another, shallow only
|
--overlay tables directly onto one another, shallow only
|
||||||
function tablex.overlay(to, from)
|
--takes as many tables as required,
|
||||||
assert(type(to) == "table", "table.overlay - argument 'to' must be a table")
|
--overlays them in passed order onto the first,
|
||||||
assert(type(from) == "table", "table.overlay - argument 'from' must be a table")
|
--and returns the first table with the overlay(s) applied
|
||||||
for k,v in pairs(from) do
|
function tablex.overlay(a, b, ...)
|
||||||
to[k] = v
|
assert(type(a) == "table", "tablex.overlay - argument 'a' must be a table")
|
||||||
|
assert(type(b) == "table", "tablex.overlay - argument 'b' must be a table")
|
||||||
|
for k,v in pairs(b) do
|
||||||
|
a[k] = v
|
||||||
end
|
end
|
||||||
return to
|
if ... then
|
||||||
|
return tablex.overlay(a, ...)
|
||||||
|
end
|
||||||
|
return a
|
||||||
end
|
end
|
||||||
|
|
||||||
--collapse the first level of a table into a new table of reduced dimensionality
|
--collapse the first level of a table into a new table of reduced dimensionality
|
||||||
@ -278,7 +284,7 @@ end
|
|||||||
-- so they can't exist in the base level
|
-- so they can't exist in the base level
|
||||||
-- (... or at least, their non-ipairs members won't survive the collapse)
|
-- (... or at least, their non-ipairs members won't survive the collapse)
|
||||||
function tablex.collapse(t)
|
function tablex.collapse(t)
|
||||||
assert(type(t) == "table", "table.collapse - argument 't' must be a table")
|
assert(type(t) == "table", "tablex.collapse - argument 't' must be a table")
|
||||||
local r = {}
|
local r = {}
|
||||||
for _, v in ipairs(t) do
|
for _, v in ipairs(t) do
|
||||||
if type(v) == "table" then
|
if type(v) == "table" then
|
||||||
|
Loading…
Reference in New Issue
Block a user