检查到模板循环:模块:Common/doc
local common = {}
--- Convert an input to number
--- @param num string|integer Input
--- @param onFail string|integer Output if conversion fails
--- @param base integer Base for tonumber, defaults to 10
--- @return number
function common.toNumber( num, onFail, base )
base = base or 10
if num == nil then
return onFail
end
local numCopy = num
if type( num ) == 'string' then
numCopy = num:gsub( ',', '.' )
end
numCopy = tonumber( numCopy, base )
if numCopy == nil then
return onFail
end
return numCopy
end
--- Formats a number according to the content language
--- @param num number|string
function common.formatNum( num, onFail )
local converted = common.toNumber( num, false )
if converted == nil or converted == false then
return onFail or false
end
local converted = mw.language.getContentLanguage():formatNum( converted )
return mw.ustring.gsub( converted, '−', '-' )
end
--- Removes (...) suffixes
--- @param pageName string
--- @param suffix string|table
function common.removeTypeSuffix( pageName, suffix )
if type( suffix ) == 'table' then
for _, toRemove in pairs( suffix ) do
pageName = common.removeTypeSuffix( pageName, toRemove )
end
return pageName
end
return mw.text.trim( pageName:gsub( '%(' .. suffix .. '%)', '' ), '_ ' )
end
--- Checks if Setting SMW Data was successful
--- @param result table
function common.checkSmwResult( result )
if result == nil then
return
end
if result ~= true and result.error ~= nil then
error( 'Semantic Mediawiki error ' .. result.error )
end
end
--- Checks if Api Request was successful and if the Response is valid
--- @param response table
--- @param errorOnData boolean
--- @param errorOnData boolean
--- @return boolean
function common.checkApiResponse( response, errorOnStatus, errorOnData )
if response[ 'status_code' ] ~= nil and response[ 'status_code' ] ~= 200 then
if errorOnStatus == nil or errorOnStatus == true then
error( 'API request returns the error code ' .. response[ 'status_code' ] .. '(' .. response[ 'message' ] .. ')', 0 )
end
return false
end
if response[ 'data' ] == nil then
if errorOnData == nil or errorOnData == true then
error( 'API data does not contain a "data" field', 0 )
end
return false
end
return true
end
--- Walks a table in order, can be used like pairs()
--- @param t table
--- @param order function - Sorting function OPTIONAL
function common.spairs( t, order )
-- collect the keys
local keys = {}
for k in pairs(t) do keys[#keys+1] = k end
-- if order function given, sort by it by passing the table and keys a, b,
-- otherwise just sort the keys
if order then
table.sort(keys, function(a,b) return order(t, a, b) end)
else
table.sort(keys)
end
-- return the iterator function
local i = 0
return function()
i = i + 1
if keys[i] then
return keys[i], t[keys[i]]
end
end
end
return common