Module:Sandbox/Toohool

local p = {}

local function trim(s) return mw.ustring.gsub(s, "^%s*(.-)%s*$", "%1") end

function p.makeWrapper(func, opts) opts = opts or {} local nullableArgNames = {} for i, v in ipairs(opts.nullable or {}) do       nullableArgNames[v] = true end local noTrimArgNames = {} for i, v in ipairs(opts.notrim or opts.noTrim or {}) do       noTrimArgNames[v] = true end return function(frame) local origArgs if frame == mw.getCurrentFrame then -- We're being called via #invoke. If the invoking template passed any args, use -- them. Otherwise, use the args that were passed into the template. origArgs = frame:getParent.args for k, v in pairs(frame.args) do               origArgs = frame.args break end else -- We're being called from another module or from the debug console, so assume -- the args are passed in directly. origArgs = frame end local args = {} setmetatable(args, {           -- ParserFunctions considers the empty string to be false, so to preserve the previous             -- behavior of the template, change any empty arguments to nil, so Lua will consider            -- them false too.            __index = function(t, k)                local v = origArgs[k]                if v and not noTrimArgNames[k] then                    v = trim(v)                end                if v ~= '' or nullableArgNames[k] then                    return v                else                    return nil                end            end,            __pairs = function(t)                local nextFunc = function(tbl, k)                    local v                    repeat                        k = next(origArgs, k)                        v = args[k]                    until (not k) or v                    return k, v                end                return nextFunc, t, nil end, __ipairs = function(t) -- TODO end })       return func(args)    end end

return p