Binding of Isaac: Rebirth Wiki
m (contains)
m (unique)
Line 2: Line 2:
   
   
-- Indicates if a table contains an element.
 
 
function p.contains( t, e )
 
function p.contains( t, e )
 
for _, v in pairs( t ) do
 
for _, v in pairs( t ) do
Line 14: Line 13:
   
 
-- Get parameters of the template and the article from the frame (usually f)
 
-- Get parameters of the template and the article from the frame (usually f)
  +
-- DEPRECATED
 
function p.getArgs( f )
 
function p.getArgs( f )
 
local args = {}
 
local args = {}
Line 32: Line 32:
   
   
  +
-- DEPRECATED
 
function p.getParametersCount( f )
 
function p.getParametersCount( f )
 
local args = p.trimAll( p.getArgs( f ) )
 
local args = p.trimAll( p.getArgs( f ) )
Line 51: Line 52:
   
 
-- Do string.gsub to all values in a table
 
-- Do string.gsub to all values in a table
function p.gsub( f, pattern, repl )
+
function p.gsub( t, pattern, repl )
local g = {}
+
local result = {}
for i, j in pairs( f ) do
+
for k, v in pairs( t ) do
if type( j ) == 'string' then
+
if type( v ) == 'string' then
g[i] = string.gsub( j, pattern, repl )
+
result[k] = string.gsub( v, pattern, repl )
 
else
 
else
g[i] = j
+
result[k] = v
 
end
 
end
 
end
 
end
return g
+
return result
 
end
 
end
   
   
 
-- Do string.gsub to all indexes in a table
 
-- Do string.gsub to all indexes in a table
function p.igsub( f, pattern, repl )
+
function p.igsub( t, pattern, repl )
local g = {}
+
local result = {}
for i, j in pairs( f ) do
+
for k, v in pairs( t ) do
g[string.gsub( i, pattern, repl )] = j
+
result[string.gsub( k, pattern, repl )] = v
 
end
 
end
return g
+
return result
 
end
 
end
   
Line 85: Line 86:
   
   
  +
-- DEPRECATED
 
function p.search( f )
 
function p.search( f )
 
local args = p.trimAll( p.getArgs( f ) )
 
local args = p.trimAll( p.getArgs( f ) )
Line 106: Line 108:
   
   
 
function p.trim( t, toRemove, keepEmptyParams )
-- Do mw.text.trim to all values in a table
 
 
local result = {}
function p.trim( f, toRemove, keepEmptyParams )
 
 
for k, v in pairs( t ) do
return p.trimAll( f, toRemove, keepEmptyParams )
 
 
if type( v ) == 'string' then
 
result[k] = mw.text.trim( v ) ~= '' and ( toRemove and mw.text.trim( v, toRemove ) or mw.text.trim( v ) ) or keepEmptyParams and ''
 
else
 
result[k] = v
  +
end
  +
end
  +
return result
 
end
 
end
   
 
-- Do mw.text.trim to all values in a table
function p.trimAll( f, toRemove, keepEmptyParams )
 
  +
-- DEPRECATED
local g = {}
 
  +
p.trimAll = p.trim
for i, j in pairs( f ) do
 
  +
if type( j ) == 'string' then
 
  +
g[i] = mw.text.trim( j ) ~= '' and ( toRemove and mw.text.trim( j, toRemove ) or mw.text.trim( j ) ) or keepEmptyParams and ''
 
  +
function unique( t )
else
 
  +
local result = {}
g[i] = j
 
  +
for k, v in pairs( t ) do
  +
if not p.contains( result, v ) then
  +
result[k] = v
 
end
 
end
 
end
 
end
return g
+
return result
 
end
 
end
   

Revision as of 13:01, 27 April 2020


local p = {}


function p.contains( t, e )
	for _, v in pairs( t ) do
		if v == e then
			return true
		end
	end
	return false
end


-- Get parameters of the template and the article from the frame (usually f)
-- DEPRECATED
function p.getArgs( f )
	local args = {}
	if f.args then
		-- Add template parameters
		for i, j in pairs( f.args ) do
			args[i] = j
		end
		-- Add transclusion parameters (parent), overwrite template parameters
		for k, l in pairs( f:getParent().args ) do
			args[k] = l
		end
	else
		args = f
	end
	return args
end


-- DEPRECATED
function p.getParametersCount( f )
	local args = p.trimAll( p.getArgs( f ) )
	local parametersCount = 0
	if args.numerical then
		local i = 1
		while args[i] do
			parametersCount = parametersCount + 1
			i = i + 1
		end
	else
		for i, j in pairs( args ) do
			parametersCount = parametersCount + 1
		end
	end
	return parametersCount
end


-- Do string.gsub to all values in a table
function p.gsub( t, pattern, repl )
	local result = {}
	for k, v in pairs( t ) do
		if type( v ) == 'string' then
			result[k] = string.gsub( v, pattern, repl )
		else
			result[k] = v
		end
	end
	return result
end


-- Do string.gsub to all indexes in a table
function p.igsub( t, pattern, repl )
	local result = {}
	for k, v in pairs( t ) do
		result[string.gsub( k, pattern, repl )] = v
	end
	return result
end


function p.merge( ... )
	local result = {}
	for _, table in ipairs( arg ) do
		for k, v in pairs( table ) do
			result[k] = v
		end
	end
	return result
end


-- DEPRECATED
function p.search( f )
	local args = p.trimAll( p.getArgs( f ) )
	if args.index then
		for i, j in pairs( args ) do
			if args.object == j then
				return i
			end
		end
	else
		local k = 1
		repeat
			if args.object == args[k] then
				return k
			end
			k = k + 1
		until not args[k]
	end
	return nil
end


function p.trim( t, toRemove, keepEmptyParams )
	local result = {}
	for k, v in pairs( t ) do
		if type( v ) == 'string' then
			result[k] = mw.text.trim( v ) ~= '' and ( toRemove and mw.text.trim( v, toRemove ) or mw.text.trim( v ) ) or keepEmptyParams and ''
		else
			result[k] = v
		end
	end
	return result
end

-- Do mw.text.trim to all values in a table
-- DEPRECATED
p.trimAll = p.trim


function unique( t )
	local result = {}
	for k, v in pairs( t ) do
		if not p.contains( result, v ) then
			result[k] = v
		end
	end
	return result
end


return p