-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalue.lua
More file actions
347 lines (311 loc) · 9.1 KB
/
value.lua
File metadata and controls
347 lines (311 loc) · 9.1 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
---@alias Types 'string'|'number'|'function'|'boolean'|'table'
---@alias EmptyTypes 'string'|'number'|'integer'|'table'
local MODSTR = 'user_api.check.value'
local ERROR = vim.log.levels.ERROR
local WARN = vim.log.levels.WARN
---@param t Types
---@return fun(var: any, multiple?: boolean): boolean
local function type_fun(t)
local ALLOWED_TYPES = {
is_bool = 'boolean',
is_fun = 'function',
is_num = 'number',
is_str = 'string',
is_tbl = 'table',
}
local ret = true
local name = ''
for k, _type in pairs(ALLOWED_TYPES) do
if _type == t then
ret = false
name = k
break
end
end
if ret then
error(('(%s.type_fun): Invalid type `%s`'):format(MODSTR, t), ERROR)
end
---@param var any
---@param multiple? boolean
return function(var, multiple)
require('user_api.check.exists').validate({
multiple = { multiple, { 'boolean', 'nil' }, true },
})
if multiple == nil then
multiple = false
end
if not multiple then
return var ~= nil and type(var) == t
end
if var == nil or type(var) ~= 'table' then
return false
end
for _, v in ipairs(var) do
if t == nil or type(v) ~= t then
vim.notify(('(%s.%s): Input is not a table (`multiple` is true)'):format(MODSTR, name), WARN)
return false
end
end
return true
end
end
---Value checking utilities.
---
---Pretty much reserved for data checking, type checking
---and conditional operations.
--- ---
---@class User.Check.Value
local M = {}
---Checks whether a value is a string
--- ---
M.is_str = type_fun('string')
---Checks whether a value is a boolean
--- ---
M.is_bool = type_fun('boolean')
---Checks whether a value is a function
--- ---
M.is_fun = type_fun('function')
---Checks whether a value is a number
--- ---
M.is_num = type_fun('number')
---Checks whether a value is a table
--- ---
M.is_tbl = type_fun('table')
---Checks whether a value is an integer,
---i.e. _greater than or equal to `0` and a **whole number**_.
--- ---
---@param var any Any data type to be checked if it's an integer
---@param multiple? boolean Tell the integer you're checking for multiple values. (Default: `false`)
---@return boolean is_int
function M.is_int(var, multiple)
require('user_api.check.exists').validate({ multiple = { multiple, { 'boolean', 'nil' }, true } })
if multiple == nil then
multiple = false
end
if not multiple then
return M.is_num(var) and var == math.floor(var) and var == math.ceil(var)
end
if not M.is_tbl(var) then
vim.notify(('(%s.is_int): Input is not a table (`multiple` is true)'):format(MODSTR), WARN)
return false
end
for _, v in ipairs(var) do
if not (M.is_num(v) and v == math.floor(v) and v == math.ceil(v)) then
return false
end
end
return true
end
---Returns whether one or more given string/number/table are **empty**.
---
---Scenarios included if `multiple` is `false`:
---
--- - Is an empty string (`x == ''`)
--- - Is an integer equal to zero (`x == 0`)
--- - Is an empty table (`{}`)
---
---If `multiple` is `true` apply the above to a table of allowed values.
---
---**THIS FUNCTION IS NOT RECURSIVE!**
--- ---
---@param data (string|number)[]|string|number|table
---@param multiple boolean|nil
---@return boolean is_empty
---@overload fun(data: (string|number)[]|string|number|table): is_empty: boolean
function M.empty(data, multiple)
require('user_api.check.exists').validate({
data = { data, { 'string', 'table', 'number' } },
multiple = { multiple, { 'boolean', 'nil' }, true },
})
if multiple == nil then
multiple = false
end
if M.is_str(data) then
return data == ''
end
if M.is_num(data) then
return data == 0
end
if not multiple then
return vim.tbl_isempty(data)
end
if vim.tbl_isempty(data) then
vim.notify(('(%s.empty): No values to check!'):format(MODSTR), WARN)
return true
end
for _, val in ipairs(data) do
---NOTE: NO RECURSIVE CHECKING
if M.empty(val, false) then
return true
end
end
return false
end
---Checks whether a certain number `num` is within a specified range.
--- ---
---@param num number The number to be checked
---@param low number The low limit
---@param high number The high limit
---@param eq { low: boolean, high: boolean }|nil A table that defines how equalities will be made
---@return boolean in_range
---@overload fun(num: number, low: number, high: number): in_range: boolean
function M.num_range(num, low, high, eq)
require('user_api.check.exists').validate({
num = { num, { 'number' } },
low = { low, { 'number' } },
high = { high, { 'number' } },
eq = { eq, { 'table', 'nil' }, true },
})
eq = M.type_not_empty('table', eq) and eq or { low = true, high = true }
eq.high = M.is_bool(eq.high) and eq.high or true
eq.low = M.is_bool(eq.low) and eq.low or true
if low > high then
low, high = high, low
end
local Comps = {
low_no_high = function()
return num >= low and num < high
end,
high_no_low = function()
return num > low and num <= high
end,
high_low = function()
return num >= low and num <= high
end,
none = function()
return num > low and num < high
end,
}
if eq.high and eq.low then
return Comps.high_low()
end
if eq.high and not eq.low then
return Comps.high_no_low()
end
if not eq.high and eq.low then
return Comps.low_no_high()
end
return Comps.none()
end
---@param field (string|integer)[]|string|integer
---@param T table<string|integer, any>
---@return boolean found
function M.fields(field, T)
require('user_api.check.exists').validate({
field = { field, { 'string', 'number', 'table', 'nil' }, true },
T = { T, { 'table' } },
})
if not M.is_tbl(field) then
return T[field] ~= nil
end
for _, v in ipairs(field) do
if not M.fields(v, T) then
return false
end
end
return true
end
---@param values any[]|table<string, any>
---@param T table
---@param return_keys boolean|nil
---@return boolean|string|integer|(string|integer)[] res
---@overload fun(values: any[]|table<string, any>, T: table): res: boolean|string|integer|(string|integer)[]
function M.tbl_values(values, T, return_keys)
require('user_api.check.exists').validate({
values = { values, { 'table' } },
T = { T, { 'table' } },
return_keys = { return_keys, { 'boolean', 'nil' }, true },
})
if return_keys == nil then
return_keys = false
end
local res = return_keys and {} or false ---@type boolean|string|integer|(string|integer)[]
for _, val in pairs(values) do
for k, v in pairs(T) do
if return_keys and v == val then
table.insert(res, k)
elseif not return_keys and v == val then
res = true
break
end
end
-- If not returning key, and no value found after previous sweep, break
if not (return_keys or res) then
break
end
end
if return_keys then
res = #res == 1 and res[1] or (M.empty(res) and false or res)
end
return res
end
---@param type_str Types
---@param T table
---@return boolean is_single_type
function M.single_type_tbl(type_str, T)
require('user_api.check.exists').validate({
type_str = { type_str, { 'string' } },
T = { T, { 'table' } },
})
if not vim.list_contains({ 'boolean', 'function', 'number', 'string', 'table' }, type_str) then
error(('(%s.single_type_tbl): Wrong type `%s`.'):format(MODSTR, type_str))
end
if vim.tbl_isempty(T) then
vim.notify(('(%s.single_type_tbl): Expected a non-empty table!'):format(MODSTR), ERROR)
return false
end
for _, v in pairs(T) do
if (type_str == 'nil' and v ~= nil) or type(v) ~= type_str then
return false
end
end
return true
end
---Check if given data is a string/table/integer/number and whether it's empty or not.
---
---Specifies what data type should the given value be
---and this function will check both if it's that type
---and if so, whether it's empty (for numbers this means a value of `0`).
--- ---
---@param type_str EmptyTypes
---@param data any
---@return boolean result
function M.type_not_empty(type_str, data)
require('user_api.check.exists').validate({ type_str = { type_str, { 'string' } } })
if not vim.list_contains({ 'integer', 'number', 'string', 'table' }, type_str) then
error(('(%s.type_not_empty): Invalid type `%s`!'):format(MODSTR, type_str))
end
if data == nil then
return false
end
local valid_types = {
string = M.is_str,
integer = M.is_int,
number = M.is_num,
table = M.is_tbl,
}
if not vim.list_contains(vim.tbl_keys(valid_types), type_str) then
return false
end
return valid_types[type_str](data) and not M.empty(data)
end
---Checks whether a certain `num` does not exceed table index range
---_i.e._ `num >= 1 and num <= #T`.
---
---If the table is empty, then it'll return `false`.
--- ---
---@param index integer
---@param T any[]
---@return boolean found
function M.in_tbl_range(index, T)
require('user_api.check.exists').validate({
index = { index, { 'number' } },
T = { T, { 'table' } },
})
if vim.tbl_isempty(T) then
return false
end
return index >= 1 and index <= #T
end
return M
-- vim: set ts=2 sts=2 sw=2 et ai si sta: