Module:Convert: Difference between revisions

Jump to navigation Jump to search
Content added Content deleted
(fix sigfig warning: if not empty, warning needs value not name; fix so does not execute following code once sigfig is handled)
(allow conversion between different unit types for certain whitelisted exceptions (torque/energy); emulate MediaWiki's template handling by accepting   and multiple spaces in a unit name)
Line 291: Line 291:
-- END: Code required only for built-in units.
-- END: Code required only for built-in units.
------------------------------------------------------------------------
------------------------------------------------------------------------

local function check_mismatch(unit1, unit2)
-- If unit1 cannot be converted to unit2, return an error message table.
-- This allows conversion between units of the same type, and between
-- Nm (normally torque) and ftlb (energy), as in gun-related articles.
-- This works because Nm is the base unit (scale = 1) for both the
-- primary type (torque), and the alternate type (energy, where Nm = J).
-- A match occurs if the primary types are the same, or if unit1 matches
-- the alternate type of unit2, and vice versa. That provides a whitelist
-- of which conversions are permitted between normally incompatible types.
if unit1.utype == unit2.utype or
(unit1.utype == unit2.alttype and unit1.alttype == unit2.utype) then
return nil
end
return { 'cvt_mismatch', unit1.utype, unit2.utype }
end


local function override_from(out_table, in_table, fields)
local function override_from(out_table, in_table, fields)
Line 436: Line 452:
-- If unitcode is a known combination code (and if allowed by what),
-- If unitcode is a known combination code (and if allowed by what),
-- a table of output multiple unit tables is included in the result.
-- a table of output multiple unit tables is included in the result.
-- For compatibility with the old template, underscores in unitcode are replaced
-- For compatibility with the old template, an underscore in a unitcode is
-- with spaces so {{convert|350|board_feet}} --> 350 board feet (0.83 m³).
-- replaced with a space so usage like {{convert|350|board_feet}} works.
-- Wikignomes may also put two spaces or " " in combinations, so
-- replace underscore, " ", and multiple spaces with a single space.
utable = utable or all_units
utable = utable or all_units
fails = fails or {}
fails = fails or {}
Line 450: Line 468:
return false, { 'cvt_no_unit' }
return false, { 'cvt_no_unit' }
end
end
unitcode = unitcode:gsub('_', ' ')
unitcode = unitcode:gsub('_', ' '):gsub(' ', ' '):gsub(' +', ' ')
local t = utable[unitcode]
local t = utable[unitcode]
if t then
if t then
Line 580: Line 598:
return false, { 'cvt_bad_unit', unitcode }
return false, { 'cvt_bad_unit', unitcode }
end
end
local utype
local result = { combination = {} }
local result = { combination = {} }
local cvt = result.combination
local cvt = result.combination
Line 586: Line 603:
local success, t = lookup(v, opt_sp_us, 'no_combination', utable, fails, depth)
local success, t = lookup(v, opt_sp_us, 'no_combination', utable, fails, depth)
if not success then return false, t end
if not success then return false, t end
if utype then
if i == 1 then
if utype ~= t.utype then
result.utype = t.utype
return false, { 'cvt_mismatch', utype, t.utype }
end
else
else
utype = t.utype
local mismatch = check_mismatch(result, t)
if mismatch then
return false, mismatch
end
end
end
cvt[i] = t
cvt[i] = t
end
end
result.utype = utype
return true, result
return true, result
end
end
Line 2,626: Line 2,643:
success, out_unit_table = lookup(out_unit, parms.opt_sp_us, 'any_combination')
success, out_unit_table = lookup(out_unit, parms.opt_sp_us, 'any_combination')
if not success then return false, out_unit_table end
if not success then return false, out_unit_table end
local mismatch = check_mismatch(in_unit_table, out_unit_table)
if in_unit_table.utype ~= out_unit_table.utype then
if mismatch then
return false, { 'cvt_mismatch', in_unit_table.utype, out_unit_table.utype }
return false, mismatch
end
end
local flipped = parms.opt_flip
local flipped = parms.opt_flip