உள்ளடக்கத்துக்குச் செல்

Module:Val

கட்டற்ற கலைக்களஞ்சியமான விக்கிப்பீடியாவில் இருந்து.

This module implements {{Val}}.

The following modules are developed:

Use {{val/sandbox}} for testing, for example:

  • {{val/sandbox|1234.5678|(23)|u=cm}}1234.5678(23) cm
  • {{val/sandbox|1234.5678|1.23|u=cm}}1234.5678±1.23 cm
  • {{val/sandbox|1234.5678|1.23|4.56|u=cm}}1234.5678+1.23
    −4.56
     cm
  • {{val/sandbox|1234.5678|e=3|u=cm}}1234.5678×103 cm
  • {{val/sandbox|1234.5678|(23)|e=3|u=cm}}1234.5678(23)×103 cm
  • {{val/sandbox|1234.5678|1.23|e=3|u=cm}}(1234.5678±1.23)×103 cm
  • {{val/sandbox|1234.5678|1.23|4.56|e=3|u=cm}}1234.5678+1.23
    −4.56
    ×103 cm
  • {{val/sandbox|1234.5678|1.23|4.56|e=3|u=cm|end=$|+errend=U$|-errend=L$}}1234.5678$+1.23U$
    −4.56L$
    ×103 cm
  • {{val/sandbox|1234.5678|(23)|u=deg}}1234.5678(23)°
  • {{val/sandbox|1234.5678|1.23|u=deg}}1234.5678°±1.23°
  • {{val/sandbox|1234.5678|1.23|4.56|u=deg}}1234.5678°+1.23°
    −4.56°
  • {{val/sandbox|1234.5678|e=3|u=deg}}1234.5678°×103
  • {{val/sandbox|1234.5678|(23)|e=3|u=deg}}1234.5678(23)°×103
  • {{val/sandbox|1234.5678|1.23|e=3|u=deg}}(1234.5678°±1.23°)×103
  • {{val/sandbox|1234.5678|1.23|4.56|e=3|u=deg}}1234.5678°+1.23°
    −4.56°
    ×103
  • {{val/sandbox|1234.5678|1.23|4.56|e=3|u=deg|end=$|+errend=U$|-errend=L$}}1234.5678$°+1.23U$°
    −4.56L$°
    ×103

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 nocat = args.nocategory
	
	-- Error checking
	if not validnumber(args[1]) then
		return valerror('Error in {{Val}}: first argument is not a valid number.',nocat)
	end
	if args[2] and not validnumber(args[2]) then
		return valerror('Error in {{Val}}: second argument is not a valid number.',nocat)
	end
	if args[3] and not validnumber(args[3]) then
		return valerror('Error in {{Val}}: third argument is not a valid number.',nocat)
	end
	-- Negative third param
	if args[3] and (not mw.ustring.find(args[3],find('[−%-]')) or mw.ustring.find(args[3],'^%D0')) then
		return valerror('Error in {{Val}}: third argument is not negative.',nocat)
	end
	if args.e and not validnumber(args.e) then
		return valerror('Error in &#123;&#123;Val&#125;&#125;: exponent argument (<b>e</b>) is not a valid number.',nocat)
	end
	if args.u and args.ul then
		return valerror('Error in &#123;&#123;Val&#125;&#125;: unit (<b>u</b>) and units with link (<b>ul</b>) are both specified, only one is allowed.',nocat)
	end
	if args.up and args.upl then
		return valerror('Error in &#123;&#123;Val&#125;&#125;: unit per (<b>up</b>) and units per with link (<b>upl</b>) are both specified, only one is allowed.',nocat)
	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)
				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

function valerror(msg,nocat)
	local ret = mw.html.create('strong')
							:addClass('error')
							:wikitext(msg)
	-- Not in talk, user, user_talk, or wikipedia_talk
	if not nocat and not mw.title.getCurrentTitle():inNamespaces(1,2,3,5) then
		ret:wikitext('[[Category:Pages with incorrect formatting templates use]]')
	end
	return tostring(ret)
end

function validnumber(n)
	-- Look for a number that may be surrounded by parentheses or may have +/-
	n = mw.ustring.match(tostring(n),'^%(?[±%-%+]?([%d\.]+)%)?$')
	return tonumber(n) ~= nil
end
return p
"https://ta.wikipedia.org/w/index.php?title=Module:Val&oldid=2743767" இலிருந்து மீள்விக்கப்பட்டது