Board Thread:Lua Help/@comment-24473195-20140820121701

-- Gets Parent/child frame function p.getFrame(bParent) local oFrame = mw.getCurrentFrame if bParent and oFrame~=nil and oFrame:getParent then oFrame = mw.getCurrentFrame:getParent end return oFrame end

-- Put all arguments into a real table function p.getArgs(frame,bParent) local t = {} local tTmp = frame.args if bParent then tTmp = frame:getParent.args end for name, value in pairs( tTmp ) do    t[name] = value end return t end

--Check if args are enough for script to run to avoid "script error messages" function p.checkArgs(frame, iArgs) local iCount =0 local tTmp = frame.args if frame.args[1]==nil then tTmp = frame:getParent.args end local s ='' for name, value in pairs( tTmp ) do    iCount = iCount + 1 s = s ..' '..name .. ' '.. value mw.log(iCount .. ' ' ..iArgs) if (iCount) == tonumber(iArgs) then mw.log('true') return true end end return false end

--Check if date is in appropriate format --Iso YYYY-MM-DD function p.checkDate(date) local pat="(%d+)-(%d+)-(%d+)" local year,month,day=date:match(pat) if year and month and day then return true 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( date1, date2) if p.checkDate(date1) and p.checkDate(date2) then date1 = string.sub(date1,0,10) date2 = string.sub(date2,0,10) local tDate1 =p.getTime(date1) local tDate2 =p.getTime(date2) local days = math.abs((os.difftime(tDate1,tDate2))/3600/24) return days end return 0 end

--Get time using Iso format ((YYYY-MM-DD) function p.getTime(date) local pat="(%d+)-(%d+)-(%d+)"  local year,month,day=date:match(pat)  local date1=(os.time({month=month,year=year,day=day}) )  return date1 end --

I think it'd be useful to have a list of common functions used when interacting with lua templates. Feel free to make any tweaks and share them, or share your own functions. 