Skip to content

Commit

Permalink
feat(api): New check.value function.
Browse files Browse the repository at this point in the history
  • Loading branch information
DrKJeff16 committed Jun 5, 2024
1 parent dfcdc76 commit 5cb93da
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lua/user/check/value.lua
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,41 @@ function M.tbl_values(values, T, return_keys)
return res
end

function M.single_type_tbl(type_str, T)
local is_str = M.is_str
local is_tbl = M.is_tbl
local empty = M.empty

local ALLOWED_TYPES = {
'boolean',
'function',
'nil',
'number',
'string',
'table',
'thread',
'userdata',
}

if not is_str(type_str) then
error('(user.check.value.single_type_tbl): You need to define a type as a string')
end

if not vim.tbl_contains(ALLOWED_TYPES, type_str) then
error('(user.check.value.single_type_tbl): `' .. type_str .. '` is not an allowed type')
end

if not is_tbl(T) or empty(T) then
error('(user.check.value.single_type_tbl): Expected a NON-EMPTY TABLE')
end

for _, v in next, T do
if type(v) ~= type_str then
return false
end
end

return true
end

return M
1 change: 1 addition & 0 deletions lua/user/types/user/check.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
---@field empty fun(v: string|table|number): boolean
---@field fields fun(fields: string|integer|(string|integer)[], T: table<string|integer, any>): boolean
---@field tbl_values fun(values: any[], T: table, return_keys: boolean?): boolean|string|integer|(string|integer)[]
---@field single_type_tbl fun(type_str: string, T: table): boolean

---@class UserCheck
---@field exists ExistanceCheck
Expand Down

0 comments on commit 5cb93da

Please sign in to comment.