-
Notifications
You must be signed in to change notification settings - Fork 42
/
util.lua
214 lines (187 loc) · 3.87 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
select(2, ...) 'aux'
M.immutable = setmetatable({}, {
__metatable = false,
__newindex = pass,
__sub = function(_, t)
return setmetatable({}, { __metatable = false, __newindex = pass, __index = t })
end
})
function M.pluralize(text)
local text = gsub(text, '(-?%d+)(.-)|4([^;]-);', function(number_string, gap, number_forms)
local singular, dual, plural
_, _, singular, dual, plural = strfind(number_forms, '(.+):(.+):(.+)')
if not singular then
_, _, singular, plural = strfind(number_forms, '(.+):(.+)')
end
local i = abs(tonumber(number_string))
local number_form
if i == 1 then
number_form = singular
elseif i == 2 then
number_form = dual or plural
else
number_form = plural
end
return number_string .. gap .. number_form
end)
return text
end
function M.wipe(t)
setmetatable(t, nil)
for k in pairs(t) do
t[k] = nil
end
end
function M.set(...)
local t = {}
for i = 1, select('#', ...) do
t[select(i, ...)] = true
end
return t
end
function M.iter(...)
local t = {}
for i = 1, select('#', ...) do
t[select(i, ...)] = true
end
return pairs(t)
end
function M.assign(t1, t2)
for k, v in pairs(t2) do
if t1[k] == nil then
t1[k] = v
end
end
return t1
end
function M.enum(n)
if n > 0 then return immutable-{}, enum(n - 1) end
end
M.join = table.concat
M.index = function(t, ...)
for _, v in ipairs{...} do
t = t and t[v]
end
return t
end
function M.modified()
return IsShiftKeyDown() or IsControlKeyDown() or IsAltKeyDown()
end
function M.copy(t)
local copy = {}
for k, v in pairs(t) do
copy[k] = v
end
return setmetatable(copy, getmetatable(t))
end
function M.size(t)
local size = 0
for _ in pairs(t) do
size = size + 1
end
return size
end
function M.key(t, value)
for k, v in pairs(t) do
if v == value then
return k
end
end
end
function M.keys(t)
local keys = {}
for k in pairs(t) do
tinsert(keys, k)
end
return keys
end
function M.values(t)
local values = {}
for _, v in pairs(t) do
tinsert(values, v)
end
return values
end
function M.eq(t1, t2)
if not t1 or not t2 then return false end
for key, value in pairs(t1) do
if t2[key] ~= value then return false end
end
for key, value in pairs(t2) do
if t1[key] ~= value then return false end
end
return true
end
function M.any(t, predicate)
for _, v in pairs(t) do
if predicate then
if predicate(v) then return true end
elseif v then
return true
end
end
return false
end
function M.all(t, predicate)
for _, v in pairs(t) do
if predicate then
if not predicate(v) then return false end
elseif not v then
return false
end
end
return true
end
function M.filter(t, predicate)
for k, v in pairs(t) do
if not predicate(v, k) then t[k] = nil end
end
return t
end
function M.map(t, f)
for k, v in pairs(t) do
t[k] = f(v, k)
end
return t
end
function M.trim(str)
return gsub(str, '^%s*(.-)%s*$', '%1')
end
function M.split(str, separator)
local parts = {}
while true do
local start_index = strfind(str, separator, 1, true)
if start_index then
local part = strsub(str, 1, start_index - 1)
tinsert(parts, part)
str = strsub(str, start_index + 1)
else
local part = strsub(str, 1)
tinsert(parts, part)
return parts
end
end
end
function M.tokenize(str)
local tokens = {}
for token in string.gmatch(str, '%S+') do tinsert(tokens, token) end
return tokens
end
function M.bounded(lower_bound, upper_bound, number)
return max(lower_bound, min(upper_bound, number))
end
function M.round(x)
return floor(x + .5)
end
function M.later(t, t0)
t0 = t0 or GetTime()
return function() return GetTime() - t0 > t end
end
function M.signal()
local arg
return function(...)
arg = {...}
end, function()
return arg
end
end