Added util.typecheck function to ensure types in certain utility functions (fixes #78)
This commit is contained in:
parent
25af884c7e
commit
48522b8dcf
|
@ -15,9 +15,13 @@
|
|||
-- You should have received a copy of the GNU General Public License
|
||||
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
local util = require (wolfa_getLuaPath()..".util.util")
|
||||
|
||||
local bits = {}
|
||||
|
||||
function bits.hasbit(x, b)
|
||||
util.typecheck("bits.hasbit", {x, b}, {"number", "number"})
|
||||
|
||||
if b == 0 then
|
||||
return x == b
|
||||
end
|
||||
|
|
|
@ -15,9 +15,13 @@
|
|||
-- You should have received a copy of the GNU General Public License
|
||||
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
local util = require (wolfa_getLuaPath()..".util.util")
|
||||
|
||||
local pagination = {}
|
||||
|
||||
function pagination.calculate(count, limit, offset)
|
||||
util.typecheck("pagination.calculate", {count, limit, offset}, {"number", "number", "number"})
|
||||
|
||||
limit = limit or 30
|
||||
offset = offset or 0
|
||||
|
||||
|
|
|
@ -15,9 +15,13 @@
|
|||
-- You should have received a copy of the GNU General Public License
|
||||
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
local util = require (wolfa_getLuaPath()..".util.util")
|
||||
|
||||
local tables = {}
|
||||
|
||||
function tables.copy(tbl)
|
||||
util.typecheck("tables.contains", {tbl}, {"table"})
|
||||
|
||||
local copy = {}
|
||||
|
||||
for key, value in pairs(tbl) do
|
||||
|
@ -28,6 +32,8 @@ function tables.copy(tbl)
|
|||
end
|
||||
|
||||
function tables.unpack(tbl)
|
||||
util.typecheck("tables.contains", {tbl}, {"table"})
|
||||
|
||||
if table.unpack ~= nil then
|
||||
return table.unpack(tbl)
|
||||
elseif unpack ~= nil then
|
||||
|
@ -36,6 +42,8 @@ function tables.unpack(tbl)
|
|||
end
|
||||
|
||||
function tables.contains(tbl, needle)
|
||||
util.typecheck("tables.contains", {tbl}, {"table"})
|
||||
|
||||
for _, value in pairs(tbl) do
|
||||
if value == needle then
|
||||
return true
|
||||
|
@ -46,6 +54,8 @@ function tables.contains(tbl, needle)
|
|||
end
|
||||
|
||||
function tables.find(tbl, needle)
|
||||
util.typecheck("tables.contains", {tbl}, {"table"})
|
||||
|
||||
for key, value in pairs(tbl) do
|
||||
if value == needle then
|
||||
return key
|
||||
|
|
|
@ -19,6 +19,14 @@ local constants = require (wolfa_getLuaPath()..".util.constants")
|
|||
|
||||
local util = {}
|
||||
|
||||
function util.typecheck(func, args, types)
|
||||
for idx, arg in ipairs(args) do
|
||||
if type(arg) ~= types[idx] then
|
||||
error("bad argument #"..idx.." to '"..func.."' ("..types[idx].." expected, got "..type(arg)..")", 3)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function util.split(str, pat)
|
||||
local t = {} -- NOTE: use {n = 0} in Lua-5.0
|
||||
local fpat = "(.-)" .. pat
|
||||
|
@ -41,10 +49,14 @@ function util.split(str, pat)
|
|||
end
|
||||
|
||||
function util.escape(str)
|
||||
util.typecheck("util.escape", {str}, {"string"})
|
||||
|
||||
return string.gsub(str, "([\"'])", "\\%1")
|
||||
end
|
||||
|
||||
function util.removeColors(str)
|
||||
util.typecheck("util.removeColors", {str}, {"string"})
|
||||
|
||||
return string.gsub(str, "(^[%a%d%p])", "")
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue