Module:Functions

local p = {} local function range(a, b, step) if not b then b = a   a = 1 end step = step or 1 local f = step > 0 and function(_, lastvalue) local nextvalue = lastvalue + step if nextvalue <= b then return nextvalue end end or   step < 0 and function(_, lastvalue) local nextvalue = lastvalue + step if nextvalue >= b then return nextvalue end end or     function(_, lastvalue) return lastvalue end return f, nil, a - step end local function split(inputstr, sep) if sep == nil then sep = "%s" end local t={} for str in string.gmatch(inputstr, "([^"..sep.."]+)") do               table.insert(t, str) end return t end local function round(val, decimal) if (decimal) then return math.floor( (val * 10^decimal) + 0.5) / (10^decimal) else return math.floor(val+0.5) end end local function numbers(number) local lang = mw.language.getContentLanguage:getCode local fN = require('Dev:Formatnum').formatNum return fN(tonumber(number), lang) end function compareLess(x, y)   return x[1] < y[1] end function compareMore(x, y)   return x[1] > y[1] end function tableSort(table1,table2,more) for i,v in ipairs(table1) do       table1[i] = {table1[i], table2[i]} end if more == 'less' then table.sort(table1, compareLess) else table.sort(table1, compareMore) end for i, v in ipairs(table1) do       table1[i] = v[1] table2[i] = v[2] end return table2 end

function upperFirstLetter(str) return (str:gsub("^%l", string.upper)) end

function noPage(frame, pagename, content) return frame:expandTemplate{title = "createpageifnotex", args={ NS_MAIN = pagename, NS_TEMPLATE = content }} end

function time(seconds) local seconds = tonumber(seconds) if seconds <= 0 then return "0S"; else toReturn = "" days = string.format("%01.f", math.floor(seconds/86400)); hours = string.format("%01.f", math.floor(seconds/3600 - (days*24))); mins = string.format("%01.f", math.floor(seconds/60 - (hours*60))); secs = string.format("%01.f", math.floor(seconds - hours*3600 - mins *60)); if(tonumber(days)>0) then toReturn = toReturn..days.."D "; end if(tonumber(hours)>0) then toReturn = toReturn..hours.."H "; end if(tonumber(mins)>0) then toReturn = toReturn..mins.."M "; end if (tonumber(secs)>0) then toReturn = toReturn..secs.."S"; end return toReturn; end end

function sortTable(o) local function padnum(d) local dec, n = string.match(d, "(%.?)0*(.+)") return #dec > 0 and ("%.12f"):format(d) or ("%s%03d%s"):format(dec, #n, n) end table.sort(o, function(a,b)   return tostring(a):gsub("%.?%d+",padnum)..("%3d"):format(#b)         < tostring(b):gsub("%.?%d+",padnum)..("%3d"):format(#a) end) return o end

function sortTableKeys(t) local tkeys = {} for k in pairs(t) do table.insert(tkeys, k) end return sortTable(tkeys) end

function hasValue(value) if(value ~= nil) then return true end return false end p.range=range p.split=split p.round=round p.numbers=numbers p.compareLess=compareLess p.compareMore=compareMore p.tableSort=tableSort p.upfirst = upperFirstLetter p.noPage = noPage p.time = time p.sortKeys = sortTableKeys p.sortTable = sortTable p.hasValue = hasValue return p