Module:Arguments: Difference between revisions

    From Nonbinary Wiki
    m>Mr. Stradivarius
    (don't use "if frame == mw.getCurrentFrame()")
    m>Mr. Stradivarius
    (memoize nils, add type checks for frame and options, use string library functions instead of mw.text.trim and mw.ustring.find, define four different tidyVal functions to avoid checking options every time)
    Line 1: Line 1:
    -- This module provides easy processing of arguments passed to Scribunto from #invoke.
    -- This module provides easy processing of arguments passed to Scribunto from #invoke.
    -- It is intended for use by other Lua modules, and should not be called from #invoke directly.
    -- It is intended for use by other Lua modules, and should not be called from #invoke directly.
    local libraryUtil = require('libraryUtil')
    local checkType = libraryUtil.checkType


    local arguments = {}
    local arguments = {}
    local nilArg = {} -- Used for memoizing nil arguments in metaArgs.
    -- Generate four different tidyVal functions, so that we don't have to check the options every time we call it.
    local function tidyValDefault(key, val)
    if type(val) == 'string' then
    val = val:match('^%s*(.-)%s*$')
    if val == '' then
    return nil
    else
    return val
    end
    else
    return val
    end
    end
    local function tidyValTrimOnly(key, val)
    if type(val) == 'string' then
    return val:match('^%s*(.-)%s*$')
    else
    return val
    end
    end
    local function tidyValRemoveBlanksOnly(key, val)
    if type(val) == 'string' then
    if val:find('%S') then
    return val
    else
    return nil
    end
    else
    return val
    end
    end
    local function tidyValNoChange(key, val)
    return val
    end


    function arguments.getArgs(frame, options)
    function arguments.getArgs(frame, options)
    options = type(options) == 'table' and options or {}
    checkType('getArgs', 1, frame, 'table', true)
    checkType('getArgs', 2, options, 'table', true)
    frame = frame or {}
    options = options or {}


    -- Get the arguments from the frame object if available. If the frame object is not available, we are being called
    -- Get the arguments from the frame object if available. If the frame object is not available, we are being called
    -- from another Lua module or from the debug console, so put the args in a special table so we can differentiate them.
    -- from another Lua module or from the debug console, so put the args in a special table so we can differentiate them.
    local fargs, pargs, luaArgs
    local fargs, pargs, luaArgs
    if type(frame) == 'table' and type(frame.args) == 'table' and type(frame.getParent) == 'function' then
    if type(frame.args) == 'table' and type(frame.getParent) == 'function' then
    fargs = frame.args
    if not options.parentOnly then
    pargs = frame:getParent().args
    fargs = frame.args
    end
    if not options.frameOnly then
    pargs = frame:getParent().args
    end
    if options.parentFirst then
    fargs, pargs = pargs, fargs
    end
    else
    else
    luaArgs = type(frame) == 'table' and frame or {}
    luaArgs = frame
    end
    end


    Line 22: Line 76:
    setmetatable(args, metatable)
    setmetatable(args, metatable)


    local function tidyVal(key, val)
    -- Generate the tidyVal function. If it has been specified by the user, we use that; if not, we choose one of four functions
    -- Processes a value according to the options given to getArguments. Can trim whitespace and remove blanks.
    -- depending on the options chosen. This is so that we don't have to call the options table every time the function is called.
    -- Keys are not used here, but they can be used by user-generated functions, so defining it here to avoid breakage.
    local tidyVal = options.valueFunc
    if type(val) == 'string' then
    if tidyVal then
    if options.trim ~= false then
    <