Skip to content

Commit

Permalink
Added util.typecheck function to ensure types in certain utility func…
Browse files Browse the repository at this point in the history
…tions (fixes #78)
  • Loading branch information
Timo Smit committed Feb 13, 2018
1 parent 25af884 commit 48522b8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
6 changes: 5 additions & 1 deletion luamods/wolfadmin/util/bits.lua
Expand Up @@ -15,14 +15,18 @@
-- 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

return x % (b + b) >= b
end

return bits
return bits
4 changes: 4 additions & 0 deletions luamods/wolfadmin/util/pagination.lua
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions luamods/wolfadmin/util/tables.lua
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions luamods/wolfadmin/util/util.lua
Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit 48522b8

Please sign in to comment.