--------------------------------------------------------- --- Utils library for ZD --------------------------------------------------------- local string = require('string') require('./string.lua')(string) local os = require('os') return function(ZD) -- Simple console output with a [ZD] tag and better tabbing. Used internal. ZD.cout = function(...) local output = "[ZD " .. tostring(os.date()) .. "] " for k,v in pairs({...}) do output = output .. tostring(v) .. " " end return print(output) end -- Input: tbl, lvl = 0, callback -- Example: debugTable(_G) -- Prints out the table + all sub-tables in an easily readable form. -- You can pass a callback function that logs the table instead. ZD.debugTable = function(tbl, lvl, func) if type(tbl) ~= "table" then tbl = {} end func = func or print lvl = lvl or 0 for k,v in pairs(tbl) do local space = "" for i = 1, lvl do space = space .. " " end func(space .. tostring(k) .. ": " .. tostring(v)) if type(v) == "table" then ZD.debugTable(v, lvl + 1, func) end end end ZD.hashTable = function(tbl) local string = "" iterate = function(tbl) if type(tbl) ~= "table" then tbl = {} end func = func or print lvl = lvl or 0 for k,v in pairs(tbl) do string = string .. tostring(k) .. ":" .. tostring(v) .. "/" if type(v) == "table" then iterate(v) end end end iterate(tbl) return ZD.md5.sumhexa(string) end -- Input: url = "/index.lua?k=5&v=test" -- Output: {path = "/index.lua", get={k=5, v="test"}, ending = "lua"} ZD.parseURL = function(url) local _url = string.split(url, "?") local path = _url[1] or url local ending = string.split(path, ".") ending = ending[#ending] local _get = string.split(_url[2] or "", "&") local get = {} for k,v in pairs(_get) do local arg = string.split(v, "=") if arg[1] and arg[2] then get[arg[1]] = arg[2] end end return {path = path, get = get, ending = ending} end end