Skip to content

Commit 71c9fcd

Browse files
committed
refactor(api): check.value.empty now accepts multi-checks; docs improved.
Signed-off-by: Guennadi Maximov C <[email protected]>
1 parent d4cfb66 commit 71c9fcd

File tree

2 files changed

+138
-78
lines changed

2 files changed

+138
-78
lines changed

lua/user_api/check/value.lua

+50-15
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ require('user_api.types.user.check')
44
---@diagnostic disable-next-line:missing-fields
55
local M = {}
66

7-
-- NOTE: We define `is_nil` first as it's used by the other checkers.
7+
-- NOTE: We define `is_nil` first as it's used by the other checkers
88
--- Checks whether a value is `nil`, i.e. non existant or explicitly set as nil
99
--- ---
1010
--- ## Parameters
@@ -106,7 +106,7 @@ local function type_fun(t)
106106
end
107107
end
108108

109-
--- Checks whether a value is a string.
109+
--- Checks whether a value is a string
110110
--- ---
111111
--- ## Parameters
112112
---
@@ -124,7 +124,7 @@ end
124124
--- ---
125125
M.is_str = type_fun('string')
126126

127-
--- Checks whether a value is a boolean.
127+
--- Checks whether a value is a boolean
128128
--- ---
129129
--- ## Parameters
130130
---
@@ -140,9 +140,9 @@ M.is_str = type_fun('string')
140140
---
141141
--- A boolean value indicating whether the data is a boolean or not.
142142
--- ---
143-
144143
M.is_bool = type_fun('boolean')
145-
--- Checks whether a value is a function.
144+
145+
--- Checks whether a value is a function
146146
--- ---
147147
--- ## Parameters
148148
---
@@ -160,7 +160,7 @@ M.is_bool = type_fun('boolean')
160160
--- ---
161161
M.is_fun = type_fun('function')
162162

163-
--- Checks whether a value is a number.
163+
--- Checks whether a value is a number
164164
--- ---
165165
--- ## Parameters
166166
---
@@ -178,7 +178,7 @@ M.is_fun = type_fun('function')
178178
--- ---
179179
M.is_num = type_fun('number')
180180

181-
--- Checks whether a value is a table.
181+
--- Checks whether a value is a table
182182
--- ---
183183
--- ## Parameters
184184
---
@@ -243,22 +243,39 @@ function M.is_int(var, multiple)
243243
return true
244244
end
245245

246-
--- Returns whether a given string/number/table is "empty", including these scenarios:
247-
--- * Is an empty string (`x == ''`)
248-
--- * Is an integer equal to zero (`x == 0`)
249-
--- * Is an empty table
246+
--- Returns whether one or more given string/number/table are **empty**
247+
--- ---
248+
--- - Scenarios included if `multiple` is `false`:
249+
--- - Is an empty string (`x == ''`)
250+
--- - Is an integer equal to zero (`x == 0`)
251+
--- - Is an empty table (`{}`)
250252
---
253+
--- If `multiple` is `true` apply the above to a table of allowed values
254+
--- NOTE: **THE FUNCTION IS NOT RECURSIVE**
255+
--- ---
251256
--- ## Parameters
252-
--- * `v`: Must be either a string, number or a table.
253-
--- Otherwise you'll get complaints and the function will return `true`
254257
---
258+
--- - `v`: Must be either a string, number or a table.
259+
--- Otherwise you'll get complaints and the function will return `true`
260+
---
261+
--- - `multiple`: Tell the integer you're checking for multiple values. (Default: `false`).
262+
--- If set to `true`, every element of the table will be checked.
263+
--- If **any** element is not _empty_, the function automatically returns `false`.
264+
--- ---
255265
--- ## Returns
266+
---
256267
--- A boolean indicatin whether input data is empty or not.
257268
--- ---
258-
function M.empty(v)
269+
---@param v string|table|number|(string|table|number)[]
270+
---@param multiple? boolean
271+
---@return boolean
272+
function M.empty(v, multiple)
259273
local is_str = M.is_str
260274
local is_tbl = M.is_tbl
261275
local is_num = M.is_num
276+
local is_bool = M.is_bool
277+
278+
multiple = is_bool(multiple) and multiple or false
262279

263280
if is_str(v) then
264281
return v == ''
@@ -268,10 +285,28 @@ function M.empty(v)
268285
return v == 0
269286
end
270287

271-
if is_tbl(v) then
288+
if is_tbl(v) and not multiple then
272289
return vim.tbl_isempty(v)
273290
end
274291

292+
if is_tbl(v) and multiple then
293+
if vim.tbl_isempty(v) then
294+
vim.notify(
295+
'(user_api.check.value.empty): No values to check despite `multiple` being `true`',
296+
vim.log.levels.WARN
297+
)
298+
return true
299+
end
300+
301+
for _, val in next, v do
302+
if M.empty(val) then
303+
return true
304+
end
305+
end
306+
307+
return false
308+
end
309+
275310
vim.notify(
276311
'(user_api.check.value.empty): Value is neither a table, string nor a number',
277312
vim.log.levels.WARN

lua/user_api/types/user/check.lua

+88-63
Original file line numberDiff line numberDiff line change
@@ -14,123 +14,148 @@
1414

1515
---@class User.Check.Value
1616
--- Checks whether a value is `nil`, i.e. non existant or explicitly set as nil
17+
--- ---
1718
--- ## Parameters
1819
---
19-
--- * `var`: Any data type to be checked if it's nil.
20-
--- **Keep in mind that if `multiple` is set to `true`, this _MUST_ be a _non-empty_ table**.
21-
--- Otherwise it will be flagged as non-existant and the function will return `true`
22-
---
23-
--- * `multiple`: Tell the function you're checking for multiple values. (Default: `false`).
24-
--- If set to `true`, every element of the table will be checked.
25-
--- If **any** element doesn't exist or is `nil`, the function automatically returns false
20+
--- - `var`: Any data type to be checked if it's nil.
21+
--- **Keep in mind that if `multiple` is set to `true`, this _MUST_ be a _non-empty_ table**.
22+
--- Otherwise it will be flagged as non-existant and the function will return `true`
2623
---
24+
--- - `multiple`: Tell the function you're checking for multiple values. (Default: `false`).
25+
--- If set to `true`, every element of the table will be checked.
26+
--- If **any** element doesn't exist or is `nil`, the function automatically returns false
27+
--- ---
2728
--- ## Return
29+
---
2830
--- A boolean value indicating whether the data is `nil` or doesn't exist
2931
--- ---
3032
---@field is_nil ValueFunc
3133
--- Checks whether a value is a string
34+
--- ---
3235
--- ## Parameters
3336
---
34-
--- * `var`: Any data type to be checked if it's a string.
35-
--- **Keep in mind that if `multiple` is set to `true`, this _MUST_ be a _non-empty_ table**.
36-
--- Otherwise it will be flagged as a non-string and the function will return `false`
37-
---
38-
--- * `multiple`: Tell the function you're checking for multiple values. (Default: `false`).
39-
--- If set to `true`, every element of the table will be checked.
40-
--- If **any** element is not a string, the function automatically returns false
37+
--- - `var`: Any data type to be checked if it's a string.
38+
--- **Keep in mind that if `multiple` is set to `true`, this _MUST_ be a _non-empty_ table**.
39+
--- Otherwise it will be flagged as a non-string and the function will return `false`.
4140
---
41+
--- - `multiple`: Tell the function you're checking for multiple values. (Default: `false`).
42+
--- If set to `true`, every element of the table will be checked.
43+
--- If **any** element is not a string, the function automatically returns false.
44+
--- ---
4245
--- ## Return
43-
--- A boolean value indicating whether the data is a string or not
46+
---
47+
--- A boolean value indicating whether the data is a string or not.
4448
--- ---
4549
---@field is_str ValueFunc
4650
--- Checks whether a value is a table
51+
--- ---
4752
--- ## Parameters
4853
---
49-
--- * `var`: Any data type to be checked if it's a table.
50-
--- **Keep in mind that if `multiple` is set to `true`, this _MUST_ be a _non-empty_ table**.
51-
--- Otherwise it will be flagged as a non-table and the function will return `false`
52-
---
53-
--- * `multiple`: Tell the function you're checking for multiple values. (Default: `false`).
54-
--- If set to `true`, every element of the table will be checked.
55-
--- If **any** element is not a table, the function automatically returns false
54+
--- - `var`: Any data type to be checked if it's a table.
55+
--- **Keep in mind that if `multiple` is set to `true`, this _MUST_ be a _non-empty_ table**.
56+
--- Otherwise it will be flagged as a non-table and the function will return `false`.
5657
---
58+
--- - `multiple`: Tell the function you're checking for multiple values. (Default: `false`).
59+
--- If set to `true`, every element of the table will be checked.
60+
--- If **any** element is not a table, the function automatically returns false.
61+
--- ---
5762
--- ## Return
58-
--- A boolean value indicating whether the data is a table or not
63+
---
64+
--- A boolean value indicating whether the data is a table or not.
5965
--- ---
6066
---@field is_tbl ValueFunc
6167
--- Checks whether a value is a number
68+
--- ---
6269
--- ## Parameters
6370
---
64-
--- * `var`: Any data type to be checked if it's a number.
65-
--- **Keep in mind that if `multiple` is set to `true`, this _MUST_ be a _non-empty_ table**.
66-
--- Otherwise it will be flagged as a non-number and the function will return `false`
67-
---
68-
--- * `multiple`: Tell the function you're checking for multiple values. (Default: `false`).
69-
--- If set to `true`, every element of the table will be checked.
70-
--- If **any** element is not a number, the function automatically returns false
71+
--- - `var`: Any data type to be checked if it's a number.
72+
--- **Keep in mind that if `multiple` is set to `true`, this _MUST_ be a _non-empty_ table**.
73+
--- Otherwise it will be flagged as a non-number and the function will return `false`.
7174
---
75+
--- - `multiple`: Tell the function you're checking for multiple values. (Default: `false`).
76+
--- If set to `true`, every element of the table will be checked.
77+
--- If **any** element is not a number, the function automatically returns false.
78+
--- ---
7279
--- ## Return
73-
--- A boolean value indicating whether the data is a number or not
80+
---
81+
--- A boolean value indicating whether the data is a number or not.
7482
--- ---
7583
---@field is_num ValueFunc
7684
--- Checks whether a value is a function
85+
--- ---
7786
--- ## Parameters
7887
---
79-
--- * `var`: Any data type to be checked if it's a function.
80-
--- **Keep in mind that if `multiple` is set to `true`, this _MUST_ be a _non-empty_ table**.
81-
--- Otherwise it will be flagged as a non-function and the function will return `false`
82-
---
83-
--- * `multiple`: Tell the function you're checking for multiple values. (Default: `false`).
84-
--- If set to `true`, every element of the table will be checked.
85-
--- If **any** element is not a function, the function automatically returns false
88+
--- - `var`: Any data type to be checked if it's a function.
89+
--- **Keep in mind that if `multiple` is set to `true`, this _MUST_ be a _non-empty_ table**.
90+
--- Otherwise it will be flagged as a non-function and the function will return `false`.
8691
---
92+
--- - `multiple`: Tell the function you're checking for multiple values. (Default: `false`).
93+
--- If set to `true`, every element of the table will be checked.
94+
--- If **any** element is not a function, the function automatically returns false.
95+
--- ---
8796
--- ## Return
88-
--- A boolean value indicating whether the data is a function or not
97+
---
98+
--- A boolean value indicating whether the data is a function or not.
8999
--- ---
90100
---@field is_fun ValueFunc
91101
--- Checks whether a value is a boolean
102+
--- ---
92103
--- ## Parameters
93104
---
94-
--- * `var`: Any data type to be checked if it's a boolean.
95-
--- **Keep in mind that if `multiple` is set to `true`, this _MUST_ be a _non-empty_ table**.
96-
--- Otherwise it will be flagged as a non-boolean and the function will return `false`
97-
---
98-
--- * `multiple`: Tell the function you're checking for multiple values. (Default: `false`).
99-
--- If set to `true`, every element of the table will be checked.
100-
--- If **any** element is not a boolean, the function automatically returns false
105+
--- - `var`: Any data type to be checked if it's a boolean.
106+
--- **Keep in mind that if `multiple` is set to `true`, this _MUST_ be a _non-empty_ table**.
107+
--- Otherwise it will be flagged as a non-boolean and the function will return `false`.
101108
---
109+
--- - `multiple`: Tell the function you're checking for multiple values. (Default: `false`).
110+
--- If set to `true`, every element of the table will be checked.
111+
--- If **any** element is not a boolean, the function automatically returns false.
112+
--- ---
102113
--- ## Return
103-
--- A boolean value indicating whether the data is a boolean or not
114+
---
115+
--- A boolean value indicating whether the data is a boolean or not.
104116
--- ---
105117
---@field is_bool ValueFunc
106-
--- Checks whether a value is an integer i.e. _greater than or equal to `0` and a **whole number**_
118+
--- Checks whether a value is an integer i.e. _greater than or equal to `0` and a **whole number**_.
119+
--- ---
107120
--- ## Parameters
108121
---
109122
--- * `var`: Any data type to be checked if it's an integer.
110-
--- **Keep in mind that if `multiple` is set to `true`, this _MUST_ be a _non-empty_ table**.
111-
--- Otherwise it will be flagged as a non-integer and the function will return `false`
112-
---
113-
--- * `multiple`: Tell the integer you're checking for multiple values. (Default: `false`).
114-
--- If set to `true`, every element of the table will be checked.
115-
--- If **any** element is not an integer, the function automatically returns false
123+
--- **Keep in mind that if `multiple` is set to `true`, this _MUST_ be a _non-empty_ table**.
124+
--- Otherwise it will be flagged as a non-integer and the function will return `false`.
116125
---
126+
--- - `multiple`: Tell the integer you're checking for multiple values. (Default: `false`).
127+
--- If set to `true`, every element of the table will be checked.
128+
--- If **any** element is not an integer, the function automatically returns false.
129+
--- ---
117130
--- ## Return
118-
--- A boolean value indicating whether the data is an integer or not
131+
---
132+
--- A boolean value indicating whether the data is an integer or not.
119133
--- ---
120134
---@field is_int fun(var: any, multiple: boolean?): boolean
121-
--- Returns whether a given string/number/table is "empty", including these scenarios:
122-
--- * Is an empty string (`x == ''`)
123-
--- * Is an integer equal to zero (`x == 0`)
124-
--- * Is an empty table
135+
--- Returns whether one or more given string/number/table are **empty**
136+
--- ---
137+
--- - Scenarios included if `multiple` is `false`:
138+
--- - Is an empty string (`x == ''`)
139+
--- - Is an integer equal to zero (`x == 0`)
140+
--- - Is an empty table (`{}`)
125141
---
142+
--- If `multiple` is `true` apply the above to a table of allowed values
143+
--- NOTE: **THE FUNCTION IS NOT RECURSIVE**
144+
--- ---
126145
--- ## Parameters
127-
--- * `v`: Must be either a string, number or a table.
128-
--- Otherwise you'll get complaints and the function will return `true`
129146
---
147+
--- - `v`: Must be either a string, number or a table.
148+
--- Otherwise you'll get complaints and the function will return `true`
149+
---
150+
--- - `multiple`: Tell the integer you're checking for multiple values. (Default: `false`).
151+
--- If set to `true`, every element of the table will be checked.
152+
--- If **any** element is not _empty_, the function automatically returns `false`.
153+
--- ---
130154
--- ## Returns
131-
--- A boolean indicatin whether input data is empty or not
155+
---
156+
--- A boolean indicatin whether input data is empty or not.
132157
--- ---
133-
---@field empty fun(v: string|table|number): boolean
158+
---@field empty fun(v: string|table|number|(string|table|number)[], multiple: boolean?): boolean
134159
---@field fields fun(fields: string|integer|(string|integer)[], T: table<string|integer, any>): boolean
135160
---@field tbl_values fun(values: any[], T: table, return_keys: boolean?): boolean|string|integer|(string|integer)[]
136161
---@field single_type_tbl fun(type_str: Types, T: table): boolean

0 commit comments

Comments
 (0)