Module:Currency
De Semantic MediaWiki - Sandbox
La documentation pour ce module peut être créée à Module:Currency/doc
-- See also [[Template:Monetary]]
-- Currency exchange rates, thousands, millions etc:
-- Numerals:
local data = {
-- Exported function names:
API = {'declare', 'Declare'},
-- Errors:
noNumber = "Pass an argument",
-- Arguments:
argNumber = 1, -- number
-- 000:
mils = {
[1] = {''},
[1000] = {'thousand', 'thousands', 'K'},
[1000000] = {'mln', 'million', 'millions', 'M'},
[1000000000] = {'bln', 'billion', 'billions', 'G'}
}, -- mils = {...}
prop = 'Corresponds to'
} -- local data = {...}
local function formatNumeralError (message, ...)
if select('#', ... ) > 0 then
message = string.format(message, ...)
end
return "<span class=\"error\">" .. message .. "</span>"
end
local function implementCurrency (names, rate)
local lang = mw.getContentLanguage ()
local declarations = {}
for ratio, numerals in pairs (data.mils) do
local units = {}
for _, numeral in ipairs (numerals) do
for __, name in ipairs (names) do
units [#units + 1] = numeral .. ' ' .. name
end
end
declarations [#declarations + 1] = '*** [[' .. data.prop .. '::' .. lang:formatNum (1 / rate / ratio) .. ' ' .. table.concat (units, ', ') .. '|' .. units [1] .. ']]'
end
return declarations
end -- local function implementCurrency (names, rate)
-- Загрузка аргументов и вызов реализации:
local function declareCurrency (args)
local ok
local rate = 1
local names = {}
-- Загрузка и проверка аргументов:
for _, arg in ipairs (args) do
if tonumber (arg) ~= nil then
-- Exchange rate:
rate = tonumber (arg)
else
-- Currency symbol or name:
names [#names + 1] = arg
end
end
return table.concat (implementCurrency (names, rate), '\n')
end -- local function declareCurrency (args)
-- Export function:
local m = {}
for _, alias in pairs (data.API) do
m [alias] = function (frame)
return declareCurrency (frame.args)
end
end
return m