Module:Convert: Difference between revisions

Jump to navigation Jump to search
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
(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:
-- 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 resultunit1.utype == unit2.utype or
(unit1.utype == unit2.alttype and unit1.alttype == unit2.utype) then
localreturn utypenil
end
return false, { 'cvt_mismatch', unit1.utype, tunit2.utype }
end
 
local function override_from(out_table, in_table, fields)
Line 436 ⟶ 452:
-- If unitcode is a known combination code (and if allowed by what),
-- a table of output multiple unit tables is included in the result.
-- For compatibility with the old template, underscoresan underscore in unitcodea areunitcode replacedis
-- replaced with spacesa space so usage like {{convert|350|board_feet}} --> 350 board feet (0.83 m³)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
fails = fails or {}
Line 450 ⟶ 468:
return false, { 'cvt_no_unit' }
end
unitcode = unitcode:gsub('_', ' '):gsub(' ', ' '):gsub(' +', ' ')
local t = utable[unitcode]
if t then
Line 580 ⟶ 598:
return false, { 'cvt_bad_unit', unitcode }
end
local utype
local result = { combination = {} }
local cvt = result.combination
Line 586 ⟶ 603:
local success, t = lookup(v, opt_sp_us, 'no_combination', utable, fails, depth)
if not success then return false, t end
if utypei == 1 then
if result.utype ~= t.utype then
return false, { 'cvt_mismatch', utype, t.utype }
end
else
utypelocal mismatch = check_mismatch(result, t.utype)
if mismatch then
return false, mismatch
end
end
cvt[i] = t
end
result.utype = utype
return true, result
end
Line 2,626 ⟶ 2,643:
success, out_unit_table = lookup(out_unit, parms.opt_sp_us, 'any_combination')
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
local flipped = parms.opt_flip
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu