Modulo:Conversione/sandbox: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
Nuova sandbox
 
Omega Bot (discussione | contributi)
m Bot: rimuovo no globals obsoleto
 
(3 versioni intermedie di un altro utente non mostrate)
Riga 6:
]]--
 
require('Modulo:No globalsstrict')
 
local cfg = mw.loadData('Modulo:Conversione/Configurazione/sandbox')
local p = {}
 
Riga 19:
 
local function getUnit(val, targetunitdata, args)
local ret, link, space
 
if args.showunit == 'long' then
ret = targetunitdata[3].symbol
elseif args.showunit == 'true'showunitlong then
-- unità per esteso
ret = val > 1 and targetunitdata[7].name2 or targetunitdata[6].name1
elseif args.showunit == 'true' then
ret = targetunitdata[3]
end
 
if args.showlink == 'true'showunitlink then
link = targetunitdata[5].link
elseif type(args.showlink) == 'string' then
link = args.showlink
end
 
returnspace ' '= (args.. (linkshowunit and '[[' .targetunitdata.nospace) link ..and '|' ..or ret ..' ']]' or ret)
 
return space .. (link and '[[' .. link .. '|' .. ret .. ']]' or ret)
end
 
-- Ritorna il valore convertito alla unità di misura e alle opzioni specificate
function p._main(valstrval, sourceunit, targetunit, args)
local sourceunitdataval, targetunitdatasourceunitdata, unittargetunitdata, cat
 
val = tonumber(valstrval)
-- se non è un numero ritorna la stringa non modificata
if not val then
return valstrval
end
if not args then
Riga 56:
targetunitdata = cfg.units[targetunit] or cfg.units[cfg.alias[targetunit]]
if sourceunitdata and targetunitdata then
if sourceunitdata[1].type ~= targetunitdata[1].type then
error('unità di misura incompatibili: ' .. sourceunitdata[1].type .. ' e ' .. targetunitdata[1].type)
end
val = val *if sourceunitdata[2] /== targetunitdata[2] then
-- nothing
elseif sourceunitdata.type == 'temperature' then
val = (val - sourceunitdata.offset) * sourceunitdata.scale
val = val / targetunitdata.scale + targetunitdata.offset
else
val = val * sourceunitdata.scale / targetunitdata.scale
end
val = val * sourceunitdata[2] / targetunitdata[2]
else
cat = '[[Categoria:Pagine con unità di misura non supportata]]'
end
 
-- eventuale arrotondamento
if args.rounding then
val = round(val, args.rounding)
end
 
-- formatnum
if targetunitdata and (args.showunit == 'true' or args.showunit == 'long') then
if args.formatnum then
unit = getUnit(val, targetunitdata, args)
val = mw.language.getContentLanguage():formatNum(val)
end
 
-- unità di misura
if targetunitdata and (args.showunit == 'true' or args.showunit == 'long'showunitlong) then
unitval = val .. getUnit(val, targetunitdata, args)
end
 
return val .. (unit or '') .. (cat or '')
end