Module:Utility: Difference between revisions

From The Satanic Wiki
Jump to navigation Jump to search
m (1 revision imported)
No edit summary
 
Line 164: Line 164:
--: (string) A printed lua table
--: (string) A printed lua table
function p.table_print (tablename)
function p.table_print (tablename)
     return require("Dev:Inspect").inspect(tablename)
     return require("Module:Inspect").inspect(tablename)
end
end
--% Gets debug information for a function  using lua xpcall
--% Gets debug information for a function  using lua xpcall
Line 192: Line 192:
--: (string) Prints out nowiki of first argument
--: (string) Prints out nowiki of first argument
function p.nowiki (frame)
function p.nowiki (frame)
     local args = require("Dev:Arguments").getArgs(frame)
     local args = require("Module:Arguments").getArgs(frame)
     return mw.text.nowiki (args[1] )
     return mw.text.nowiki (args[1] )
end
end

Latest revision as of 06:10, 2 May 2021

Documentation for this module may be created at Module:Utility/doc

--<pre> //
--| A library with many miscellaneous functions
p = {}

--% Put all arguments into a real table - Returns Parent, child args table
--@ frame (table) A frame object
--@ targetArgs (string) the target arguments to return, possible values ("parent", "child", "both"
--: (table) A table containing arguments
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
--% This is meant to fetch a args table
--@ tTmp (table) The template args from the module {{#invoke
--: (table) A table containing arguments  or nil
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
--@ frame (table) Scribunto frame object
--@ iArgs (number) Number of arguments required to run checkArgs
--@ bParent (boolean) Checks parent (true) or child arguments 
--: (boolean) True 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
--@ sDate (string) Date string e.g. 2012-01-02, YYYY-MM-DD
--@ iDays (number) Number of days to add or subtract (negative or positive)
--@ bReturnTable (boolean) Set to true to returns a lua date table
--@ sFormat (string) specific format for date 
--: (string) A calculated 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
--@ sDate (string) Date string e.g. 2012-01-02, YYYY-MM-DD
--@ sPat (string) A lua pattern for a date, default is (%d%d%d%d)-(%d%d)-(%d%d)
--: (boolean) True for valid date

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
--@ tTab (table) A valid table
--: (boolean) true for empty
function p.isempty(tTab)
    if next(tTab) == nil then
        return true
    end
    return false
end

--% Round a number
--@ num (number) A number
--@ idp (number) Decimal points
--: (number) Rounded number 

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

--% Calculate difference between two dates
--@ sDate1 (string) A string containing a date
--@ sDate2 (string) A string containing a date
--@ isAbsolute (boolean) absolute number (no negatives)
--: (number) Difference between days number or null
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 lua timestamp (os.time) using custom format
--@ sDate (string) A string containing a date
--@ sPat (string) A lua pattern, default: "(%d%d%d%d)-(%d%d)-(%d%d)"
--: (table) Lua os.time
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
--% Get a printed table
--@ tablename (table) A simple lua table
--: (string) A printed lua table
function p.table_print (tablename)
    return require("Module:Inspect").inspect(tablename)
end
--% Gets debug information for a function  using lua xpcall
--@ oFunc (function) A function to debug
--@ bShowErr (boolean) True to show error
--: (string) A stacktrace containing error details
function p.debug(oFunc, bShowErr)
     local bStatus, bResults = xpcall(oFunc, debug.traceback)
     
     if (not(bShowErr) or bStatus) then
        return bResults
     end
end
--% Gets variable content 
--@ oData (variable) A variable to output information (e.g. a table {})
--: (string) Prints out variable content
function p.log(oData)
    local sOutput = oData
    if type(oData)=="table" then
        sOutput = p.table_print(oData,4) 
    end
    mw.log(sOutput)
    return sOutput
end
--% Performs a nowiki on content. Same as using mw.text.nowiki on the first argument.
--@ frame (table) Scribunto frame
--: (string) Prints out nowiki of first argument
function p.nowiki (frame)
    local args = require("Module:Arguments").getArgs(frame)
    return mw.text.nowiki (args[1] )
end

return p
-- [[Category:Lua modules]]