Module:ConvertCapacity: Difference between revisions

From Computing Classics Wiki
Forgot the return p
m 4 revisions imported: All kinds of templates for CCW to emulate AEW's old hard drive stock
 
(One intermediate revision 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.

Latest revision as of 06:15, 24 September 2024

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
Cookies help us deliver our services. By using our services, you agree to our use of cookies.