Version 2.01 : - correction de deep_print(<table>) Version 2.02 : - amélioration du deep_print(<table>)

-- tools.isNumber(num)
--	  // tools.isNumber(20) -- return true
--	  // tools.isNumber("20") -- return true
-- tools.iif(condition, messageTrue, messageFalse)
--	  // tools.iif(false, "OK", "KO") -- return "KO"
-- tools.cut(message, 10)
--	  // tools.cut("1234567890", 6) -- return "123..." (6 caractère max)
--	  // tools.cut("123456", 6) -- return "123456" (6 caractère max)
-- tools.isNil(variable)
--	  // tools.isNil(coucou) -- return true
-- tools.isNotNil(variable)
--	  // tools.isNotNil(coucou) -- return false
-- tools.split(s:string, delimiter: string)     
--        // tools.split("Comment;Allez;Vous", ";") -- return table {1="Comment", 2="Allez", 3="Vous"}
-- tools.trim(s:string)     
--        // tools.trim("   Comment   ") -- return "Comment"
-- tools.tostring(string | boolean | table | ...)   
--        // tools.tostring({Value=2})  -- return "{'value': 2}"
--        // tools.tostring(true)       -- return "true"
--        // tools.tostring(100)        -- return "1000"
-- tools.log(message: string [, color: string] [, force: boolean])
--        // tools.log("Message", "red", true)  -- display "Message" in red in the debug window
-- tools.info(message: string [, color: string])
--        // tools.info("Message")      -- always display "Message" in white in the debug window
-- tools.error(message: string [, color: string])
--        // tools.error("Message")      -- always display "Message" in red in the debug window
-- tools.warning(message: string [, color: string])
--        // tools.warning("Message")      -- always display "Message" in orange in the debug window
-- tools.debug(message: string [, color: string])
--        // tools.debug("Message")      -- display "Message" in gray in the debug window if debug = true only
-- tools.deep_print(table)
--        // tools.deep_print({1="Comment", 2="Allez", 3="Vous"})   -- print line by line in the debug window
-- tools.getStringTime([seconds: boolean])
--        // tools.getStringTime(true)  -- return "20:35:59"
--        // tools.getStringTime()      -- return "20:35"
-- tools.getStringDate()
--        // tools.getStringDate()      -- return "23/09/2016"
-- tools.toTime(hour: String)
--        // result = tools.toTime("20:35") -- get os.time() object corresponding to the next 20:35 hours

Original :

tools.lua
if (not tools) then 
  tools = { 
    version = "2.02",
    addstyle = "",
    isdebug = false,
    log = function(msg, color, force) 
      msg = tools.tostring(msg)
      for spacestodo, nombre in string.gmatch(msg, "(#spaces(%d+)#)") do
        local dots = ""
        for i = 1, nombre do
          dots = dots .. "."
        end
        msg = string.gsub(msg, spacestodo, "<span style=\"color:black;"..tools.addstyle.."\">"..dots .."</span>")
      end
      if (tools.isdebug or force) then 
        fibaro:debug("<span style=\"color:"..(color or "white")..";"..tools.addstyle.."\">"..msg.."</span>") 
      end 
    end,
    error = function(msg, color) tools.log(msg, color or "red", true) end,
    warning = function(msg, color) tools.log(msg, color or "orange", true) end,
    info = function(msg, color) tools.log(msg, color or "white", true) end,
    debug = function(msg, color) tools.log(msg, color or "gray", false) end,
    tostring = function(what)
      if (type(what) == "boolean") then
        if (what) then return "true" else return "false" end
      elseif (type(what) == "table") then
        if (json) then
          return json.encode(what)
        else
          return "table found"
        end
      else
        return tostring(what)
      end
    end,
    split = function(text, sep)
      local sep, fields = sep or ":", {}
      local pattern = string.format("([^%s]+)", sep)
      text:gsub(pattern, function(c) fields[#fields+1] = c end)
      return fields
    end,
    trim = function(s)
      return (s:gsub("^%s*(.-)%s*$", "%1"))
    end,
    collect_keys = function(t, sort) local _k = {} for k in pairs(t) do _k[#_k+1] = k end table.sort(_k, sort) return _k end,
    sortedPairs = function(t, sort) local keys = tools.collect_keys(t, sort) local i = 0 return function() i = i + 1 if keys[i] then return keys[i], t[keys[i]] end end end,
    deep_print = function(tableau, parent)
      local par = parent or ""
      for k,v in tools.sortedPairs(tableau) do
        local res = par .. "-- " .. k
        print(res)
        if (type(v) == "table") then
          tools.deep_print(v, par .. "  ")
        end
      end
    end,
    iif = function(condition, messagetrue, messagefalse)
      if (condition) then return messagetrue else return messagefalse end
    end, 
    cut = function(message, max)
      max = max or 10
      if (max < message:len()) then return message:sub(1, max-3) .. "..." end
      return message
    end,   
    isNumber = function(num)
      if (type(num)=="number") then return true end
      if (type(num)=="string") then return type(tonumber(num))=="number" end
      return false
    end,
    getStringTime = function(seconds)
      if (seconds) then return os.date("%H:%M:%S") end
      return os.date("%H:%M")
    end,
    toTime = function(hour) 
      local h,m = string.match(hour, "(%d+):(%d+)")
      local d = os.date("*t")
      local t = os.time{year=d.year, month=d.month, day=d.day, hour=h, min=m, sec=0}
      if (t < os.time()) then
        t= os.time{year=d.year, month=d.month, day=(d.day+1), hour=h, min=m, sec=0}
      end
      return t      
    end,
    getStringDate = function()
      return os.date("%d/%m/%Y")
    end,
    isNil = function(variable) 
      return type(variable) == "nil"
    end,
    isNotNil = function(variable) 
      return not tools.isNil(variable)
    end,
  }
end

Version minifiée

toolsminifier.lua
if not tools then tools={version="2.02",addstyle="",isdebug=false,log=function(a,b,c)a=tools.tostring(a)for d,e in string.gmatch(a,"(#spaces(%d+)#)")do local f=""for g=1,e do f=f.."."end;a=string.gsub(a,d,"<span style=\"color:black;"..tools.addstyle.."\">"..f.."</span>")end;if tools.isdebug or c then fibaro:debug("<span style=\"color:"..(b or"white")..";"..tools.addstyle.."\">"..a.."</span>")end end,error=function(a,b)tools.log(a,b or"red",true)end,warning=function(a,b)tools.log(a,b or"orange",true)end,info=function(a,b)tools.log(a,b or"white",true)end,debug=function(a,b)tools.log(a,b or"gray",false)end,tostring=function(h)if type(h)=="boolean"then if h then return"true"else return"false"end elseif type(h)=="table"then if json then return json.encode(h)else return"table found"end else return tostring(h)end end,split=function(i,j)local j,k=j or":",{}local l=string.format("([^%s]+)",j)i:gsub(l,function(m)k[#k+1]=m end)return k end,trim=function(n)return n:gsub("^%s*(.-)%s*$","%1")end,collect_keys=function(o,p)local q={}for r in pairs(o)do q[#q+1]=r end;table.sort(q,p)return q end,sortedPairs=function(o,p)local s=tools.collect_keys(o,p)local g=0;return function()g=g+1;if s[g]then return s[g],o[s[g]]end end end,deep_print=function(t,u)local v=u or""for r,w in tools.sortedPairs(t)do local x=v.."-- "..r;print(x)if type(w)=="table"then tools.deep_print(w,v.."  ")end end end,iif=function(y,z,A)if y then return z else return A end end,cut=function(B,C)C=C or 10;if C<B:len()then return B:sub(1,C-3).."..."end;return B end,isNumber=function(D)if type(D)=="number"then return true end;if type(D)=="string"then return type(tonumber(D))=="number"end;return false end,getStringTime=function(E)if E then return os.date("%H:%M:%S")end;return os.date("%H:%M")end,toTime=function(F)local G,H=string.match(F,"(%d+):(%d+)")local I=os.date("*t")local o=os.time{year=I.year,month=I.month,day=I.day,hour=G,min=H,sec=0}if o<os.time()then o=os.time{year=I.year,month=I.month,day=I.day+1,hour=G,min=H,sec=0}end;return o end,getStringDate=function()return os.date("%d/%m/%Y")end,isNil=function(J)return type(J)=="nil"end,isNotNil=function(J)return not tools.isNil(J)end}end