Jump to content

Module:Val/units

विकिपीडिया से
Johnuniq (बातचीत | योगदान) (try new procedure: define units in a big string (easier to edit and fast); use enhanced method of getting unit information from Module:Convert; include "long scale" units from Template:Val/units) के द्वारा 09:46, 7 जुलाई 2015 के बदलाव
-- Definitions for units known to val (short scale; see below for long scale).
-- There must be two spaces (not tab characters) after each unit code, and
-- two spaces or tabs between columns.
-- There must be a blank line before the first unit and a newline after the last.
-- Unit code       Symbol                                Link                                   Flags
local builtin_units = [=[

"                  ″                                     second of arc                          NOSPACE
%                  %                                     percent                                NOSPACE
'                  ′                                     minute of arc                          NOSPACE
°                  °                                     degree (angle)                         NOSPACE
‰                  ‰                                     per mil                                NOSPACE
′                  ′                                     minute of arc                          NOSPACE
″                  ″                                     second of arc                          NOSPACE
µA                 µA                                    microampere
µB                 µ<sub>B</sub>                         Bohr magneton
µG                 µG                                    microgauss
µGal               µGal                                  Gal (unit)
µHz                µHz                                   hertz
µK                 µK                                    microkelvin
µN                 µ<sub>N</sub>                         nuclear magneton
µT                 µT                                    microtesla
µV                 µV                                    microvolt
μg/dL              μg/dL                                 gram per litre
μg/dl              μg/dL                                 gram per litre
μN                 µ<sub>N</sub>                         nuclear magneton
A                  A                                     ampere
aA                 aA                                    attoampere
AD                 AD                                    Anno Domini
aG                 aG                                    attogauss
ag                 ag                                    attogram
aHz                aHz                                   attohertz
aK                 aK                                    attokelvin
am                 am                                    attometre
arcmin             ′                                     minute of arc                          NOSPACE
arcminute          ′                                     minute of arc                          NOSPACE
arcsec             ″                                     second of arc                          NOSPACE
arcsecond          ″                                     second of arc                          NOSPACE
aT                 aT                                    attotesla
BC                 BC                                    Before Christ
BCE                BCE                                   Before the Common Era
bit/s              bit/s                                 bit per second
BP                 BP                                    Before present
bps                bit/s                                 bit per second
bya                bya                                   billion years ago
byr                byr                                   billion years
c                  <i>c</i>                              speed of light
C                  C                                     coulomb
cA                 cA                                    centiampere
CE                 CE                                    Common Era
cG                 cG                                    centigauss
cg                 cg                                    centigram
cHz                cHz                                   centihertz
cK                 cK                                    centikelvin
cm-1               cm<sup>&minus;1</sup>                 wavenumber
cT                 cT                                    centitesla
cV                 cV                                    volt
dA                 dA                                    ampere
daA                daA                                   decaampere
daG                daG                                   decagauss
dag                dag                                   decagram
daHz               daHz                                  decahertz
daK                daK                                   decakelvin
daT                daT                                   decatesla
daV                daV                                   volt
deg                °                                     degree (angle)                         NOSPACE
degC               °C                                    degree Celsius
degF               °F                                    Fahrenheit
degR               °R                                    Rankine scale
dG                 dG                                    decigauss
dg                 dg                                    decigram
dHz                dHz                                   hertz
dK                 dK                                    decikelvin
dT                 dT                                    decitesla
dV                 dV                                    volt
e                  <i>e</i>                              Elementary charge
EA                 EA                                    ampere
Earth mass         <i>M</i><sub>⊕</sub>                  Earth mass
Earth radius       <i>R</i><sub>⊕</sub>                  Earth radius
EG                 EG                                    exagauss
Eg                 Eg                                    exagram
EHz                EHz                                   exahertz
EK                 EK                                    exakelvin
ET                 ET                                    exatesla
EV                 EV                                    volt
eV/c2              eV/<i>c</i><sup>2</sup>               electronvolt#Mass
F                  F                                     farad
fA                 fA                                    femtoampere
fb-1               fb<sup>&minus;1</sup>                 Barn (unit)
fG                 fG                                    femtogauss
fg                 fg                                    femtogram
fHz                fHz                                   femtohertz
fK                 fK                                    femtokelvin
fT                 fT                                    femtotesla
fV                 fV                                    femtovolt
g*mol-1            g&sdot;mol<sup>&minus;1</sup>         molar mass
g/cm3              g/cm<sup>3</sup>                      gram per cubic centimetre
g/l                g/l                                   gram per litre
g/L                g/L                                   gram per litre
g/mol              g&sdot;mol<sup>&minus;1</sup>         molar mass
GA                 GA                                    gigaampere
Ga                 Ga                                    gigaannum
gal                gal                                   gallon
Gbit/s             Gbit/s                                gigabit per second
GeV/c2             GeV/<i>c</i><sup>2</sup>              electronvolt#Mass
GG                 GG                                    gigagauss
Gg                 Gg                                    gigagram
GHz                GHz                                   gigahertz
GJ*mol-1           GJ&sdot;mol<sup>&minus;1</sup>        joule per mole
GJ/mol             GJ/mol                                joule per mole
GK                 GK                                    gigakelvin
GPa                GPa                                   gigapascal
Gpc                Gpc                                   megaparsec#Megaparsecs and gigaparsecs
Gpc2               Gpc<sup>2</sup>                       megaparsec#Megaparsecs and gigaparsecs
Gpc3               Gpc<sup>3</sup>                       megaparsec#Megaparsecs and gigaparsecs
GT                 GT                                    gigatesla
GV                 GV                                    gigavolt
Gyr                Ga                                    gigaannum
H                  H                                     Henry (unit)
H*m-1              H                                     Henry (unit)
hA                 hA                                    hectoampere
hG                 hG                                    hectogauss
hg                 hg                                    hectogram
hHz                hHz                                   hectohertz
hK                 hK                                    hectokelvin
hT                 hT                                    hectotesla
hV                 hV                                    volt
Hz                 Hz                                    hertz
J*mol-1            J&sdot;mol<sup>&minus;1</sup>         joule per mole
J*s                J&sdot;s                              joule-second
J/kg               J/kg                                  joule
J/mol              J/mol                                 joule per mole
Js                 J&sdot;s                              joule-second
Jupiter mass       <i>M</i><sub>J</sub>                  Jupiter mass
Jupiter radius     <i>R</i><sub>J</sub>                  Jupiter radius
kA                 kA                                    kiloampere
ka                 ka                                    kiloannum
kbit/s             kbit/s                                kilobit per second
keV/c2             keV/<i>c</i><sup>2</sup>              electronvolt#Mass
kG                 kG                                    kilogauss
kg/cm3             kg/cm<sup>3</sup>                     density#Formula and common units
kg/m3              kg/m<sup>3</sup>                      kilogram per cubic metre
kg/mol             kg&sdot;mol<sup>&minus;1</sup>        molar mass
kHz                kHz                                   Kilohertz
kJ*mol-1           kJ&sdot;mol<sup>&minus;1</sup>        joule per mole
kJ/mol             kJ/mol                                joule per mole
kK                 kK                                    kilokelvin
KPa                kPa                                   pascal (unit)
kpc                kpc                                   kiloparsec
kpc2               kpc<sup>2</sup>                       kiloparsec
kpc3               kpc<sup>3</sup>                       kiloparsec
kT                 kT                                    kilotesla
kV                 kV                                    kilovolt
kW.h               kW&sdot;h                             kilowatt hour
kWh                kWh                                   kilowatt hour
kW·h               kW&sdot;h                             kilowatt hour
kya                kya                                   kyr
kyr                kyr                                   kyr
L_solar            <i>L</i><sub>&#x2609;</sub>           solar luminosity
lbf                {{lbf}}                               Pound (force)
lbm                {{lbm}}                               Pound (mass)
ly                 ly                                    light-year
m                  m                                     metre
m*s-1              m&sdot;s<sup>&minus;1</sup>           metre per second
m*s-2              m&sdot;s<sup>&minus;2</sup>           metre per second squared
m-1                m<sup>&minus;1</sup>                  metre
m-2                m<sup>&minus;2</sup>                  square metre
m-3                m<sup>&minus;3</sup>                  cubic metre
m2                 m<sup>2</sup>                         Square metre
m3                 m<sup>3</sup>                         Cubic metre
M_Earth            <i>M</i><sub>⊕</sub>                  Earth mass
M_Jupiter          <i>M</i><sub>J</sub>                  Jupiter mass
M_solar            <i>M</i><sub>&#x2609;</sub>           solar mass
M_Solar            <i>M</i><sub>&#x2609;</sub>           solar mass
MA                 MA                                    megaampere
Ma                 Ma                                    megaannum
mA                 mA                                    milliampere
Mbit/s             Mbit/s                                megabit per second
mcg                µg                                    microgram
mcg/dL             µg/dL                                 gram per
mcg/dl             µg/dl                                 gram per
meter              m                                     metre
metre              m                                     metre
MeV/c2             MeV/<i>c</i><sup>2</sup>              electronvolt#Mass
MG                 MG                                    megagauss
Mg                 Mg                                    megagram
mG                 mG                                    milligauss
mg/ml              mg/mL                                 gram per litre
mg/mL              mg/mL                                 gram per litre
mHz                mHz                                   hertz
MHz                MHz                                   megahertz
MJ*mol-1           MJ&sdot;mol<sup>&minus;1</sup>        joule per mole
MJ/mol             MJ/mol                                joule per mole
MK                 MK                                    megakelvin
mK                 mK                                    millikelvin
Mpc                Mpc                                   parsec#Megaparsecs and gigaparsecs
Mpc2               Mpc<sup>2</sup>                       parsec#Megaparsecs and gigaparsecs
Mpc3               Mpc<sup>3</sup>                       parsec#Megaparsecs and gigaparsecs
MT                 MT                                    megatesla
mT                 mT                                    millitesla
muA                µA                                    ampere
muG                µG                                    microgauss
mug                µg                                    microgram
muGal              µGal                                  Gal (unit)
muHz               µHz                                   hertz
muJ                µJ                                    joule
muK                µK                                    microkelvin
mum                µm                                    micrometre
mus                µs                                    microsecond
muT                µT                                    microtesla
muV                µV                                    volt
MV                 MV                                    volt
mV                 mV                                    volt
mya                mya                                   million years ago
myr                myr                                   million years
N*A-2              N&sdot;A<sup>&minus;2</sup>           permeability (electromagnetism)
N*m-1              N&sdot;m<sup>&minus;1</sup>           joule (unit)
nA                 nA                                    ampere
nG                 nG                                    nanogauss
ng                 ng                                    nanogram
nHz                nHz                                   hertz
nK                 nK                                    nanokelvin
nT                 nT                                    nanotesla
nV                 nV                                    volt
pA                 pA                                    ampere
PA                 PA                                    ampere
pc2                pc<sup>2</sup>                        Parsec
pc3                pc<sup>3</sup>                        Parsec
per cent           %                                     percent                                NOSPACE
per mil            ‰                                     per mil                                NOSPACE
per mill           ‰                                     per mil                                NOSPACE
per mille          ‰                                     per mil                                NOSPACE
percent            %                                     percent                                NOSPACE
permil             ‰                                     per mil                                NOSPACE
permill            ‰                                     per mil                                NOSPACE
permille           ‰                                     per mil                                NOSPACE
PG                 PG                                    petagauss
Pg                 Pg                                    petagram
pG                 pG                                    picogauss
pg                 pg                                    picogram
pHz                pHz                                   hertz
PHz                PHz                                   petahertz
PK                 PK                                    petakelvin
pK                 pK                                    picokelvin
ppb                ppb                                   parts per billion
ppm                ppm                                   parts per million
PT                 PT                                    petatesla
pT                 pT                                    picotesla
PV                 PV                                    volt
pV                 pV                                    volt
R                  R                                     rayleigh (unit)
R_Earth            <i>R</i><sub>⊕</sub>                  Earth radius
R_Jupiter          <i>R</i><sub>J</sub>                  Jupiter radius
R_solar            <i>R</i><sub>&#x2609;</sub>           solar radius
R_Solar            <i>R</i><sub>&#x2609;</sub>           solar radius
s                  s                                     second
s-1                s<sup>&minus;1</sup>                  second
s-2                s<sup>&minus;2</sup>                  second
s2                 s<sup>2</sup>                         second
second             s                                     second
seconds            s                                     second
solar luminosity   <i>L</i><sub>&#x2609;</sub>           solar luminosity
Solar luminosity   <i>L</i><sub>&#x2609;</sub>           solar luminosity
solar mass         <i>M</i><sub>&#x2609;</sub>           solar mass
Solar mass         <i>M</i><sub>&#x2609;</sub>           solar mass
solar radius       <i>R</i><sub>&#x2609;</sub>           solar radius
Solar radius       <i>R</i><sub>&#x2609;</sub>           solar radius
TA                 TA                                    ampere
Tbit/s             Tbit/s                                terabit per second
TeV/c2             TeV/<i>c</i><sup>2</sup>              electronvolt#Mass
TG                 TG                                    teragauss
Tg                 Tg                                    teragram
THz                THz                                   terahertz
TJ*mol-1           TJ&sdot;mol<sup>&minus;1</sup>        joule per mole
TJ/mol             TJ/mol                                joule per mole
TK                 TK                                    terakelvin
torr               Torr                                  Torr
Torr               Torr                                  Torr
TT                 TT                                    teratesla
TV                 TV                                    volt
u                  u                                     atomic mass unit
uBP                <sup>14</sup>C yr BP                  Radiocarbon dating#Calibration
USgal              US gal                                US gallon
V                  V                                     volt
V*m-1              V&sdot;m<sup>&minus;1</sup>           electric field
V/m                V/m                                   electric field
Vm-1               V&sdot;m<sup>&minus;1</sup>           electric field
VV                 VV                                    volt
W                  W                                     Watt
Wb                 Wb                                    Weber (unit)
y                  y                                     year
YA                 YA                                    ampere
yA                 yA                                    ampere
year               year                                  year
years              years                                 year
yG                 yG                                    yoctogauss
yg                 yg                                    yoctogram
YG                 YG                                    yottagauss
Yg                 Yg                                    yottagram
yHz                yHz                                   hertz
YHz                YHz                                   yottahertz
yK                 yK                                    yoctokelvin
YK                 YK                                    yottakelvin
ym                 ym                                    yoctometre
yr                 yr                                    year#Symbols y and yr
yT                 yT                                    yoctotesla
YT                 YT                                    Yottatesla
YV                 YV                                    volt
yV                 yV                                    volt
ZA                 ZA                                    ampere
zA                 zA                                    ampere
zG                 zG                                    zeptogauss
zg                 zg                                    zeptogram
ZG                 ZG                                    zettagauss
Zg                 Zg                                    zettagram
zHz                zHz                                   hertz
ZHz                ZHz                                   hertz
zK                 zK                                    zeptokelvin
ZK                 ZK                                    zettakelvin
zm                 zm                                    zeptometre
zT                 zT                                    zeptotesla
ZT                 ZT                                    zettatesla
zV                 zV                                    volt
ZV                 ZV                                    volt

billiard           billiard                              Orders of magnitude (numbers)#1015
billion            billion                               1,000,000,000
billionth          billionth                             1,000,000,000
billionths         billionths                            1,000,000,000
decilliard         decilliard                            Orders of magnitude (numbers)#1063
decillion          decillion                             Orders of magnitude (numbers)#1033
decillionth        decillionth                           Orders of magnitude (numbers)#1033
decillionths       decillionths                          Orders of magnitude (numbers)#1033
milliard           milliard                              1,000,000,000
nonilliard         nonilliard                            Orders of magnitude (numbers)#1057
nonillion          nonillion                             Orders of magnitude (numbers)#1030
nonillionth        nonillionth                           Orders of magnitude (numbers)#1030
nonillionths       nonillionths                          Orders of magnitude (numbers)#1030
octilliard         octilliard                            Orders of magnitude (numbers)#1051
octillion          octillion                             Orders of magnitude (numbers)#1027
octillionth        octillionth                           Orders of magnitude (numbers)#1027
octillionths       octillionths                          Orders of magnitude (numbers)#1027
quadrilliard       quadrilliard                          Orders of magnitude (numbers)#1027
quadrillion        quadrillion                           Orders of magnitude (numbers)#1015
quadrillionth      quadrillionth                         Orders of magnitude (numbers)#1015
quadrillionths     quadrillionths                        Orders of magnitude (numbers)#1015
quintilliard       quintilliard                          Orders of magnitude (numbers)#1033
quintillion        quintillion                           Orders of magnitude (numbers)#1018
quintillionth      quintillionth                         Orders of magnitude (numbers)#1018
quintillionths     quintillionths                        Orders of magnitude (numbers)#1018
septilliard        septilliard                           Orders of magnitude (numbers)#1045
septillion         septillion                            Orders of magnitude (numbers)#1024
septillionth       septillionth                          Orders of magnitude (numbers)#1024
septillionths      septillionths                         Orders of magnitude (numbers)#1024
sextilliard        sextilliard                           Orders of magnitude (numbers)#1039
sextillion         sextillion                            Orders of magnitude (numbers)#1021
sextillionth       sextillionth                          Orders of magnitude (numbers)#1021
sextillionths      sextillionths                         Orders of magnitude (numbers)#1021
trilliard          trilliard                             Orders of magnitude (numbers)#1021
trillion           trillion                              Orders of magnitude (numbers)#1012
trillionth         trillionth                            Orders of magnitude (numbers)#1012
trillionths        trillionths                           Orders of magnitude (numbers)#1012

]=]

-- If val has "|long scale=on" the following definitions are used
-- (if not found here, the normal definitions are used).
-- Unit code       Symbol                                Link                                   Flags
local builtin_units_long_scale = [=[

billion            billion                               Orders of magnitude (numbers)#1012
billionth          billionth                             Orders of magnitude (numbers)#1012
billionths         billionths                            Orders of magnitude (numbers)#1012
decillion          decillion                             Orders of magnitude (numbers)#1060
decillionth        decillionth                           Orders of magnitude (numbers)#1060
decillionths       decillionths                          Orders of magnitude (numbers)#1060
nonillion          nonillion                             Orders of magnitude (numbers)#1054
nonillionth        nonillionth                           Orders of magnitude (numbers)#1054
nonillionths       nonillionths                          Orders of magnitude (numbers)#1054
octillion          octillion                             Orders of magnitude (numbers)#1048
octillionth        octillionth                           Orders of magnitude (numbers)#1048
octillionths       octillionths                          Orders of magnitude (numbers)#1048
quadrillion        quadrillion                           Orders of magnitude (numbers)#1024
quadrillionth      quadrillionth                         Orders of magnitude (numbers)#1024
quadrillionths     quadrillionths                        Orders of magnitude (numbers)#1024
quintillion        quintillion                           Orders of magnitude (numbers)#1030
quintillionth      quintillionth                         Orders of magnitude (numbers)#1030
quintillionths     quintillionths                        Orders of magnitude (numbers)#1030
septillion         septillion                            Orders of magnitude (numbers)#1042
septillionth       septillionth                          Orders of magnitude (numbers)#1042
septillionths      septillionths                         Orders of magnitude (numbers)#1042
sextillion         sextillion                            Orders of magnitude (numbers)#1036
sextillionth       sextillionth                          Orders of magnitude (numbers)#1036
sextillionths      sextillionths                         Orders of magnitude (numbers)#1036
trillion           trillion                              Orders of magnitude (numbers)#1018
trillionth         trillionth                            Orders of magnitude (numbers)#1018
trillionths        trillionths                           Orders of magnitude (numbers)#1018

]=]

local function get_builtin_unit(unitcode, definitions)
	-- Return table of information for the specified built-in unit, or nil if not known.
	-- Each defined unit code must be followed by two spaces (not tab characters).
	local _, pos = definitions:find('\n' .. unitcode .. '  ', 1, true)
	if pos then
		local endline = definitions:find('\n', pos, true)
		if endline then
			local result = {}
			local n = 0
			local text = definitions:sub(pos, endline - 1)
			for item in (text .. '  '):gmatch('(%S[^\n]-)%s%s') do
				if item == 'NOSPACE' then
					result.nospace = true
				else
					n = n + 1
					if n == 1 then
						result.symbol = item
					elseif n == 2 then
						result.link = item
					else
						break
					end
				end
			end
			if n == 2 then
				return result
			end
			-- Ignore invalid definition.
		end
	end
end

local function get_unit(ucode, value, want_link, want_longscale)
	local result = want_longscale and
		get_builtin_unit(ucode, builtin_units_long_scale) or
		get_builtin_unit(ucode, builtin_units)
	local lookup = require('Module:Convert/sandbox')._unit
	local convert_unit = lookup(ucode, { value = value, link = want_link })
	if result then
		-- Have: result.symbol + result.link + result.nospace
		if want_link then
			result.text = '[[' .. result.link .. '|' .. result.symbol .. ']]'
		else
			result.text = result.symbol
		end
		result.sortkey = convert_unit.sortkey
	else
		result = {
			text = convert_unit.text,
			sortkey = convert_unit.sortkey,
		}
	end
	return result
end

local function bracketed(ucode, text)
	if ucode:find('[*./]') then
		return '(' .. text .. ')'
	end
	return text
end

local function makeunit(ucode, options)
	-- Return wikitext, sortkey for the requested unit and options.
	-- LATER The sortkey does not account for any per unit.
	options = options or {}
	local unit = get_unit(ucode, options.value, options.link, options.longscale)
	local text = unit.text
	local percode = options.per
	if percode then
		local perunit = get_unit(percode, 0, options.per_link, options.longscale)
		text = bracketed(ucode, text) .. '/' .. bracketed(percode, perunit.text)
	end
	if not unit.nospace then
		text = '&nbsp;' .. text
	end
	return text, unit.sortkey
end

return makeunit