• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lunarmodules / Penlight / 46279356752

29 Nov 2026 03:36PM UTC coverage: 83.225% (-7.1%) from 90.342%
46279356752

push

github

web-flow
Merge 0622ec222 into 44007e542

5125 of 6158 relevant lines covered (83.23%)

880.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

74.29
/lua/pl/types.lua
1
---- Dealing with Detailed Type Information
2

3
-- Dependencies `pl.utils`
4
-- @module pl.types
5

6
local utils = require 'pl.utils'
×
7
local math_ceil = math.ceil
×
8
local assert_arg = utils.assert_arg
×
9
local types = {}
×
10

11
do
12
    -- we prefer debug.getmetatable, but only if available
13
    local gmt = (debug or {}).getmetatable or getmetatable
×
14

15
    --- is the object either a function or a callable object?.
16
    -- @param obj Object to check.
17
    function types.is_callable (obj)
×
18
        if type(obj) == 'function' then
1,302 ✔
19
            return true
420 ✔
20
        end
21
        local mt = gmt(obj)
882 ✔
22
        if not mt then
882 ✔
23
            return false
616 ✔
24
        end
25
        return type(rawget(mt, "__call")) == "function"
266 ✔
26
    end
27
end
28

29
--- is the object of the specified type?.
30
-- Checks `type(obj) == tp` if `tp` is a string, otherwise checks exact
31
-- metatable equality (`tp == getmetatable(obj)`).
32
--
33
-- Because it only compares metatables directly, it does not detect inheritance
34
-- or subtyping (e.g. `types.is_type(OrderedMap(), Map)` returns `false`). For
35
-- Penlight class inheritance checks, use `Class:class_of(obj)` or
36
-- `obj:is_a(Class)` from `pl.class`.
37
--
38
-- NOTE: this function is imported from `utils.is_type`.
39
-- @param obj An object to check
40
-- @param tp The expected type name (string) or metatable
41
-- @return boolean
42
-- @function is_type
43
-- @see utils.is_type
44
-- @see pl.class
45
types.is_type = utils.is_type
×
46

47
local fileMT = getmetatable(io.stdout)
×
48

49
--- a string representation of a type.
50
-- For tables and userdata with metatables, we assume that the metatable has a `_name`
51
-- field. If the field is not present it will return 'unknown table' or
52
-- 'unknown userdata'.
53
-- Lua file objects return the type 'file'.
54
-- @param obj an object
55
-- @return a string like 'number', 'table', 'file' or 'List'
56
function types.type (obj)
×
57
    local t = type(obj)
174 ✔
58
    if t == 'table' or t == 'userdata' then
174 ✔
59
        local mt = getmetatable(obj)
146 ✔
60
        if mt == fileMT then
146 ✔
61
            return 'file'
42 ✔
62
        elseif mt == nil then
104 ✔
63
            return t
28 ✔
64
        else
65
            -- TODO: the "unknown" is weird, it should just return the type
66
            return mt._name or "unknown "..t
76 ✔
67
        end
68
    else
69
        return t
28 ✔
70
    end
71
end
72

73
--- is this number an integer?
74
-- @param x a number
75
-- @raise error if x is not a number
76
-- @return boolean
77
function types.is_integer (x)
×
78
    return math_ceil(x)==x
98 ✔
79
end
80

81
--- Check if the object is "empty".
82
-- An object is considered empty if it is:
83
--
84
-- - `nil`
85
-- - a table without any items (key-value pairs or indexes)
86
-- - a string with no content ("")
87
-- - not a nil/table/string
88
-- @param o The object to check if it is empty.
89
-- @param ignore_spaces If the object is a string and this is true the string is
90
-- considered empty if it only contains spaces.
91
-- @return `true` if the object is empty, otherwise a falsy value.
92
function types.is_empty(o, ignore_spaces)
×
93
    if o == nil then
168 ✔
94
        return true
14 ✔
95
    elseif type(o) == "table" then
154 ✔
96
        return next(o) == nil
42 ✔
97
    elseif type(o) == "string" then
112 ✔
98
        return o == "" or (not not ignore_spaces and (not not o:find("^%s+$")))
42 ✔
99
    else
100
        return true
70 ✔
101
    end
102
end
103

104
local function check_meta (val)
105
    if type(val) == 'table' then return true end
7,090 ✔
106
    return getmetatable(val)
148 ✔
107
end
108

109
--- is an object 'array-like'?
110
-- An object is array like if:
111
--
112
-- - it is a table, or
113
-- - it has a metatable with `__len` and `__index` methods
114
--
115
-- NOTE: since `__len` is 5.2+, on 5.1 is usually returns `false` for userdata
116
-- @param val any value.
117
-- @return `true` if the object is array-like, otherwise a falsy value.
118
function types.is_indexable (val)
×
119
    local mt = check_meta(val)
2,966 ✔
120
    if mt == true then return true end
2,966 ✔
121
    return mt and mt.__len and mt.__index and true
40 ✔
122
end
123

124
--- can an object be iterated over with `pairs`?
125
-- An object is iterable if:
126
--
127
-- - it is a table, or
128
-- - it has a metatable with a `__pairs` meta method
129
--
130
-- NOTE: since `__pairs` is 5.2+, on 5.1 is usually returns `false` for userdata
131
-- @param val any value.
132
-- @return `true` if the object is iterable, otherwise a falsy value.
133
function types.is_iterable (val)
×
134
    local mt = check_meta(val)
4,028 ✔
135
    if mt == true then return true end
4,254 ✔
136
    return mt and mt.__pairs and true
54 ✔
137
end
138

139
--- can an object accept new key/pair values?
140
-- An object is iterable if:
141
--
142
-- - it is a table, or
143
-- - it has a metatable with a `__newindex` meta method
144
--
145
-- @param val any value.
146
-- @return `true` if the object is writeable, otherwise a falsy value.
147
function types.is_writeable (val)
×
148
    local mt = check_meta(val)
96 ✔
149
    if mt == true then return true end
96 ✔
150
    return mt and mt.__newindex and true
54 ✔
151
end
152

153
-- Strings that should evaluate to true.   -- TODO: add on/off ???
154
local trues = { yes=true, y=true, ["true"]=true, t=true, ["1"]=true }
×
155
-- Conditions types should evaluate to true.
156
local true_types = {
×
157
    boolean=function(o, true_strs, check_objs) return o end,
28 ✔
158
    string=function(o, true_strs, check_objs)
159
        o = o:lower()
306 ✔
160
        if trues[o] then
238 ✔
161
            return true
98 ✔
162
        end
163
        -- Check alternative user provided strings.
164
        for _,v in ipairs(true_strs or {}) do
154 ✔
165
            if type(v) == "string" and o == v:lower() then
72 ✔
166
                return true
42 ✔
167
            end
168
        end
169
        return false
98 ✔
170
    end,
171
    number=function(o, true_strs, check_objs) return o ~= 0 end,
56 ✔
172
    table=function(o, true_strs, check_objs) if check_objs and next(o) ~= nil then return true end return false end
84 ✔
173
}
174
--- Convert to a boolean value.
175
-- True values are:
176
--
177
-- * boolean: true.
178
-- * string: 'yes', 'y', 'true', 't', '1' or additional strings specified by `true_strs`.
179
-- * number: Any non-zero value.
180
-- * table: Is not empty and `check_objs` is true.
181
-- * everything else: Is not `nil` and `check_objs` is true.
182
--
183
-- @param o The object to evaluate.
184
-- @param[opt] true_strs optional Additional strings that when matched should evaluate to true. Comparison is case insensitive.
185
-- This should be a List of strings. E.g. "ja" to support German.
186
-- @param[opt] check_objs True if objects should be evaluated.
187
-- @return true if the input evaluates to true, otherwise false.
188
function types.to_bool(o, true_strs, check_objs)
×
189
    local true_func
190
    if true_strs then
434 ✔
191
        assert_arg(2, true_strs, "table")
42 ✔
192
    end
193
    true_func = true_types[type(o)]
434 ✔
194
    if true_func then
434 ✔
195
        return true_func(o, true_strs, check_objs)
406 ✔
196
    elseif check_objs and o ~= nil then
28 ✔
197
        return true
14 ✔
198
    end
199
    return false
14 ✔
200
end
201

202

203
return types
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc