-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_util.lua
More file actions
45 lines (38 loc) · 765 Bytes
/
Copy path_util.lua
File metadata and controls
45 lines (38 loc) · 765 Bytes
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
local fmt = string.format
local M = {}
function M.tbl_copy(input)
local output = {}
for k, v in pairs(input) do
output[k] = v
end
return output
end
function M.tbl_update(dst, src)
for k, v in pairs(src) do
dst[k] = v
end
return dst
end
function M.tbl_find(t, v)
for i, value in ipairs(t) do
if value == v then
return i
end
end
end
function M.tbl_keys(t)
local keys = {}
for k in pairs(t) do
keys[#keys + 1] = k
end
return keys
end
---@type fun <T> (lbl:string?, v:any, tp:type, optional:boolean?):(value:T)
function M.validate(lbl, v, tp, optional)
local tv = type(v)
if tv == tp or (optional and v == nil) then
return v
end
error(fmt("%s: (%s expected, got %s)", lbl, tp, tv), 3)
end
return M