Difference between pages "Module:Util" and "Card Gallery:Time Thief Perpetua"

From Yugipedia
(Difference between pages)
Jump to: navigation, search
m (Fix name. Funny how this has been working so far, due to module loading times, I guess.)
 
(Restoring revision 4948984 by User:Cardsknower on 2022-04-17 08:13:45.)
 
Line 1: Line 1:
-- <pre>
+
{{Navigation}}
--[=[Doc
 
@module Util
 
@description Holds commonly used simple functions.
 
@author [[User:Becasita]]
 
@contact [[User talk:Becasita]]
 
@todo Remove those last functions to a dedicated module
 
or simply remove them.
 
]=]
 
  
-------------------
+
{{Card gallery|region=EN|
-- Export variable:
+
IGAS-EN094; Ignition Assault; SR; 1E
-------------------
+
IGAS-EN094; Ignition Assault; StR; 1E
--[[Doc
+
IGAS-EN094; Ignition Assault; SR; UE
@data U
+
GFTP-EN065; Ghosts From the Past (set); UR; 1E
@description Variable to hold the library to be exported.
+
}}
@exportable
 
]]
 
local U = {};
 
  
-------------
+
{{Card gallery|region=FR|
-- Functions:
+
IGAS-FR094; Ignition Assault; SR; 1E
-------------
+
IGAS-FR094; Ignition Assault; StR; 1E
-- mw functions:
+
IGAS-FR094; Ignition Assault; SR; UE
local mwTextTrim = mw.text.trim;
+
GFTP-FR065; Ghosts From the Past (set); UR; 1E
 +
}}
  
--[[Doc
+
{{Card gallery|region=DE|
@function U bold
+
IGAS-DE094; Ignition Assault; SR; 1E
@description Renders bold wikitext markup.
+
IGAS-DE094; Ignition Assault; StR; 1E
@parameter {string} s String to make bold.
+
IGAS-DE094; Ignition Assault; SR; UE
@return {string} Bold formatted `s`.
+
GFTP-DE065; Ghosts From the Past (set); UR; 1E
@todo Escape ' ?
+
}}
]]
 
function U.bold( s )
 
return ("'''%s'''"):format( s );
 
end
 
  
--[[Doc
+
{{Card gallery|region=IT|
@function U italic
+
IGAS-IT094; Ignition Assault; SR; 1E
@description Renders italic wikitext markup.
+
IGAS-IT094; Ignition Assault; StR; 1E
@parameter {string} s String to italicize.
+
IGAS-IT094; Ignition Assault; SR; UE
@return {string} Italicized `s`.
+
GFTP-IT065; Ghosts From the Past (set); UR; 1E
@todo Escape ' ?
+
}}
]]
 
function U.italic( s )
 
return ("''%s''"):format( s );
 
end
 
  
--[=[Doc
+
{{Card gallery|region=PT|
@function U italicNoDab
+
IGAS-PT094; Ignition Assault; SR; 1E
@description Renders italics wikitext markup, except for the dab.
+
IGAS-PT094; Ignition Assault; StR; 1E
Also normalizes the space between the dab and the rest.
+
IGAS-PT094; Ignition Assault; SR; UE
@parameter {string} s String to italicize, except its dab.
+
GFTP-PT065; Ghosts From the Past (set); UR; 1E
@return {string} Italicized `s`, except its dab.
+
}}
@see [[#U.getDab]]
 
@see [[#U.removeDab]]
 
]=]
 
function U.italicNoDab( s )
 
local dab = U.getDab( s );
 
local noDab = U.removeDab( s );
 
return table.concat( {
 
("''%s''"):format( noDab ),
 
dab ~= '' and ("(%s)"):format( dab ) or nil
 
}, ' ' );
 
end
 
  
--[=[Doc
+
{{Card gallery|region=SP|
@function U trim
+
IGAS-SP094; Ignition Assault; SR; 1E
@description Trims white space from front and tail of string.
+
IGAS-SP094; Ignition Assault; StR; 1E
Returns nil if only whitespace. Also normalizes the space between
+
IGAS-SP094; Ignition Assault; SR; UE
the dab and the rest.
+
GFTP-SP065; Ghosts From the Past (set); UR; 1E
@parameter {string|nil} s String to trim.
+
}}
@return {string|nil} Trimmed `s`. If `s` ends up being only whitespace
 
or `s` is `nil`, it returns `nil`.
 
@see [[mw:Extension:Scribunto/Lua reference manual#mw.text.trim]]
 
]=]
 
function U.trim( s )
 
if s and not s:match( '^%s*$' ) then
 
return mwTextTrim( s );
 
end
 
end
 
  
--[[Doc
+
{{Card gallery|region=JP|
@function U count
+
EP19-JP044; Extra Pack 2019 :: OP
@description Counts the number of elements in a table.
+
EP19-JP044; Extra Pack 2019; ScR
@parameter {table} t Table to get counted.
+
SLT1-JP026; Selection 10; C
@return {number} Number of elements in the table.
+
}}
]]
 
function U.count( t )
 
local counter = 0;
 
for key, value in pairs( t ) do
 
counter = counter + 1;
 
end
 
return counter;
 
end
 
  
--[[Doc
+
{{Card gallery|region=SC|
@function U link
+
MGP4-SC100; Mega Pack 04; UR
@description Creates a wikitext link.
+
}}
@parameter {string} page Page name to link.
 
@parameter {string|nil} label Label for the link. If unspecified,
 
the label used will be `page` with its dab removed.
 
@return {string} Wikilink.
 
]]
 
function U.link( page, label )
 
return ('[[%s|%s]]'):format(
 
page:gsub( '#', '' ),
 
label or U.removeDab( page )
 
);
 
end
 
  
--[[Doc
+
{{Card gallery|region=KR|
@function U italicLink
+
EP19-KR044; Extra Pack 2019; ScR; UE
@description Creates a wikitext link, italicized. Usually used
+
SLT1-KR026; Selection 10; C; UE
to display linked set names, etc..
+
}}
@parameter {string} page Page name to link.
 
@parameter {string|nil} label Label for the link. If unspecified,
 
the label used will be `page` with its dab removed and italicized.
 
Else, will italicize the label given.
 
@return {string} Italicized wikilink.
 
]]
 
function U.italicLink( page, label )
 
return U.link(
 
page,
 
label and U.italic( label ) or U.italicNoDab(
 
U.removeDab( page ) -- TODO there's some twisted logic here for edge cases of multiple dabs. Check this.
 
)
 
)
 
end
 
  
--[[Doc
+
{{Card gallery|region=JP|type=Video game|
@function U getDab
+
LOD2 :: artwork
@description Gets the dab text of a title, if it has dab.
+
}}
@parameter {string} title Page title to get the dab from.
 
@return {string} Dab for `title`.
 
]]
 
function U.getDab( title )
 
return title:match( '%(([^%(]*)%)%s*$' ) or '';
 
end
 
  
--[[Doc
+
{{Card gallery|region=EN|type=Video game|
@function U removeDab
+
MADU :: artwork
@description Removes the dab text of a title.
+
}}
@parameter {string} title Page title to get its dab removed.
 
@return {string} `title` with its dab removed.
 
]]
 
function U.removeDab( title )
 
return title:gsub( '%s*%(([^%(]*)%)%s*$', '' );
 
end
 
  
--[[Doc
+
{{Card gallery|region=JP|type=Other|
@function isSomething
+
TimeThiefPerpetua-OW.png // twitter::1169550743679823874
@description Meta-function for type checkers.
+
}}
@parameter {*} toCompare Anything to type-compare.
 
@parameter {*} compareTo Specific thing to be type-compared with.
 
@return {boolean} If `toCompare` and `compareTo` have the same type.
 
]]
 
local function isSomething( toCompare, compareTo )
 
return type( toCompare ) == type( compareTo );
 
end
 
 
 
--[=[Doc
 
@function U isBoolean
 
@description Checks if it's a `boolean` type.
 
@parameter {*} v Any value to check if it's a `boolean`.
 
@return {boolean} If `v` type is `boolean`.
 
@see [[#isSomething]]
 
]=]
 
function U.isBoolean( v )
 
return isSomething( v, true );
 
end
 
 
 
--[=[Doc
 
@function U isFunction
 
@description Checks if it's a `function` type.
 
@parameter {*} v Any value to check if it's a `function`.
 
@return {boolean} If `v` type is `function`.
 
@see [[#isSomething]]
 
]=]
 
function U.isFunction( v )
 
return isSomething( v, function() end );
 
end
 
 
 
--[=[Doc
 
@function U isNil
 
@description Checks if it's `nil`.
 
@parameter {*} v Any value to check if it's `nil`.
 
@return {boolean} If `v` type `nil`.
 
@see [[#isSomething]]
 
]=]
 
function U.isNil( v )
 
return isSomething( v, nil );
 
end
 
 
 
--[=[Doc
 
@function U isNumber
 
@description Checks if it's a `number` type.
 
@parameter {*} v Any value to check if it's a `number`.
 
@return {boolean} If `v` type is `number`.
 
@see [[#isSomething]]
 
]=]
 
function U.isNumber( v )
 
return isSomething( v, 1 );
 
end
 
 
 
--[=[Doc
 
@function U isString
 
@description Checks if it's a `string` type.
 
@parameter {*} v Any value to check if it's a `string`.
 
@return {boolean} If `v` type is `string`.
 
@see [[#isSomething]]
 
]=]
 
function U.isString( v )
 
return isSomething( v, '' );
 
end
 
 
 
--[=[Doc
 
@function U isTable
 
@description Checks if it's a `table` type.
 
@parameter {*} v Any value to check if it's a `table`.
 
@return {boolean} If `v` type is `table`.
 
@see [[#isSomething]]
 
]=]
 
function U.isTable( v )
 
return isSomething( v, {} );
 
end
 
 
 
--[[Doc
 
@function U isEmpty
 
@description Checks if it's a empty.
 
@parameter {*} v Any value to check if it's empty.
 
@return {boolean} If `v` type is empty.
 
@todo Keep this? Check dependencies.
 
]]
 
function U.isEmpty( v )
 
return (
 
U.isString( v ) and mwTextTrim( v ) == ''
 
or
 
U.isTable( v ) and U.count( v ) == 0
 
);
 
end
 
 
 
-- @name validate
 
-- @description Asserts if a value has content.
 
--[[function U.validate( v )
 
-- If boolean, function or nil, just return them:
 
if U.isBoolean( v ) or U.isFunction( v ) or U.isNil( v ) then return v end
 
-- If number, it is accepted if it's not 0:  
 
if U.isNumber( v ) then return v ~= 0 and v end
 
-- If string, it is accepted if it's not the empty string:
 
if U.isString( v ) then return mwTextTrim( v ) ~= '' and v end
 
-- If table, it is accepted if it has elements:
 
if U.isTable( v ) then return  U.count( v ) ~= 0 and v end
 
end--]]
 
 
 
--[[Doc
 
@function U wrapInQuotes
 
@description Wraps a name in quotes, based on the language.
 
@parameter {string|nil} name A name to wrap in quotes.
 
@parameter {string} ln A language index.
 
@return {string} Wrapped `name`. If `name` is `nil`, returns the empty string.
 
@todo Accept `ln` as a language struct.
 
]]
 
function U.wrapInQuotes( name, ln )
 
if not U.trim( name ) then
 
return '';  --  Return empty string.
 
end
 
 
 
return (ln ~= 'ja' and ln ~= 'zh')
 
and table.concat( { '"', name, '"' } )
 
or  table.concat( { '「', name, '」' } )
 
;
 
end
 
 
 
--[[Doc
 
@function U formatParameter
 
@description
 
@parameter {string} v
 
@return {string}
 
]]
 
function U.formatParameter( v )
 
return ('&#123;&#123;&#123;%s&#125;&#125;&#125;'):format( v );
 
end
 
 
 
--[[function U.processArgs( frame, ... )
 
return
 
end]]
 
-- @name getArgs
 
-- @description Parses arguments.
 
-- @see [[Module:Arguments]]
 
function U.getArgs( ... )
 
return require( 'Module:Arguments' ).getArgs( ... );
 
end
 
 
 
-- @name getName
 
-- @description Gets the localized name of a card, set or character.
 
-- @see [[Module:Name]]
 
function U.getName( ... )
 
return require( 'Module:Name' ).main( ... );
 
end
 
 
 
-- @name getImgName
 
-- @description Gets the localized name of a card, set or character.
 
-- @see [[Module:Name]]
 
function U.getImgName( ... )
 
return require( 'Module:Card image name' ).main( ... );
 
end
 
 
 
----------
 
-- Return:
 
----------
 
--[[Doc
 
@exports Util library (`U`).
 
]]
 
return U;
 
-- </pre>
 

Revision as of 07:31, 16 March 2023