-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.lua
134 lines (119 loc) · 3.24 KB
/
helpers.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
DT = 1/60
---
---Raises an error if the value of its argument v is false (i.e., `nil` or `false`); otherwise, returns all its arguments. In case of error, `message` is the error object; when absent, it defaults to `"assertion failed!"`
---
---[View documents](command:extension.lua.doc?["en-us/53/manual.html/pdf-assert"])
---
---@generic T
---@param v? T
---@param message? any
---@return T
---@return any ...
function assert(v, message, ...)
if not v then
error(message or "assertion failed!")
end
return v, message, ...
end
--- NOTE: Based of LifeBoatAPI's lb_tostring
--- Converts the given value t, to a string, regardless of what type of value it is
--- Doesn't handle self-referential tables (e.g. a = {}; a.b=a)
---@param t any
---@param indent nil|number
---@return string
function toStringRepr(t, maxdepth, indent, seen)
seen = seen or {}
indent = (indent or 0) + 1
maxdepth = maxdepth or math.maxinteger
local typeof = type(t)
if typeof == "table" then
local existing = seen[t]
if existing then
return "{REF-"..tostring(t).."}"
elseif indent > (maxdepth+1) then
return "{"..tostring(t).."}"
else
seen[t] = true
local s = {}
for k,v in pairs(t) do
local kType = type(k)
if kType == "string" then
s[#s+1] = string.rep(" ", indent*4) .. "" .. tostring(k) .. " = " .. toStringRepr(v, maxdepth, indent, seen)
elseif kType ~= "number" or (k < 1 or k > #t) then
s[#s+1] = string.rep(" ", indent*4) .. "[" .. tostring(k) .. "] = " .. toStringRepr(v, maxdepth, indent, seen)
end
-- don't print numbers, do numericals below
end
for i=1,#t do
s[#s+1] = string.rep(" ", indent*4) .. toStringRepr(t[i], maxdepth, indent, seen)
end
if #s > 0 then
return "{<"..tostring(t)..">\n" .. table.concat(s, ",\n") .. "\n" .. string.rep(" ", (indent-1)*4) .. "}"
else
return "{<"..tostring(t)..">}"
end
end
elseif typeof == "string" then
return "\"" .. t .. "\""
else
return tostring(t)
end
end
---@param source table
---@param dest table?
function shallowCopy(source, dest)
dest = dest or {}
for i, v in pairs(source) do
dest[i] = v
end
return dest
end
---@generic T : table?
---@param source T
---@param dest T
---@return T
function simpleDeepCopy(source, dest)
if source == nil then
return nil
end
dest = dest or {}
for i, v in pairs(source) do
if type(v) == "table" then
v = simpleDeepCopy(v, dest[i] or {})
end
dest[i] = v
end
return dest
end
local TRUTHY_VALUES = {t=true, tr=true, tru=true, ["true"]=true, y=true, ye=true, yes=true}
---@param s string|nil
function arg_truthy(s)
return s ~= nil and TRUTHY_VALUES[s:lower()] or false
end
---@param seconds number
---@return string
function fmtRate(seconds)
local hours = 0
local days = 0
if seconds >= 60 then
hours = seconds // 60
seconds = seconds - hours*60
if hours >= 24 then
days = hours // 24
hours = hours - hours*24
end
end
local parts = {("%.0fs"):format(seconds)}
if hours > 0 then
table.insert(parts, ("%.0fh"):format(hours))
end
if days > 0 then
table.insert(parts, ("%.0fdays"):format(days))
end
return table.concat(parts, " ")
end
---@param value number
---@param decimals integer
function round(value, decimals)
return tonumber(string.format("%."..(decimals//1).."f", value))
end