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
|
-- You should have received a copy of the GNU General Public License
|
||||||
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
local util = require (wolfa_getLuaPath()..".util.util")
|
||||||
|
|
||||||
local bits = {}
|
local bits = {}
|
||||||
|
|
||||||
function bits.hasbit(x, b)
|
function bits.hasbit(x, b)
|
||||||
|
util.typecheck("bits.hasbit", {x, b}, {"number", "number"})
|
||||||
|
|
||||||
if b == 0 then
|
if b == 0 then
|
||||||
return x == b
|
return x == b
|
||||||
end
|
end
|
||||||
|
@ -25,4 +29,4 @@ function bits.hasbit(x, b)
|
||||||
return x % (b + b) >= b
|
return x % (b + b) >= b
|
||||||
end
|
end
|
||||||
|
|
||||||
return bits
|
return bits
|
||||||
|
|
|
@ -15,9 +15,13 @@
|
||||||
-- You should have received a copy of the GNU General Public License
|
-- You should have received a copy of the GNU General Public License
|
||||||
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
local util = require (wolfa_getLuaPath()..".util.util")
|
||||||
|
|
||||||
local pagination = {}
|
local pagination = {}
|
||||||
|
|
||||||
function pagination.calculate(count, limit, offset)
|
function pagination.calculate(count, limit, offset)
|
||||||
|
util.typecheck("pagination.calculate", {count, limit, offset}, {"number", "number", "number"})
|
||||||
|
|
||||||
limit = limit or 30
|
limit = limit or 30
|
||||||
offset = offset or 0
|
offset = offset or 0
|
||||||
|
|
||||||
|
|
|
@ -15,9 +15,13 @@
|
||||||
-- You should have received a copy of the GNU General Public License
|
-- You should have received a copy of the GNU General Public License
|
||||||
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
local util = require (wolfa_getLuaPath()..".util.util")
|
||||||
|
|
||||||
local tables = {}
|
local tables = {}
|
||||||
|
|
||||||
function tables.copy(tbl)
|
function tables.copy(tbl)
|
||||||
|
util.typecheck("tables.contains", {tbl}, {"table"})
|
||||||
|
|
||||||
local copy = {}
|
local copy = {}
|
||||||
|
|
||||||
for key, value in pairs(tbl) do
|
for key, value in pairs(tbl) do
|
||||||
|
@ -28,6 +32,8 @@ function tables.copy(tbl)
|
||||||
end
|
end
|
||||||
|
|
||||||
function tables.unpack(tbl)
|
function tables.unpack(tbl)
|
||||||
|
util.typecheck("tables.contains", {tbl}, {"table"})
|
||||||
|
|
||||||
if table.unpack ~= nil then
|
if table.unpack ~= nil then
|
||||||
return table.unpack(tbl)
|
return table.unpack(tbl)
|
||||||
elseif unpack ~= nil then
|
elseif unpack ~= nil then
|
||||||
|
@ -36,6 +42,8 @@ function tables.unpack(tbl)
|
||||||
end
|
end
|
||||||
|
|
||||||
function tables.contains(tbl, needle)
|
function tables.contains(tbl, needle)
|
||||||
|
util.typecheck("tables.contains", {tbl}, {"table"})
|
||||||
|
|
||||||
for _, value in pairs(tbl) do
|
for _, value in pairs(tbl) do
|
||||||
if value == needle then
|
if value == needle then
|
||||||
return true
|
return true
|
||||||
|
@ -46,6 +54,8 @@ function tables.contains(tbl, needle)
|
||||||
end
|
end
|
||||||
|
|
||||||
function tables.find(tbl, needle)
|
function tables.find(tbl, needle)
|
||||||
|
util.typecheck("tables.contains", {tbl}, {"table"})
|
||||||
|
|
||||||
for key, value in pairs(tbl) do
|
for key, value in pairs(tbl) do
|
||||||
if value == needle then
|
if value == needle then
|
||||||
return key
|
return key
|
||||||
|
|
|
@ -19,6 +19,14 @@ local constants = require (wolfa_getLuaPath()..".util.constants")
|
||||||
|
|
||||||
local util = {}
|
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)
|
function util.split(str, pat)
|
||||||
local t = {} -- NOTE: use {n = 0} in Lua-5.0
|
local t = {} -- NOTE: use {n = 0} in Lua-5.0
|
||||||
local fpat = "(.-)" .. pat
|
local fpat = "(.-)" .. pat
|
||||||
|
@ -41,10 +49,14 @@ function util.split(str, pat)
|
||||||
end
|
end
|
||||||
|
|
||||||
function util.escape(str)
|
function util.escape(str)
|
||||||
|
util.typecheck("util.escape", {str}, {"string"})
|
||||||
|
|
||||||
return string.gsub(str, "([\"'])", "\\%1")
|
return string.gsub(str, "([\"'])", "\\%1")
|
||||||
end
|
end
|
||||||
|
|
||||||
function util.removeColors(str)
|
function util.removeColors(str)
|
||||||
|
util.typecheck("util.removeColors", {str}, {"string"})
|
||||||
|
|
||||||
return string.gsub(str, "(^[%a%d%p])", "")
|
return string.gsub(str, "(^[%a%d%p])", "")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue