Module:Common

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 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 end

if response[ 'data' ] == nil then if errorOnData == nil or errorOnData == true then error( 'API data does not contain a "data" field', 0 ) end end end

return common