Module:Val/units

-- TODO: Need to see what units are missing and add them

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

return function (u, tbl) if not tbl then tbl = {} end local per = tbl.per local link = tbl.link local perl = tbl.per_link

local function get_unit(_u) -- load convert module data local convert_data = mw.loadData('Module:Convert/data').all_units local c_unit = convert_data[_u] if c_unit then if c_unit.target then c_unit = convert_data[c_unit.target] end return {abbr=c_unit.symbol or _u, link=c_unit.link or c_unit.name1 or _u, symlink=c_unit.symlink} else return nil end end

local unit = units_tbl[u] or get_unit(u) or {abbr=u, link=u} local nospace = unit.nospace

if link and unit.symlink then unit = unit.symlink elseif link then unit = ..unit.abbr.. else unit = unit.abbr end if per then -- Look for "*" "." or "/" -- If found, add parentheses if u:find('[%*%.%/]') then unit = '('..unit..')' end local per_u = units_tbl[per] or get_unit(per) or {abbr=per, link=per} if perl and per_u.symlink then per_u = per_u.symlink elseif perl then per_u = ..per_u.abbr.. else per_u = per_u.abbr end -- Look for "*" "." or "/" -- If found, add parentheses if per:find('[%*%.%/]') then per_u = '('..per_u..')' end unit = unit..'/'..per_u end

if not nospace then unit = ' '..unit end

return unit end