Module:Utility

-- //Contains many miscellaneous functions p = {}

-- Put all arguments into a real table - Returns Parent, child args table function p.getArgs(frame,targetArgs) if frame and type(frame)=="table" then local tParent = frame:getParent local tChild = p.fetchArgs(frame.args or nil) if frame  then tParent = p.fetchArgs(tParent.args or nil) end if targetArgs =="parent" then return tParent elseif targetArgs =="child" then return tChild elseif targetArgs =="both" then return tParent, tChild end return tParent or tChild end end

function p.fetchArgs(tTmp) if not(tTmp) then return end for name, value in pairs( tTmp ) do        if(name) then -- This is meant to avoid a read-only meta-table with no table functions return mw.clone(tTmp) end end end

--Check if args are enough for script to run function p.checkArgs(frame, iArgs, bParent) local iCount =0 local tParent, tChild = p.getArgs(frame) local tArgTable = tParent

if not(bParent) then tArgTable = tChild end

for name, value in pairs(tArgTable) do         iCount = iCount + 1 if (iCount) == tonumber(iArgs) then return true end end return false end

-- add days to date function p.addDays(sDate,iDays,bReturnTable, sFormat) if p.checkdate(sDate) and iDays and tonumber(iDays) then local iDayToSecs = iDays * 24 * 60 *60 if (bReturnTable) then return os.date("*t",p.getdate(sDate,sFormat) + iDayToSecs) end return os.date("%Y-%m-%d",p.getdate(sDate,sFormat) + iDayToSecs) end return sDate end

--Check if date is in appropriate format --Iso YYYY-MM-DD function p.checkdate(sDate,sPat) if sDate then local sPat=sPat or "(%d%d%d%d)-(%d%d)-(%d%d)" local year,month,day=sDate:match(sPat) if year and month and day then return true end end return false end

-- Check if table is empty

function p.isempty(table) if next(table) == nil then return true end return false end

-- round a number

function p.round(num, idp) return tonumber(string.format("%." .. (idp or 0) .. "f", num)) end

-- Calculate difference between two dates function p.datediff( sDate1, sDate2,isAbsolute) if p.checkdate(sDate1) and p.checkdate(sDate2) then sDate1 = string.sub(sDate1,0,10) sDate2 = string.sub(sDate2,0,10) local tDate1 =p.getdate(sDate1) local tDate2 =p.getdate(sDate2) local iDays = (os.difftime(tDate1,tDate2))/3600/24 if isAbsolute then iDays = math.abs(iDays) end return iDays end -- return 0 end

--Get time using custom format function p.getdate(sDate,sPat) if not(sDate) then sDate = os.date("%Y-%m-%d") end local sPat=sPat or "(%d%d%d%d)-(%d%d)-(%d%d)" local year,month,day=sDate:match(sPat) local tDate if year and month and day then tDate = os.time({month=month,year=year,day=day}) end return tDate end

-- function p.table_print (tablename) return require("Dev:Inspect").inspect(tablename) end

function p.debug(oFunc,bShowErr) local bStatus,bResults =xpcall(oFunc, debug.traceback) if (not(bShowErr) or bStatus) then return bResults end end

function p.log(oData) local sOutput = oData if type(oData)=="table" then sOutput = p.table_print(oData,4) end return sOutput end

function p.module_exists(inputData) local name = "" if type(inputData)=="table" and inputData.args then name = inputData.args[1] or inputData.args.name if inputData.getParent then local parent = inputData.getParent name = parent.args[1] or parent.args.name end end if type (inputData) == "string" then name = inputData end if not name:match(":") then name = "Module:"..name end

if package.loaded[name] then return true else for _, searcher in ipairs(package.searchers or package.loaders) do     local loader = searcher(name) if type(loader) == 'function' then return true end end return false end end

function p.makePseudoFrame(frame,childArgs,parentArgs) if type(frame) ~= "table" then error("frame must be a table") end

local pseudoFrame = {}

for name in pairs(frame) do       pseudoFrame[name] = function (pseudoFrame, ...) local args = unpack(arg)

return frame[name](frame, args) end end

pseudoFrame.args = childArgs pseudoFrame.parentArgs = pseudoFrame.parentArgs or parentArgs

pseudoFrame.getParent = function (pseudoFrame) local tmpFrame = nil if frame:getParent and pseudoFrame.parentArgs==nil then tmpFrame = p.makePseudoFrame(frame:getParent,pseudoFrame.parentArgs) end return tmpFrame end return pseudoFrame end

function p.testframe(frame) local fakeFrame = p.makePseudoFrame(frame,{1,2,3},{4,5,6}) -- return p.log(fakeFrame.args) ..' '.. return p.log(fakeFrame.getParent) end return p --