From 5cb93dad2fa53c71f3a220fb5deda4cefd1b2104 Mon Sep 17 00:00:00 2001 From: Guennadi Maximov C Date: Wed, 5 Jun 2024 01:44:27 -0600 Subject: [PATCH] feat(api): New `check.value` function. --- lua/user/check/value.lua | 37 +++++++++++++++++++++++++++++++++++ lua/user/types/user/check.lua | 1 + 2 files changed, 38 insertions(+) diff --git a/lua/user/check/value.lua b/lua/user/check/value.lua index f6ee6483..2233e3fe 100644 --- a/lua/user/check/value.lua +++ b/lua/user/check/value.lua @@ -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 diff --git a/lua/user/types/user/check.lua b/lua/user/types/user/check.lua index 9b97bee9..431d312d 100644 --- a/lua/user/types/user/check.lua +++ b/lua/user/types/user/check.lua @@ -24,6 +24,7 @@ ---@field empty fun(v: string|table|number): boolean ---@field fields fun(fields: string|integer|(string|integer)[], T: table): 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