<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh-Hans-CN">
	<id>https://wiki.statsape.com/index.php?action=history&amp;feed=atom&amp;title=%E6%A8%A1%E5%9D%97%3ACommon</id>
	<title>模块:Common - 版本历史</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.statsape.com/index.php?action=history&amp;feed=atom&amp;title=%E6%A8%A1%E5%9D%97%3ACommon"/>
	<link rel="alternate" type="text/html" href="https://wiki.statsape.com/index.php?title=%E6%A8%A1%E5%9D%97:Common&amp;action=history"/>
	<updated>2026-11-10T17:44:45Z</updated>
	<subtitle>本wiki上该页面的版本历史</subtitle>
	<generator>MediaWiki 1.39.6</generator>
	<entry>
		<id>https://wiki.statsape.com/index.php?title=%E6%A8%A1%E5%9D%97:Common&amp;diff=3792&amp;oldid=prev</id>
		<title>Zeroclanzhang：​创建页面，内容为“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 ) == &#039;string&#039; then         numCopy = num:gsub( &#039;,&#039;, &#039;.&#039; )     end      numCopy = ton…”</title>
		<link rel="alternate" type="text/html" href="https://wiki.statsape.com/index.php?title=%E6%A8%A1%E5%9D%97:Common&amp;diff=3792&amp;oldid=prev"/>
		<updated>2023-12-13T08:19:10Z</updated>

		<summary type="html">&lt;p&gt;创建页面，内容为“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 ) == &amp;#039;string&amp;#039; then         numCopy = num:gsub( &amp;#039;,&amp;#039;, &amp;#039;.&amp;#039; )     end      numCopy = ton…”&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新页面&lt;/b&gt;&lt;/p&gt;&lt;div&gt;local common = {}&lt;br /&gt;
&lt;br /&gt;
--- Convert an input to number&lt;br /&gt;
--- @param num string|integer Input&lt;br /&gt;
--- @param onFail string|integer Output if conversion fails&lt;br /&gt;
--- @param base integer Base for tonumber, defaults to 10&lt;br /&gt;
--- @return number&lt;br /&gt;
function common.toNumber( num, onFail, base )&lt;br /&gt;
	base = base or 10&lt;br /&gt;
&lt;br /&gt;
    if num == nil then&lt;br /&gt;
        return onFail&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
	local numCopy = num&lt;br /&gt;
    if type( num ) == &amp;#039;string&amp;#039; then&lt;br /&gt;
        numCopy = num:gsub( &amp;#039;,&amp;#039;, &amp;#039;.&amp;#039; )&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    numCopy = tonumber( numCopy, base )&lt;br /&gt;
&lt;br /&gt;
    if numCopy == nil then&lt;br /&gt;
        return onFail&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    return numCopy&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--- Formats a number according to the content language&lt;br /&gt;
--- @param num number|string&lt;br /&gt;
function common.formatNum( num, onFail )&lt;br /&gt;
    local converted = common.toNumber( num, false )&lt;br /&gt;
&lt;br /&gt;
    if converted == nil or converted == false then&lt;br /&gt;
    	return onFail or false&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
    local converted = mw.language.getContentLanguage():formatNum( converted )&lt;br /&gt;
&lt;br /&gt;
    return mw.ustring.gsub( converted, &amp;#039;−&amp;#039;, &amp;#039;-&amp;#039; )&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--- Removes (...) suffixes&lt;br /&gt;
--- @param pageName string&lt;br /&gt;
--- @param suffix string|table&lt;br /&gt;
function common.removeTypeSuffix( pageName, suffix )&lt;br /&gt;
    if type( suffix ) == &amp;#039;table&amp;#039; then&lt;br /&gt;
        for _, toRemove in pairs( suffix ) do&lt;br /&gt;
            pageName = common.removeTypeSuffix( pageName, toRemove )&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return pageName&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    return mw.text.trim( pageName:gsub( &amp;#039;%(&amp;#039; .. suffix .. &amp;#039;%)&amp;#039;, &amp;#039;&amp;#039; ), &amp;#039;_ &amp;#039; )&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--- Checks if Setting SMW Data was successful&lt;br /&gt;
--- @param result table&lt;br /&gt;
function common.checkSmwResult( result )&lt;br /&gt;
	if result == nil then&lt;br /&gt;
        return&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
    if result ~= true and result.error ~= nil then&lt;br /&gt;
        error( &amp;#039;Semantic Mediawiki error &amp;#039; .. result.error )&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--- Checks if Api Request was successful and if the Response is valid&lt;br /&gt;
--- @param response table&lt;br /&gt;
--- @param errorOnData boolean&lt;br /&gt;
--- @param errorOnData boolean&lt;br /&gt;
--- @return boolean&lt;br /&gt;
function common.checkApiResponse( response, errorOnStatus, errorOnData )&lt;br /&gt;
    if response[ &amp;#039;status_code&amp;#039; ] ~= nil and response[ &amp;#039;status_code&amp;#039; ] ~= 200 then&lt;br /&gt;
        if errorOnStatus == nil or errorOnStatus == true then&lt;br /&gt;
            error( &amp;#039;API request returns the error code &amp;#039; .. response[ &amp;#039;status_code&amp;#039; ] .. &amp;#039;(&amp;#039; .. response[ &amp;#039;message&amp;#039; ] .. &amp;#039;)&amp;#039;, 0 )&lt;br /&gt;
        end&lt;br /&gt;
        return false&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if response[ &amp;#039;data&amp;#039; ] == nil then&lt;br /&gt;
        if errorOnData == nil or errorOnData == true then&lt;br /&gt;
            error( &amp;#039;API data does not contain a &amp;quot;data&amp;quot; field&amp;#039;, 0 )&lt;br /&gt;
        end&lt;br /&gt;
        return false&lt;br /&gt;
    end&lt;br /&gt;
    return true&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--- Walks a table in order, can be used like pairs()&lt;br /&gt;
--- @param t table&lt;br /&gt;
--- @param order function - Sorting function OPTIONAL&lt;br /&gt;
function common.spairs( t, order )&lt;br /&gt;
    -- collect the keys&lt;br /&gt;
    local keys = {}&lt;br /&gt;
    for k in pairs(t) do keys[#keys+1] = k end&lt;br /&gt;
&lt;br /&gt;
    -- if order function given, sort by it by passing the table and keys a, b,&lt;br /&gt;
    -- otherwise just sort the keys&lt;br /&gt;
    if order then&lt;br /&gt;
        table.sort(keys, function(a,b) return order(t, a, b) end)&lt;br /&gt;
    else&lt;br /&gt;
        table.sort(keys)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    -- return the iterator function&lt;br /&gt;
    local i = 0&lt;br /&gt;
    return function()&lt;br /&gt;
        i = i + 1&lt;br /&gt;
        if keys[i] then&lt;br /&gt;
            return keys[i], t[keys[i]]&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return common&lt;/div&gt;</summary>
		<author><name>Zeroclanzhang</name></author>
	</entry>
</feed>