Simplify & alter nilget

This commit is contained in:
Lars Mueller 2021-01-19 13:18:25 +01:00
parent a12ce08430
commit 15ce314e8c
2 changed files with 11 additions and 16 deletions

@ -15,19 +15,12 @@ function set_case_insensitive_index(table)
return map_index(table, string.lower)
end
function nilget(table, key, ...)
assert(key ~= nil)
local function nilget(table, key, ...)
if key == nil then
return table
end
local value = table[key]
if value == nil then
return nil
end
return nilget(value, ...)
--+ nilget(a, "b", "c") == a?.b?.c
function nilget(value, key, ...)
if value == nil or key == nil then
return value
end
return nilget(table, key, ...)
return nilget(value[key], ...)
end
-- Fisher-Yates

@ -1,9 +1,8 @@
-- table
local t = {}
t[t] = t
local t2 = modlib.table.deepcopy(t)
assert(t2[t2] == t2)
assert(modlib.table.equals_noncircular({[{}]={}}, {[{}]={}}))
assert(modlib.table.equals_content(t, t2))
assert(modlib.table.is_circular(t))
@ -23,8 +22,11 @@ assert(equals_references(t, t2))
local x, y = {}, {}
assert(not equals_references({[x] = x, [y] = y}, {[x] = y, [y] = x}))
assert(equals_references({[x] = x, [y] = y}, {[x] = x, [y] = y}))
assert(modlib.table.nilget({a = {b = {c = 42}}}, "a", "b", "c") == 42)
assert(modlib.table.nilget({a = {}}, "a", "b", "c") == nil)
local nilget = modlib.table.nilget
assert(nilget({a = {b = {c = 42}}}, "a", "b", "c") == 42)
assert(nilget({a = {}}, "a", "b", "c") == nil)
assert(nilget(nil, "a", "b", "c") == nil)
assert(nilget(nil, "a", nil, "c") == nil)
assert(modlib.string.escape_magic_chars("%") == "%%")
local tests = {
liquid_dir = false,