Modul:Dim

De la Wikipedia, enciclopedia liberă
Sari la navigare Sari la căutare

Implementează {{Dim}}.


local formatNum = require('Modul:Formatnum')._formatNum
local getArgs = require('Modul:Arguments').getArgs
local TableTools = require('Modul:TableTools')

local nbsp = mw.text.decode(' ')

local p = {}

local function nullSafeFormat(num)
	if num ~= nil then
		return formatNum(num)
	end
	return nil
end

p.fromArgs = function(low, up, unit)
	local numValue = table.concat(TableTools.compressSparseArray({nullSafeFormat(low), nullSafeFormat(up)}), '–')
	return table.concat(TableTools.compressSparseArray({numValue, unit}), nbsp)
end

p.fromArray = function(args)
	local unit = args[3] or args[2]
	local lowerBound, upperBound
	if args[3] then
		unit = args[3]
		upperBound = args[2]
		lowerBound = args[1]
	else
		unit = args[2]
		upperBound = args[1]
	end
	
	return p.fromArgs(lowerBound, upperBound, unit)
end

p.fromFrame = function(frame)
	local args = getArgs(frame)
	return p.fromArray(args)
end

return p