Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local api = {}
- function api.isTable(o)
- return type(o) == "table"
- end
- function api.hasKey(tbl, target)
- if tbl[target] ~= nil then
- return true
- end
- -- key might exist but set to nil value
- for key, _ in pairs(tbl) do
- if key == target then
- return true
- end
- end
- return false
- end
- function api.hasValue(tbl, target)
- for _, value in pairs(tbl) do
- if value == target then
- return true
- end
- end
- return false
- end
- function api.size(tbl)
- count = 0
- for _ in pairs(tbl) do count = count + 1 end
- return count
- end
- function api.print(tbl, level)
- level = (type(level) == "number") and level or 1
- if api.isTable(tbl) then
- local s = "{"
- if api.size(tbl) > 0 then
- for k, v in pairs(tbl) do
- if not api.isTable(k) then
- k = "\""..(k ~= nil and tostring(k) or "nil").."\""
- else
- k = api.print(k, level + 1)
- end
- s = s.."\n"..(string.rep("\t", level))..""..k.." = "..api.print(v, level + 1)..","
- end
- else
- return "{}"
- end
- return s.."\n"..(string.rep("\t", level - 1)).."}"
- else
- return tostring(tbl)
- end
- end
- local index = {}
- local metatable = {
- __index = function(t, k)
- -- print("*access to element "..tostring(k))
- return t[index][k]
- end,
- __newindex = function(t, k, v)
- -- print("*update of element "..tostring(k).." to "..tostring(v))
- t[index][k] = v
- end
- }
- function api.proxy(tbl)
- local proxy = {}
- proxy[index] = tbl
- setmetatable(proxy, metatable)
- return proxy, metatable
- end
- return api
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement