Jump to content

Module:Val

विकिपीडिया से
imported>The Mol Man के द्वारा 03:14, 8 जनवरी 2015 के बदलाव
local p = {}
 
local getArgs
local delimit_groups = require('Module:Gapnum').groups
local makeunit = require('Module:Val/units')
local mSu = require('Module:Su')._main

function p.main(frame)
	if not getArgs then
		getArgs = require('Module:Arguments').getArgs
	end
	local args = getArgs(frame, {wrappers = 'Template:Val'})
	local number = {n=args[1], nend=args['end']}
	local uncertainty = {upper=args[2], lower=args[3],
							errend=args.errend,
							upperend=args['+errend'], lowerend=args['-errend']}
	local u_tbl = {u=args.ul or args.u, ul=args.ul ~= nil,
					p=args.upl or args.up, pl=args.upl ~= nil}
	local misc_tbl = {e=args.e, pre=args.p, suf=args.s, fmt=args.fmt or '', nocat=args.nocategory}
	return p._main(number,uncertainty,u_tbl,misc_tbl)
end

function p._main(number,uncertainty,u_tbl,misc_tbl)
	local fmt = misc_tbl.fmt
	local n = delimit(number.n,fmt)
	local e_10 = misc_tbl.e
	
	local unc
	local uncU, uncL = uncertainty.upper, uncertainty.lower
	if number.nend then
		n:wikitext(number.nend)
	end

	local paren_wrap = misc_tbl.e and (not uncL and (uncU and not uncU:find('%(')))
	
	if u_tbl.u then
		units = makeunit(u_tbl.u,
						{link=u_tbl.ul,
							per=u_tbl.p,
							per_link=u_tbl.pl})
	end
	local paren_uncertainty
	if uncU then
		if uncL then
			uncU = delimit(uncU,fmt)
			uncL = delimit(uncL,fmt)
			if not e_10 and units then
				uncU = uncU..units
				uncL = uncL..units
			end
			uncU = uncU..(uncertainty.upperend or '')
			uncL = uncL..(uncertainty.lowerend or '')
			unc = '<span style="margin-left:0.3em;">'..mSu(uncU,uncL)..'</span>'
		else
			local uncU_n = mw.ustring.match(uncU,('%((.+)%)')) or uncU
			if uncU == uncU_n then
				unc = '<span style="margin-left:0.3em;margin-right:0.15em">±</span>'..delimit(uncU_n,fmt)
				if not e_10 and units then
					unc = unc..units
				end
				unc = unc..'</span>'
			else
				unc = '('..delimit(uncU_n,fmt)..')'
				paren_uncertainty = true
			end
			if not e_10 and units then
				unc = unc..units
			end
		end
	end

	if not e_10 and units and not paren_uncertainty then
		n = n..units
	end
	
	if e_10 then
		e_10 = '<span style="margin-left:0.25em;margin-right:0.15em">×</span>10<sup>'..delimit(misc_tbl.e)..'</sup>'
		if units then
			e_10 = e_10..units
		end
	else
		e_10 = ''
	end
	local ret = { 
				misc_tbl.pre or '',
				paren_wrap and '(' or '',
				n,
				misc_tbl.nend or '',
				unc or '',
				paren_wrap and ')' or '',
				e_10,
				misc_tbl.suf or ''
			}
	ret = table.concat(ret)
	return ret
end

-- TODO: Add other format options
function delimit(n,fmt)
	local prefix,num
	if not fmt then fmt = '' end
	if n:find('[%-%+]') then
		prefix,num = mw.ustring.match(n,'([-+])([%d.]+)')
	else
		num = n
	end
	local ipart, dpart = delimit_groups(num)
	if fmt:lower() == 'commas' then
		num = table.concat(ipart,',')
		if dpart then
			dpart = table.concat(dpart)
			num = num..'.'..dpart
		end
	else
		num = {}
		num[1] = table.remove(ipart,1)
		for _, v in ipairs(ipart) do
			table.insert(num,'<span style="margin-left:.25em">'..v..'</span>')
		end
		if dpart then
			table.insert(num,'.'..table.remove(dpart,1))
			for _, v in ipairs(dpart) do
				table.insert(num,'<span style="margin-left:.25em">'..v..'</span>')
			end
		end
		num = table.concat(num)
	end
	if prefix then
		if prefix == '-' then
			prefix = '&minus;'
		end
		num = prefix..num
	end
	return num
end

return p