-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.lua
executable file
·185 lines (153 loc) · 3.98 KB
/
utils.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
local utils = {}
-- eg. utils.get_by_path({a={b=1}}, "a.b")
function utils.get_by_path(tbl, path, default)
local l = str.split("%.", path)
if tbl == nil then return default end
if #l == 0 then return default end
local t = tbl
--~ utils.vardump(t)
for i = 1, #l do
local k = l[i]
-- try to use key as number type if possible
local kn = tonumber(k)
if kn then t = t[kn] else t = t[k] end
--~ print(i,k,t,#l)
if t == nil then return default end -- print("key " .. i .. " not found: " .. k) return default end
end
return t
end
function utils.vardump(value, max_depth)
utils.vardump_rec(max_depth or 1, value)
end
function utils.vardump_rec(max_depth, value, depth, key)
if (depth or 0) > max_depth then return end
local linePrefix = ""
local spaces = ""
if key ~= nil then
linePrefix = "["..key.."] = "
end
if depth == nil then
depth = 0
else
depth = depth + 1
for i=1, depth do spaces = spaces .. " " end
end
if type(value) == 'table' then
local mTable = getmetatable(value)
if mTable == nil then
print(spaces ..linePrefix.."(table) ")
else
print(spaces .."(metatable) ")
value = mTable
end
for tableKey, tableValue in pairs(value) do
utils.vardump_rec(max_depth, tableValue, depth, tableKey)
end
elseif type(value) == 'function' or
type(value) == 'thread' or
type(value) == 'userdata' or
value == nil
then
print(spaces..tostring(value))
else
print(spaces..linePrefix.."("..type(value)..") "..tostring(value))
end
end
function utils.clamp01 (value)
if value < 0 then return 0
elseif value > 1 then return 1
else return value end
end
function utils.clamp (value, min, max)
if value < min then return min
elseif value > max then return max
else return value end
end
function utils.mapIntoRange (srcValue, srcMin, srcMax, dstMin, dstMax)
if srcMax - srcMin <= 0.0000001 then return dstMin end
local r = (utils.clamp (srcValue, srcMin, srcMax) - srcMin) / (srcMax - srcMin)
return dstMin + (dstMax - dstMin) * r
end
function utils.sign (x)
if x < 0 then return -1 end
if x > 0 then return 1 end
return 0
end
-- function fun(key, value) -> key, value
function utils.filter2 (l, fun)
local r = {}
for k,v in pairs(l) do
if fun(k,v) then r[kk] = vv end
end
return r
end
-- function fun(key, value) -> key, value
function utils.map2 (l, fun)
local r = {}
for k,v in pairs(l) do
local kk,vv = fun(k,v)
r[kk] = vv
end
return r
end
-- function fun(value) -> value
function utils.map1 (l, fun)
local r = {}
for k,v in pairs(l) do
r[k] = fun(v)
end
return r
end
-- returns {key0, key1, ...}
function utils.keys (l)
r = {}
for k,v in pairs(l) do
table.insert(r, k)
end
return r
end
-- returns {value0, value1, ...}
function utils.values (l)
r = {}
for k,v in pairs(l) do
table.insert(r, v)
end
return r
end
function utils.toHex(s)
local replaceNonPrintableChars = function (s, replacement)
local r = ""
for i = 1,string.len(s) do
local b = s:byte(i)
if b >= 33 and b <= 126 then r = r .. string.char(b)
else r = r .. replacement end
end
return r
end
if not s or type(s) ~= "string" then return "<nil>" end
local r = ""
for i = 1,string.len(s) do
r = r .. (s:byte(i) < 16 and "0" or "") .. string.format("%x ", s:byte(i))
end
r = r .. "(" .. string.len(s) .. " bytes) [" .. replaceNonPrintableChars(s, ".") .. "]"
return r
end
function utils.round(num, idp)
local mult = 10^(idp or 0)
return math.floor(num * mult + 0.5) / mult
end
function utils.color255to1(color)
local c = {color[1],color[2],color[3]}
c[1] = utils.mapIntoRange(color[1], 0, 255, 0, 1)
c[2] = utils.mapIntoRange(color[2], 0, 255, 0, 1)
c[3] = utils.mapIntoRange(color[3], 0, 255, 0, 1)
return c
end
function utils.color1to255(color)
local c = {color[1],color[2],color[3]}
c[1] = utils.mapIntoRange(color[1], 0, 1, 0, 255)
c[2] = utils.mapIntoRange(color[2], 0, 1, 0, 255)
c[3] = utils.mapIntoRange(color[3], 0, 1, 0, 255)
return c
end
return utils