Module:ConvertCapacity: Difference between revisions
From Australian Enthusiasts Wiki
Spin off from Module:Validate platter capacity |
Another dependency forgotten |
||
| (2 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
-- Return the first index with the given value (or nil if not found). | |||
function p.indexOf(array, value) | |||
for i, v in ipairs(array) do | |||
if v == value then | |||
return i | |||
end | |||
end | |||
return nil | |||
end | |||
-- Convert a capacity from one target to another. | -- Convert a capacity from one target to another. | ||
| Line 14: | Line 24: | ||
return number * (1000 ^ stepsRemoved) | return number * (1000 ^ stepsRemoved) | ||
end | end | ||
-- Main entry point for this module. | |||
function p.invokeMain(frame) | |||
return p.convertCapacity(tonumber(frame.args[1]), frame.args[2], frame.args[3]) | |||
end | |||
return p | |||
Latest revision as of 22:02, 11 September 2022
Documentation for this module may be created at Module:ConvertCapacity/doc
local p = {}
-- Return the first index with the given value (or nil if not found).
function p.indexOf(array, value)
for i, v in ipairs(array) do
if v == value then
return i
end
end
return nil
end
-- Convert a capacity from one target to another.
function p.convertCapacity(number, from, to)
-- Return immediately if the unit from and to are the same.
if (from == to) then
return number
end
unitArray = {"mb", "mib", "gb", "gib", "tb", "tib", "eb", "eib"}
stepsRemoved = (p.indexOf(unitArray, from) - p.indexOf(unitArray, to)) / 2
return number * (1000 ^ stepsRemoved)
end
-- Main entry point for this module.
function p.invokeMain(frame)
return p.convertCapacity(tonumber(frame.args[1]), frame.args[2], frame.args[3])
end
return p