-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.lua
82 lines (73 loc) · 1.63 KB
/
util.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
function tableLength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
function tableMerge(t1, t2)
for k,v in pairs(t2) do
if type(v) == "table" then
if type(t1[k] or false) == "table" then
tableMerge(t1[k] or {}, t2[k] or {})
else
t1[k] = v
end
else
t1[k] = v
end
end
return t1
end
function dump(o)
if type(o) == 'table' then
local s = '{\n'
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. dump(v) .. ',\n'
end
return s .. '} \n'
else
return tostring(o)
end
end
function tableConcat(t1, t2)
for i=1,#t2 do
t1[#t1+1] = t2[i]
end
return t1
end
-- table.filter({"a", "b", "c", "d"}, function(o, k, i) return o >= "c" end) --> {"c","d"}
--
-- @FGRibreau - Francois-Guillaume Ribreau
-- @Redsmin - A full-feature client for Redis http://redsmin.com
table.filter = function(t, filterIter)
local out = {}
for k, v in pairs(t) do
if filterIter(v, k, t) then out[k] = v end
end
return out
end
function printObj(obj, hierarchyLevel)
if (hierarchyLevel == nil) then
hierarchyLevel = 0
elseif (hierarchyLevel == 4) then
return 0
end
local whitespace = ""
for i=0,hierarchyLevel,1 do
whitespace = whitespace .. "-"
end
io.write(whitespace)
print(obj)
if (type(obj) == "table") then
for k,v in pairs(obj) do
io.write(whitespace .. "-")
if (type(v) == "table") then
printObj(v, hierarchyLevel+1)
else
print(v)
end
end
else
print(obj)
end
end