Difference between revisions of "Module:Data"
m (Use actual template names.) |
(Adapt for Special:Diff/3920269. Cleaner.) |
||
Line 1: | Line 1: | ||
-- <pre> | -- <pre> | ||
-- @name Data | -- @name Data | ||
− | -- @description Interface for Module: | + | -- @description Interface for [[Module:Data/data]]. |
-- @notes Internal-only, so far. | -- @notes Internal-only, so far. | ||
-- @author [[User:Becasita]] | -- @author [[User:Becasita]] | ||
Line 14: | Line 14: | ||
-- Load data: | -- Load data: | ||
-------------- | -------------- | ||
− | local DATA = mw.loadData( 'Module: | + | local DATA = mw.loadData( 'Module:Data/data' ); |
+ | local NORM = DATA.normalize -- mw.loadData( 'Module:Data/normalize' ); | ||
+ | |||
+ | ------------- | ||
+ | -- Constants: | ||
+ | ------------- | ||
+ | -- @description | ||
+ | --local WORD_DELIMITERS = "[%s%-_]"; | ||
--------------------- | --------------------- | ||
Line 23: | Line 30: | ||
-- @name normalize | -- @name normalize | ||
− | |||
− | |||
-- @description Normalizes the input applying a specific set of rules. | -- @description Normalizes the input applying a specific set of rules. | ||
+ | -- The rules are applied in case the input is of the type `string` | ||
+ | -- @parameter {*} arg Argument to be normalized. | ||
+ | -- @parameter {function} rules Normalization rules to apply. | ||
+ | -- @return A generally normalized arg to be used to index the normalization table. | ||
local function normalize( arg, rules ) | local function normalize( arg, rules ) | ||
return type( arg ) == type( 'string' ) | return type( arg ) == type( 'string' ) | ||
Line 33: | Line 42: | ||
end | end | ||
− | -- @name | + | -- @name normalizeRegionLanguageMedium |
− | -- @description Normalizes the input for | + | -- @description Normalizes the input for region, language and medium, |
− | + | -- since there are dependencies between these. | |
− | + | -- @parameter {*} arg Argument to be normalized. | |
− | + | -- @return A normalized arg to be used to index the normalization table. | |
− | + | local function normalizeRegionLanguageMedium( arg ) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | -- @ | ||
− | |||
− | local function | ||
return normalize( arg, function( normalizedArg ) | return normalize( arg, function( normalizedArg ) | ||
return normalizedArg | return normalizedArg | ||
− | :gsub( | + | :gsub( "[%s%-_'!]", '' ) -- Remove a bunch of commonly used characters. |
− | + | :gsub( 'north', '' ) -- Remove "north" (for region and language). | |
− | :gsub( | + | :gsub( 'yugioh', '' ) -- Remove "yugioh" (becuase of the medium). |
− | + | :gsub( 'cardgame', '' ) -- Remove "cardgame" (because of the medium). | |
− | :gsub( | ||
− | |||
− | :gsub( | ||
− | |||
− | |||
; | ; | ||
end ); | end ); | ||
end | end | ||
− | -- @name | + | -- @name normalizeRarity |
− | -- @description Normalizes the input for | + | -- @description Normalizes the input for rarity. |
− | local function | + | -- @parameter {*} arg Argument to be normalized. |
+ | -- @return A normalized arg to be used to index the normalization table. | ||
+ | --[[local function normalizeRarity( arg ) | ||
return normalize( arg, function( normalizedArg ) | return normalize( arg, function( normalizedArg ) | ||
− | return normalizedArg | + | return NORM.rarity[ normalizedArg |
− | :gsub( | + | :gsub( ' rare$', '' ) -- Remove " rare" at the end (and with a space before it). |
− | + | :gsub( "[%s%-_']", '' ) -- Remove a bunch of commonly used characters. | |
− | :gsub( | + | ]; |
− | |||
− | ; | ||
end ); | end ); | ||
− | end | + | end--]] |
----------------------- | ----------------------- | ||
-- Methods (Interface): | -- Methods (Interface): | ||
----------------------- | ----------------------- | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
-- @name getRegion | -- @name getRegion | ||
− | -- @parameter {string} | + | -- @description Gets the region for `arg`. `nil` if not found. |
− | -- @return { | + | -- @parameter {string} arg |
− | + | -- @return {table|nil} A region or nil. | |
function D.getRegion( arg ) | function D.getRegion( arg ) | ||
− | return DATA.region[ | + | return DATA.region[ |
− | + | NORM.region[ | |
− | + | normalizeRegionLanguageMedium( arg ) | |
− | + | ] | |
− | + | ]; | |
− | |||
− | |||
− | |||
− | |||
end | end | ||
-- @name getLanguage | -- @name getLanguage | ||
− | -- @parameter {string} | + | -- @description Gets the language for `arg`. `nil` if not found. |
− | -- @return { | + | -- @parameter {string} arg |
− | + | -- @return {table|nil} A language or nil. | |
function D.getLanguage( arg ) | function D.getLanguage( arg ) | ||
− | return DATA.language[ | + | return DATA.language[ |
+ | NORM.language[ | ||
+ | NORM.region[ | ||
+ | normalizeRegionLanguageMedium( arg ) | ||
+ | ] | ||
+ | ] | ||
+ | ]; | ||
end | end | ||
− | -- @name | + | -- @name getMedium |
− | -- @parameter {string} | + | -- @description Gets the language for `arg`. `nil` if not found. |
− | -- @return { | + | -- @parameter {string} arg |
− | + | -- @return {table|nil} A language or nil. | |
− | function D. | + | function D.getMedium( arg ) |
− | return DATA. | + | local normalizedArg = normalizeRegionLanguageMedium( arg ); |
+ | return DATA.medium[ | ||
+ | NORM.medium[ | ||
+ | NORM.region[ | ||
+ | normalizedArg | ||
+ | ] or normalizedArg | ||
+ | ] | ||
+ | ]; | ||
end | end | ||
-- @name getEdition | -- @name getEdition | ||
− | -- @parameter {string} | + | -- @description Gets the edition for `arg`. `nil` if not found. |
− | -- @return {string|nil} | + | -- @parameter {string} arg |
− | + | -- @return {string|nil} An edition or nil. | |
function D.getEdition( arg ) | function D.getEdition( arg ) | ||
− | return DATA.edition[ | + | return DATA.edition[ |
− | + | normalize( arg, function( normalizedArg ) | |
− | + | return NORM.edition[ normalizedArg | |
− | -- | + | :gsub( '[%s%-_]', '' ) -- Remove a bunch of commonly used characters. |
− | - | + | :gsub( 'edition', '' ) -- Redundant. |
− | -- | + | ]; |
− | + | end ) | |
− | + | ]; | |
− | |||
end | end | ||
-- @name getRelease | -- @name getRelease | ||
− | -- @parameter {string} | + | -- @description Gets the release for `arg`. `nil` if not found. |
− | -- @return {string|nil} | + | -- @parameter {string} arg |
− | + | -- @return {string|nil} A release or `nil`. | |
function D.getRelease( arg ) | function D.getRelease( arg ) | ||
− | return DATA.release[ | + | return DATA.release[ |
− | + | normalize( arg, function( normalizedArg ) | |
− | + | return NORM.release[ normalizedArg | |
− | + | :gsub( '[%s%-_]', '' ) -- Remove a bunch of commonly used characters. | |
− | + | :gsub( 'case', '' ) -- for "Case Topper". | |
− | + | :gsub( 'card', '' ) -- for "Giant Card". | |
− | + | :gsub( 'official', '' ) -- For "Official Proxy". | |
− | + | ]; | |
− | + | end ) | |
− | + | ]; | |
− | |||
− | -- | ||
− | -- | ||
− | |||
− | -- | ||
− | |||
− | |||
− | end | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
-- @name getRarity | -- @name getRarity | ||
− | -- @parameter {string} | + | -- @description Gets the rarity for `arg`. `nil` if not found. |
− | -- @return {string|nil} | + | -- @parameter {string} arg |
− | + | -- @return {string|nil} A rarity or `nil`. | |
function D.getRarity( arg ) | function D.getRarity( arg ) | ||
− | return DATA.rarity[ | + | return DATA.rarity[ |
+ | normalize( arg, function( normalizedArg ) | ||
+ | return NORM.rarity[ normalizedArg | ||
+ | :gsub( ' rare$', '' ) -- Remove " rare" at the end (and with a space before it). | ||
+ | :gsub( "[%s%-_']", '' ) -- Remove a bunch of commonly used characters. | ||
+ | ]; | ||
+ | end ) | ||
+ | ]; | ||
end | end | ||
− | -- @name | + | --------------- |
− | -- @parameter {string} | + | -- Anime stuff: |
− | -- @return {string|nil} Anime | + | --------------- |
− | + | -- @name getAnimeRelease | |
− | function D. | + | -- @description Gets the anime release for `arg`. `nil` if not found. |
− | return DATA. | + | -- @parameter {string} arg |
+ | -- @return {string|nil} Anime release or `nil`. | ||
+ | function D.getAnimeRelease( arg ) | ||
+ | return DATA.anime.release[ | ||
+ | normalize( arg, function( normalizedArg ) | ||
+ | return NORM.anime.release[ normalizedArg | ||
+ | :gsub( '[%s%-_]', '' ) -- Remove a bunch of commonly used characters. | ||
+ | ]; | ||
+ | end ) | ||
+ | ]; | ||
end | end | ||
-- @name getAnimeSeries | -- @name getAnimeSeries | ||
− | -- @parameter {string} | + | -- @description Gets the anime series name for `arg`. `nil` if not found. |
− | -- @return {string|nil} Anime series | + | -- @parameter {string} arg |
− | + | -- @return {string|nil} Anime series or `nil`. | |
function D.getAnimeSeries( arg ) | function D.getAnimeSeries( arg ) | ||
− | return DATA.series | + | return DATA.anime.series[ |
− | + | normalize( arg, function( normalizedArg ) | |
− | + | return NORM.anime.series[ normalizedArg | |
− | - | + | :gsub( "[%s%-_'/:!]", '' ) -- Remove a bunch of commonly used characters. |
− | + | :gsub( 'the', '' ) | |
− | -- | + | :gsub( 'ygo', '' ) |
− | + | :gsub( 'yugioh', '' ) | |
− | + | ]; | |
− | + | end ) | |
− | end | + | ]; |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
+ | ------------------- | ||
+ | -- Templates stuff: | ||
+ | ------------------- | ||
-- @name getCardGalleryType | -- @name getCardGalleryType | ||
− | -- @parameter {string} | + | -- @description Gets the `{{Card gallery}}` type for `arg`. `nil` if not found. |
− | -- @return {string|nil} {{Card gallery}} | + | -- @parameter {string} arg |
− | + | -- @return {string|nil} `{{Card gallery}}` possible `{{{type}}}`s. | |
function D.getCardGalleryType( arg ) | function D.getCardGalleryType( arg ) | ||
− | return DATA['Card gallery'].types[ | + | return DATA.templates[ 'Card gallery' ].types[ |
+ | normalize( arg, function( normalizedArg ) | ||
+ | return normalizedArg | ||
+ | :gsub( "[%s%-s]", '' ) -- Remove a bunch of commonly used characters. | ||
+ | :gsub( 'video', '' ) -- Remove "video". | ||
+ | ; | ||
+ | end ) | ||
+ | ]; | ||
end | end | ||
Line 224: | Line 217: | ||
-- Return: | -- Return: | ||
---------- | ---------- | ||
+ | -- @exports `D`: Interface to interact with [[Module:Data/data]]. | ||
return D; | return D; | ||
-- </pre> | -- </pre> |
Revision as of 14:04, 10 August 2018
- Data/Templates
- Data/data
- Data/doc
- Data/endpoints
- Data/loader
- Data/namespaces/anime
- Data/namespaces/anime/endpoints
- Data/namespaces/anime/static/release
- Data/namespaces/anime/static/release/data
- Data/namespaces/anime/static/series
- Data/namespaces/anime/static/series/data
- Data/namespaces/manga
- Data/namespaces/manga/endpoints
- Data/namespaces/manga/static/release
- Data/namespaces/manga/static/release/data
- Data/namespaces/manga/static/series
- Data/namespaces/manga/static/series/data
- Data/namespaces/videoGames
- Data/namespaces/videoGames/endpoints
- Data/namespaces/videoGames/static/name
- Data/namespaces/videoGames/static/name/data
- Data/namespaces/videoGames/static/release
- Data/namespaces/videoGames/static/release/data
- Data/sandbox
- Data/smw/cardType
- Data/smw/name
- Data/smw/releaseDate
- Data/smw/translatedName
- Data/static/edition
- Data/static/edition/data
- Data/static/language
- Data/static/language/data
- Data/static/medium
- Data/static/medium/data
- Data/static/rarity
- Data/static/rarity/data
- Data/static/region
- Data/static/region/data
- Data/static/release
- Data/static/release/data
-- <pre>
-- @name Data
-- @description Interface for [[Module:Data/data]].
-- @notes Internal-only, so far.
-- @author [[User:Becasita]]
-- @contact [[User talk:Becasita]]
-------------------
-- Export variable:
-------------------
local D = {};
--------------
-- Load data:
--------------
local DATA = mw.loadData( 'Module:Data/data' );
local NORM = DATA.normalize -- mw.loadData( 'Module:Data/normalize' );
-------------
-- Constants:
-------------
-- @description
--local WORD_DELIMITERS = "[%s%-_]";
---------------------
-- Utility functions:
---------------------
-- mw functions:
local trim = mw.text.trim;
-- @name normalize
-- @description Normalizes the input applying a specific set of rules.
-- The rules are applied in case the input is of the type `string`
-- @parameter {*} arg Argument to be normalized.
-- @parameter {function} rules Normalization rules to apply.
-- @return A generally normalized arg to be used to index the normalization table.
local function normalize( arg, rules )
return type( arg ) == type( 'string' )
and rules( trim( arg ):lower() )
or nil
;
end
-- @name normalizeRegionLanguageMedium
-- @description Normalizes the input for region, language and medium,
-- since there are dependencies between these.
-- @parameter {*} arg Argument to be normalized.
-- @return A normalized arg to be used to index the normalization table.
local function normalizeRegionLanguageMedium( arg )
return normalize( arg, function( normalizedArg )
return normalizedArg
:gsub( "[%s%-_'!]", '' ) -- Remove a bunch of commonly used characters.
:gsub( 'north', '' ) -- Remove "north" (for region and language).
:gsub( 'yugioh', '' ) -- Remove "yugioh" (becuase of the medium).
:gsub( 'cardgame', '' ) -- Remove "cardgame" (because of the medium).
;
end );
end
-- @name normalizeRarity
-- @description Normalizes the input for rarity.
-- @parameter {*} arg Argument to be normalized.
-- @return A normalized arg to be used to index the normalization table.
--[[local function normalizeRarity( arg )
return normalize( arg, function( normalizedArg )
return NORM.rarity[ normalizedArg
:gsub( ' rare$', '' ) -- Remove " rare" at the end (and with a space before it).
:gsub( "[%s%-_']", '' ) -- Remove a bunch of commonly used characters.
];
end );
end--]]
-----------------------
-- Methods (Interface):
-----------------------
-- @name getRegion
-- @description Gets the region for `arg`. `nil` if not found.
-- @parameter {string} arg
-- @return {table|nil} A region or nil.
function D.getRegion( arg )
return DATA.region[
NORM.region[
normalizeRegionLanguageMedium( arg )
]
];
end
-- @name getLanguage
-- @description Gets the language for `arg`. `nil` if not found.
-- @parameter {string} arg
-- @return {table|nil} A language or nil.
function D.getLanguage( arg )
return DATA.language[
NORM.language[
NORM.region[
normalizeRegionLanguageMedium( arg )
]
]
];
end
-- @name getMedium
-- @description Gets the language for `arg`. `nil` if not found.
-- @parameter {string} arg
-- @return {table|nil} A language or nil.
function D.getMedium( arg )
local normalizedArg = normalizeRegionLanguageMedium( arg );
return DATA.medium[
NORM.medium[
NORM.region[
normalizedArg
] or normalizedArg
]
];
end
-- @name getEdition
-- @description Gets the edition for `arg`. `nil` if not found.
-- @parameter {string} arg
-- @return {string|nil} An edition or nil.
function D.getEdition( arg )
return DATA.edition[
normalize( arg, function( normalizedArg )
return NORM.edition[ normalizedArg
:gsub( '[%s%-_]', '' ) -- Remove a bunch of commonly used characters.
:gsub( 'edition', '' ) -- Redundant.
];
end )
];
end
-- @name getRelease
-- @description Gets the release for `arg`. `nil` if not found.
-- @parameter {string} arg
-- @return {string|nil} A release or `nil`.
function D.getRelease( arg )
return DATA.release[
normalize( arg, function( normalizedArg )
return NORM.release[ normalizedArg
:gsub( '[%s%-_]', '' ) -- Remove a bunch of commonly used characters.
:gsub( 'case', '' ) -- for "Case Topper".
:gsub( 'card', '' ) -- for "Giant Card".
:gsub( 'official', '' ) -- For "Official Proxy".
];
end )
];
end
-- @name getRarity
-- @description Gets the rarity for `arg`. `nil` if not found.
-- @parameter {string} arg
-- @return {string|nil} A rarity or `nil`.
function D.getRarity( arg )
return DATA.rarity[
normalize( arg, function( normalizedArg )
return NORM.rarity[ normalizedArg
:gsub( ' rare$', '' ) -- Remove " rare" at the end (and with a space before it).
:gsub( "[%s%-_']", '' ) -- Remove a bunch of commonly used characters.
];
end )
];
end
---------------
-- Anime stuff:
---------------
-- @name getAnimeRelease
-- @description Gets the anime release for `arg`. `nil` if not found.
-- @parameter {string} arg
-- @return {string|nil} Anime release or `nil`.
function D.getAnimeRelease( arg )
return DATA.anime.release[
normalize( arg, function( normalizedArg )
return NORM.anime.release[ normalizedArg
:gsub( '[%s%-_]', '' ) -- Remove a bunch of commonly used characters.
];
end )
];
end
-- @name getAnimeSeries
-- @description Gets the anime series name for `arg`. `nil` if not found.
-- @parameter {string} arg
-- @return {string|nil} Anime series or `nil`.
function D.getAnimeSeries( arg )
return DATA.anime.series[
normalize( arg, function( normalizedArg )
return NORM.anime.series[ normalizedArg
:gsub( "[%s%-_'/:!]", '' ) -- Remove a bunch of commonly used characters.
:gsub( 'the', '' )
:gsub( 'ygo', '' )
:gsub( 'yugioh', '' )
];
end )
];
end
-------------------
-- Templates stuff:
-------------------
-- @name getCardGalleryType
-- @description Gets the `{{Card gallery}}` type for `arg`. `nil` if not found.
-- @parameter {string} arg
-- @return {string|nil} `{{Card gallery}}` possible `{{{type}}}`s.
function D.getCardGalleryType( arg )
return DATA.templates[ 'Card gallery' ].types[
normalize( arg, function( normalizedArg )
return normalizedArg
:gsub( "[%s%-s]", '' ) -- Remove a bunch of commonly used characters.
:gsub( 'video', '' ) -- Remove "video".
;
end )
];
end
----------
-- Return:
----------
-- @exports `D`: Interface to interact with [[Module:Data/data]].
return D;
-- </pre>