Module:Convert default: Difference between revisions
Appearance
Content deleted Content added
wrong variable name |
unit resolution, proper indexing |
||
Line 1: | Line 1: | ||
local getArgs = require('Module:Arguments').getArgs |
local getArgs = require('Module:Arguments').getArgs |
||
local TableTools = require('Module:TableTools') |
local TableTools = require('Module:TableTools') |
||
local ConvertData = require('Module:Convert/data') |
local ConvertData = require('Module:Convert/data').all_units |
||
local p = {} |
local p = {} |
||
Line 12: | Line 12: | ||
return p._main(frame, args) |
return p._main(frame, args) |
||
end |
|||
function resolveUnit( unit ) |
|||
-- Resolves a unit by name to its canonical name. |
|||
local unitData = ConvertData[unit] |
|||
if unitData then |
|||
if unitData["target"] then |
|||
-- Unit alias |
|||
return unitData["target"] |
|||
else |
|||
-- Not an alias |
|||
return unit |
|||
end |
|||
else |
|||
-- Not a valid unit |
|||
return nil |
|||
end |
|||
end |
end |
||
Line 24: | Line 41: | ||
-- Set the input value and unit |
-- Set the input value and unit |
||
local resolvedFrom = resolveUnit( from ) |
|||
convertArgs[1] = input |
convertArgs[1] = input |
||
convertArgs[2] = |
convertArgs[2] = resolvedFrom |
||
-- Wipe custom arguments |
-- Wipe custom arguments |
||
Line 31: | Line 49: | ||
convertArgs["defaultUnit"] = nil |
convertArgs["defaultUnit"] = nil |
||
convertArgs["default unit"] = nil |
convertArgs["default unit"] = nil |
||
convertArgs["default-units"] = nil |
|||
convertArgs["defaultUnits"] = nil |
|||
convertArgs["default units"] = nil |
|||
-- Remove duplicate units in output units (if available) |
-- Remove duplicate units in output units (if available) |
||
if convertArgs[3] ~= nil then |
if convertArgs[3] ~= nil then |
||
local outputUnits = mw.text.split( mw.text.trim( convertArgs[3] ), "%s" ) |
local outputUnits = mw.text.split( mw.text.trim( convertArgs[3] ), "%s" ) |
||
local j = |
local j = 1 |
||
while outputUnits[j] ~= nil do |
while outputUnits[j] ~= nil do |
||
mw.logObject(resolveUnit( outputUnits[j] ), resolvedFrom) |
|||
if resolveUnit( outputUnits[j] ) == resolvedFrom then |
|||
-- Duplicate unit. Remove it. |
-- Duplicate unit. Remove it. |
||
table.remove( outputUnits, j ) |
table.remove( outputUnits, j ) |
||
Line 53: | Line 75: | ||
function p._main(frame, args) |
function p._main(frame, args) |
||
local rawInput = mw.text.trim( args[1] ) |
local rawInput = mw.text.trim( args[1] ) |
||
local defaultUnit = args["default-unit"] or args["defaultUnit"] or args["default unit"] |
local defaultUnit = args["default-unit"] or args["defaultUnit"] or args["default unit"] or |
||
args["default-units"] or args["defaultUnits"] or args["default units"] |
|||
local numSection = mw.ustring.match( rawInput, "^-?[%d,e]+" ) |
local numSection = mw.ustring.match( rawInput, "^-?[%d,e]+" ) |
Revision as of 04:14, 13 May 2023
local getArgs = require('Module:Arguments').getArgs
local TableTools = require('Module:TableTools')
local ConvertData = require('Module:Convert/data').all_units
local p = {}
function p.main(frame)
local args = getArgs(frame, {
frameOnly = true,
trim = true
})
return p._main(frame, args)
end
function resolveUnit( unit )
-- Resolves a unit by name to its canonical name.
local unitData = ConvertData[unit]
if unitData then
if unitData["target"] then
-- Unit alias
return unitData["target"]
else
-- Not an alias
return unit
end
else
-- Not a valid unit
return nil
end
end
function callConvert( frame, args, input, from )
local convertArgs = TableTools.shallowClone( args )
-- Shift all numbered parameters up by 1.
local i = 2
while args[i] ~= nil do
convertArgs[i + 1] = args[i]
i = i + 1
end
-- Set the input value and unit
local resolvedFrom = resolveUnit( from )
convertArgs[1] = input
convertArgs[2] = resolvedFrom
-- Wipe custom arguments
convertArgs["default-unit"] = nil
convertArgs["defaultUnit"] = nil
convertArgs["default unit"] = nil
convertArgs["default-units"] = nil
convertArgs["defaultUnits"] = nil
convertArgs["default units"] = nil
-- Remove duplicate units in output units (if available)
if convertArgs[3] ~= nil then
local outputUnits = mw.text.split( mw.text.trim( convertArgs[3] ), "%s" )
local j = 1
while outputUnits[j] ~= nil do
mw.logObject(resolveUnit( outputUnits[j] ), resolvedFrom)
if resolveUnit( outputUnits[j] ) == resolvedFrom then
-- Duplicate unit. Remove it.
table.remove( outputUnits, j )
-- Do not increment; the next value will now have an index of `j`.
else
j = j + 1
end
end
convertArgs[3] = table.concat( outputUnits, " " )
end
return frame:expandTemplate{ title = "convert", args = convertArgs }
end
function p._main(frame, args)
local rawInput = mw.text.trim( args[1] )
local defaultUnit = args["default-unit"] or args["defaultUnit"] or args["default unit"] or
args["default-units"] or args["defaultUnits"] or args["default units"]
local numSection = mw.ustring.match( rawInput, "^-?[%d,e]+" )
if numSection == nil then
-- Raw output (not a determinable number)
return rawInput
end
if mw.ustring.len( rawInput ) == mw.ustring.len( mw.text.trim( numSection ) ) then
-- There only exists a number section.
-- Convert, and use the default unit (if defined).
if not defaultUnit then
return error("No default unit is set, and a unitless input was provided.")
else
return callConvert( frame, args, numSection, defaultUnit )
end
else
-- There exists a text section.
-- Get that section.
local textSection = mw.text.trim(
string.sub( rawInput, mw.ustring.len( mw.text.trim( numSection ) ) + 1 )
)
return callConvert( frame, args, numSection, textSection )
end
end
return p