Permanently protected module

Module:Data

From Yugipedia
Revision as of 14:46, 25 April 2018 by Becasita (talk | contribs) (Interface to handle Module:Database.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

-- <pre>
-- @name Data
-- @description Interface for Module:Database.
-- @notes Internal-only, so far.
-- @author [[User:Becasita]]
-- @contact [[User talk:Becasita]]

-------------------
-- Export variable:
-------------------
local D = {};

--------------
-- Load data:
--------------
local DATA = mw.loadData( 'Module:Database' );

----------------------
-- Internal functions:
----------------------
-- @name normalize
-- @description Normalize de input.
local function normalize( arg )
	return type( arg ) == 'string'
		and arg:lower()
			:gsub(    '%-', '' ) -- Remove dashes.
			:gsub(     ' ', '' ) -- Remove spaces.
			:gsub( 'north', '' ) -- Remove "north".
			:gsub( 'rare$', '' ) -- Remove "rare" at the end.
		or nil
	;
end

-----------
-- Methods:
-----------
-- @name getRg
-- @parameter {string} «arg»
-- @return {string|nil} Region index.
-- @description Gets the region index for «arg». «nil» if not found.
function D.getRg( arg )
	return DATA.rg[ normalize( arg ) ];
end

-- @name getRegion
-- @parameter {string} «arg»
-- @return {string|nil} Region name.
-- @description Gets the region name for «arg». «nil» if not found.
function D.getRegion( arg )
	return DATA.region[ D.getRg( arg ) ];
end

-- @name getLn
-- @parameter {string} «arg»
-- @return {string|nil} Language index.
-- @description Gets the language index for «arg». «nil» if not found.
function D.getLn( arg )
	return DATA.ln[ D.getRg( arg ) ];
end

-- @name getLanguage
-- @parameter {string} «arg»
-- @return {string|nil} Language name.
-- @description Gets the language name for «arg». «nil» if not found.
function D.getLanguage( arg )
	return DATA.language[ D.getLn( arg ) ];
end

-- @name getR
-- @parameter {string} «arg»
-- @return {string|nil} Rarity abbreviation.
-- @description Gets the rarity abbreviation for «arg». «nil» if not found.
function D.getR( arg )
	return DATA.r[ normalize( arg ) ];
end

-- @name getRarity
-- @parameter {string} «arg»
-- @return {string|nil} Rarity name.
-- @description Gets the rarity name for «arg». «nil» if not found.
function D.getRarity( arg )
	return DATA.rarity[ D.getR( arg ) ];
end

----------
-- Return:
----------
return D;
-- </pre>