mirror of
https://github.com/appgurueu/modlib.git
synced 2024-08-20 05:14:10 +02:00
Simplify & alter nilget
This commit is contained in:
parent
a12ce08430
commit
15ce314e8c
17
table.lua
17
table.lua
@ -15,19 +15,12 @@ function set_case_insensitive_index(table)
|
|||||||
return map_index(table, string.lower)
|
return map_index(table, string.lower)
|
||||||
end
|
end
|
||||||
|
|
||||||
function nilget(table, key, ...)
|
--+ nilget(a, "b", "c") == a?.b?.c
|
||||||
assert(key ~= nil)
|
function nilget(value, key, ...)
|
||||||
local function nilget(table, key, ...)
|
if value == nil or key == nil then
|
||||||
if key == nil then
|
return value
|
||||||
return table
|
|
||||||
end
|
end
|
||||||
local value = table[key]
|
return nilget(value[key], ...)
|
||||||
if value == nil then
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
return nilget(value, ...)
|
|
||||||
end
|
|
||||||
return nilget(table, key, ...)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Fisher-Yates
|
-- Fisher-Yates
|
||||||
|
10
test.lua
10
test.lua
@ -1,9 +1,8 @@
|
|||||||
|
-- table
|
||||||
local t = {}
|
local t = {}
|
||||||
t[t] = t
|
t[t] = t
|
||||||
|
|
||||||
local t2 = modlib.table.deepcopy(t)
|
local t2 = modlib.table.deepcopy(t)
|
||||||
assert(t2[t2] == t2)
|
assert(t2[t2] == t2)
|
||||||
|
|
||||||
assert(modlib.table.equals_noncircular({[{}]={}}, {[{}]={}}))
|
assert(modlib.table.equals_noncircular({[{}]={}}, {[{}]={}}))
|
||||||
assert(modlib.table.equals_content(t, t2))
|
assert(modlib.table.equals_content(t, t2))
|
||||||
assert(modlib.table.is_circular(t))
|
assert(modlib.table.is_circular(t))
|
||||||
@ -23,8 +22,11 @@ assert(equals_references(t, t2))
|
|||||||
local x, y = {}, {}
|
local x, y = {}, {}
|
||||||
assert(not equals_references({[x] = x, [y] = y}, {[x] = y, [y] = x}))
|
assert(not equals_references({[x] = x, [y] = y}, {[x] = y, [y] = x}))
|
||||||
assert(equals_references({[x] = x, [y] = y}, {[x] = x, [y] = y}))
|
assert(equals_references({[x] = x, [y] = y}, {[x] = x, [y] = y}))
|
||||||
assert(modlib.table.nilget({a = {b = {c = 42}}}, "a", "b", "c") == 42)
|
local nilget = modlib.table.nilget
|
||||||
assert(modlib.table.nilget({a = {}}, "a", "b", "c") == nil)
|
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("%") == "%%")
|
assert(modlib.string.escape_magic_chars("%") == "%%")
|
||||||
local tests = {
|
local tests = {
|
||||||
liquid_dir = false,
|
liquid_dir = false,
|
||||||
|
Loading…
Reference in New Issue
Block a user