Apex Legends Esports Wiki

READ MORE

Apex Legends Esports Wiki
[unchecked revision][checked revision]
Donut (talk | contribs)
No edit summary
m (Syncing content across wikis, if something seems broken as a result let me know)
Line 1: Line 1:
  +
local util_text = require('Module:TextUtil')
  +
local util_table = require('Module:TableUtil')
  +
 
local bool_false = { ['false'] = true, ['no'] = true, [''] = true }
 
local bool_false = { ['false'] = true, ['no'] = true, [''] = true }
  +
local bool_true = { ['true'] = true, ['yes'] = true }
   
 
local lang = mw.getLanguage('en')
 
local lang = mw.getLanguage('en')
Line 8: Line 12:
 
if not v then
 
if not v then
 
return false
 
return false
  +
end
elseif bool_false[lang:lc(v)] then
 
  +
local lc = lang:lc(v)
  +
if bool_false[lc] then
 
return false
 
return false
  +
elseif bool_true[lc] then
  +
return true
 
end
 
end
 
return v
 
return v
  +
end
  +
  +
function p.castAsBool(str)
 
if not str or bool_false[lang:lc(str)] then
  +
return false
  +
end
  +
return true
  +
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
 
end
   
Line 39: Line 62:
 
for k, v in pairs(origArgs) do
 
for k, v in pairs(origArgs) do
 
v = mw.text.trim(tostring(v))
 
v = mw.text.trim(tostring(v))
if norm and v ~= '' then
+
if norm and v ~= '' or not norm then
args[k] = p.norm(v)
 
elseif v ~= '' then
 
 
args[k] = v
 
args[k] = v
 
end
 
end
Line 48: Line 69:
 
for k, v in pairs(parentArgs) do
 
for k, v in pairs(parentArgs) do
 
v = mw.text.trim(v)
 
v = mw.text.trim(v)
if norm and v ~= '' then
+
if norm and v ~= '' or not norm then
args[k] = p.norm(v)
 
elseif v ~= '' then
 
 
args[k] = v
 
args[k] = v
 
end
 
end
Line 69: Line 88:
 
for k, v in pairs(parentArgs) do
 
for k, v in pairs(parentArgs) do
 
v = mw.text.trim(v)
 
v = mw.text.trim(v)
if norm and v ~= '' then
+
if norm and v ~= '' or not norm then
args[k] = p.norm(v)
 
elseif v ~= '' then
 
 
args[k] = v
 
args[k] = v
 
end
 
end
Line 78: Line 95:
 
for k, v in pairs(origArgs) do
 
for k, v in pairs(origArgs) do
 
v = mw.text.trim(tostring(v))
 
v = mw.text.trim(tostring(v))
if norm and v ~= '' then
+
if norm and v ~= '' or not norm then
args[k] = p.norm(v)
 
elseif v ~= '' then
 
 
args[k] = v
 
args[k] = v
 
end
 
end
Line 88: Line 103:
 
end
 
end
   
function p.numberedArgsToTable(args, argname, disallowUnnamedFirst)
+
function p.numberedArgsToTable(args, argname, disallowUnnamedFirst, max)
  +
if not max then max = -1 end
 
local i = 1
 
local i = 1
 
local tbl = {}
 
local tbl = {}
Line 95: Line 111:
 
i = 2
 
i = 2
 
end
 
end
while args[argname .. i] do
+
while args[argname .. i] or i <= max do
 
tbl[i] = args[argname .. i]
 
tbl[i] = args[argname .. i]
 
i = i + 1
 
i = i + 1
Line 104: Line 120:
 
return tbl
 
return tbl
 
end
 
end
  +
  +
function p.numberedArgsToList(args, argname, disallowUnnamedFirst, max, sep, removeEmpty)
  +
local tbl = p.numberedArgsToTable(args, argname, disallowUnnamedFirst, max)
  +
if removeEmpty then
  +
util_table.removeFalseEntries(tbl, max)
 
elseif max then
  +
for k = 1, max do
  +
tbl[k] = tbl[k] or ''
  +
end
  +
end
  +
return table.concat(tbl, sep or ',')
  +
end
  +
  +
function p.splitAndMap(str, sep, f)
  +
if not sep then
  +
sep = '%s*,%s*'
  +
end
  +
local tbl = util_text.split(str,sep)
  +
if f then
  +
return util_table.mapInPlace(tbl, f)
  +
else
  +
return tbl
  +
end
  +
end
  +
  +
function p.splitMapConcat(str, sep, f, sep2)
  +
if not sep2 then sep2 = '' end
  +
local tbl = p.splitAndMap(str, sep, f)
  +
return table.concat(tbl, sep2)
  +
end
  +
  +
function p.ifArg(arg, display)
  +
if not arg then
  +
return false
  +
end
  +
return display
  +
end
  +
  +
function p.splitArgs(input, fieldlist, sep)
  +
sep = sep or '%s*;;;%s*'
  +
local result = {}
  +
local tbl = util_text.split(input,sep)
  +
for i, v in ipairs(fieldlist) do
 
if tbl[i] ~= '' then
  +
result[v] = tbl[i]
  +
end
  +
end
  +
return result
  +
end
  +
 
return p
 
return p

Revision as of 12:20, 15 February 2019

To edit the documentation or categories for this module, click here.


local util_text = require('Module:TextUtil')
local util_table = require('Module:TableUtil')

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

local lang = mw.getLanguage('en')

local p = {}

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 not str or bool_false[lang:lc(str)] then
		return false
	end
	return true
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]
	end
	return vars
end

function p.merge(norm)
	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 norm and v ~= '' or not norm then
			args[k] = v
		end
	end
	
	for k, v in pairs(parentArgs) do
		v = mw.text.trim(v)
		if norm and v ~= '' or not norm then
			args[k] = v
		end
	end
	
	return args
end

function p.overwrite(origArgs, parentArgs, norm)
	if type(origArgs) ~= 'table' then
		norm = origArgs
		local f = mw.getCurrentFrame()
		origArgs = f.args
		parentArgs = f:getParent().args
	end
	local args = {}
	
	for k, v in pairs(parentArgs) do
		v = mw.text.trim(v)
		if norm and v ~= '' or not norm then
			args[k] = v
		end
	end
	
	for k, v in pairs(origArgs) do
		v = mw.text.trim(tostring(v))
		if norm and v ~= '' or not norm then
			args[k] = v
		end
	end
	
	return args
end

function p.numberedArgsToTable(args, argname, disallowUnnamedFirst, max)
	if not max then max = -1 end
	local i = 1
	local tbl = {}
	if args[argname] and not disallowUnnamedFirst then
		tbl[1] = args[argname]
		i = 2
	end
	while args[argname .. i] or i <= max do
		tbl[i] = args[argname .. i]
		i = i + 1
	end
	if not next(tbl) then
		return nil
	end
	return tbl
end

function p.numberedArgsToList(args, argname, disallowUnnamedFirst, max, sep, removeEmpty)
	local tbl = p.numberedArgsToTable(args, argname, disallowUnnamedFirst, max)
	if removeEmpty then
		util_table.removeFalseEntries(tbl, max)
	elseif max then
		for k = 1, max do
			tbl[k] = tbl[k] or ''
		end
	end
	return table.concat(tbl, sep or ',')
end

function p.splitAndMap(str, sep, f)
	if not sep then
		sep = '%s*,%s*'
	end
	local tbl = util_text.split(str,sep)
	if f then
		return util_table.mapInPlace(tbl, f)
	else
		return tbl
	end
end

function p.splitMapConcat(str, sep, f, sep2)
	if not sep2 then sep2 = '' end
	local tbl = p.splitAndMap(str, sep, f)
	return table.concat(tbl, sep2)
end

function p.ifArg(arg, display)
	if not arg then
		return false
	end
	return display
end

function p.splitArgs(input, fieldlist, sep)
	sep = sep or '%s*;;;%s*'
	local result = {}
	local tbl = util_text.split(input,sep)
	for i, v in ipairs(fieldlist) do
		if tbl[i] ~= '' then
			result[v] = tbl[i]
		end
	end
	return result
end

return p