Sari la conținut

Modul:Dim

De la Wikipedia, enciclopedia liberă

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

local function processUnit(unit)
	if mw.ustring.match(unit, '2$') then
		unit = mw.ustring.gsub(unit, '2$', '²')
		return unit
	end
	if mw.ustring.match(unit, '3$') then
		unit = mw.ustring.gsub(unit, '3$', '³')
		return unit
	end
	return unit
end

p.fromArgs = function(low, up, unit)
	local numValue = table.concat(TableTools.compressSparseArray({nullSafeFormat(low), nullSafeFormat(up)}), '–')
	return table.concat(TableTools.compressSparseArray({numValue, processUnit(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