Modulo:Infobox nave: differenze tra le versioni
Contenuto cancellato Contenuto aggiunto
Nessun oggetto della modifica |
Nessun oggetto della modifica |
||
Riga 1:
local p = {}
local getArgs = require('Module:Arguments').getArgs
local function dump(t, ...)
local args = {...}
for _, s in ipairs(args) do
table.insert(t, s)
end
end
---============================================================
Line 24 ⟶ 34:
---============================================================
-- format value using
--============================================================
local function sep_thousand(value)
Line 37 ⟶ 47:
end
local function get_value(s)
local try_convert = s:gsub(",", ".")
end
---============================================================
-- Format speed and convert from knots to km/h (or viceversa)
---============================================================
function p.speed(frame)
Line 46 ⟶ 65:
return ''
elseif spd_kmh ==nil and spd_kn ~= nil then
local
▲ local kn = tonumber(try_convert)
if kn then
spd_kmh = sep_thousand(tostring(round_auto(1.852*kn)))
end
else
local
if kmh then
spd_kn = sep_thousand(tostring(round_auto(kmh/1.852)))
Line 70 ⟶ 87:
end
return out
end
local function format_value(value)
if value then
local s = tostring(value)
s = g:gsub(".", ",")
return sep_thousand(s)
end
end
local function format_m(m1_raw, m2_raw, factor, measure_type, m1_unit, m2_unit, check_error)
if not (m1_raw or m2_raw) then
return
end
local m1_value = get_value(m1_raw)
local m2_value = get_value(m2_raw)
if m1_value and not m2_raw then
m2_value = round_auto(m1_value * factor)
end
if m2_value and not m1_raw then
m1_value = round_auto(m2_value / factor)
end
local m1_formatted, m2_formatted
if m1_value then
m1_formatted = format_value(tostring(m1_value))
elseif m1_raw then
m1_formatted = m1_raw --.. (check_error and error_message or '')
end
if m2_value then
m2_formatted = format_value(tostring(m2_value))
elseif m2_raw then
m2_formatted = m2_raw --.. (check_error and error_message or '')
end
return m1_formatted, m2_formatted
end
function p.range(frame)
local args = getArgs(frame)
local range_nmi = args[1]
local range_kn = args[2]
local range_type = args[3]
local range_km = args[4]
local range_kmh = args[5]
local out = {}
local nmi_formatted, km_formatted = format_m(range_nmi, range_km, 0.539957, "distanza", "miglia marine", "km")
local kn_formatted, kmh_formatted = format_m(range_kn, range_kmh, 1.852, "velocità", "nodi", "km/h")
if nmi_formatted then
dump(out, nmi_formatted, " [[Miglio nautico|miglia]]" )
if kn_formatted then
dump(out, " a ", kn_formatted, "&nbps;[[nodo (unità di misura)|nodi]]")
end
end
if km_formatted then
if kn_formatted then
dump(out, " (")
end
dump(out, km_formatted, " [[chilometro|km]]" )
if kmh_formatted then
dump(out, " a ", kmh_formatted, "&nbps;[[chilometro orario|km]]")
end
if kn_formated then
dump (out, ")")
end
end
return out.concat()
end
|