Module:ArgsUtil

local bool_false = { ['false'] = true, ['no'] = true, [''] = true, ['0'] = true, ['nil'] = true } local bool_true = { ['true'] = true, ['yes'] = true }

local p = {} local h = {}

function p.norm(v) if not v then return false end local lc = lang:lc(v) if bool_false[lc] then return false elseif bool_true[lc] then return true end return v end

function p.castAsBool(str) if type(str) == 'boolean' then return str end if not str or bool_false[lang:lc(str)] then return false end return true end

function p.isDefined(str) if not str then return false end return true end

function p.boolToStringYN(str) if str then return 'Yes' end return 'No' end

function p.boolToStringTF(str) if str then return 'True' end return 'False' end

function p.nilToFalse(val) -- casts nil as false -- does not change anything else -- used if needing to have false but non-nil values in a table -- for ipairs, util_table.removeFalseEntries, etc return not not val and val end

function p.lookupVars(str, lookup_tbl, skipdefault) -- for rolenames etc. if a default table is supplied, this will be	-- returned with priority over lookup.DEFAULT in the case of no match local vars = str and lookup_tbl[lang:lc(str)] if not vars then if skipdefault then return nil end return lookup_tbl.DEFAULT end if type(vars) == 'string' then vars = lookup_tbl[vars] if type(vars) == 'string' then error(('Error in lookup module for input %s'):format(str)) end end return vars end

function p.merge local f = mw.getCurrentFrame local origArgs = f.args local parentArgs = f:getParent.args

local args = {} for k, v in pairs(origArgs) do		v = mw.text.trim(tostring(v)) if v ~= '' then args[k] = v		end end for k, v in pairs(parentArgs) do		v = mw.text.trim(v) if v ~= '' then args[k] = v		end end return args end

function p.mergeKeepEmpty local f = mw.getCurrentFrame local origArgs = f.args local parentArgs = f:getParent.args

local args = {} for k, v in pairs(origArgs) do		v = mw.text.trim(tostring(v)) args[k] = v	end for k, v in pairs(parentArgs) do		v = mw.text.trim(v) args[k] = v	end return args end

function p.overwrite local f = mw.getCurrentFrame local origArgs = f.args local parentArgs = f:getParent.args

local args = {} for k, v in pairs(parentArgs) do		v = mw.text.trim(v) args[k] = v	end for k, v in pairs(origArgs) do		v = mw.text.trim(tostring(v)) args[k] = v	end return args end

function p.original local f = mw.getCurrentFrame local origArgs = f.args

local args = {} for k, v in pairs(origArgs) do		v = mw.text.trim(tostring(v)) args[k] = v	end

return args end

return p