User:Bobblewik/monobook.js/unitformatter.js: Difference between revisions

Content deleted Content added
Bobblewik (talk | contribs)
mNo edit summary
m Maintenance: Replacing addOnloadHook with native jQuery (mw:ResourceLoader/Migration_guide_(users)#addOnloadHook - phab:T130879)
 
(98 intermediate revisions by one other user not shown)
Line 9:
txt.value = txt.value.replace(/°/g, '°');
txt.value = txt.value.replace(/º/g, '°');
 
txt.value = txt.value.replace(/°\s?([CF])/g, '°$1');
// Celsius spelling errors
txt.value = txt.value.replace(/°\s?(celsius)/gi, '°C');
txt.value = txt.value.replace(/(\d)\s?(°[CF])/g, '$1 $2');
//Fix spelling errors
txt.value = txt.value.replace(/celsius/gi, 'Celsius');
txt.value = txt.value.replace(/celcius/gi, 'Celsius');
Line 18 ⟶ 16:
txt.value = txt.value.replace(/centigrade/gi, 'Celsius');
 
//Celsius
// Convert &sup into superscript ² symbol
//txt.value = txt.value.replace(/\[\[(celsius)\]\]/gi, '$1');
//txt.value = txt.value.replace(/\[\[celsius\|([^\]]{1,30})\]\]/gi, '$1');
 
//Fahrenheit
//txt.value = txt.value.replace(/\[\[(Fahrenheit)\]\]/gi, '$1');
// txt.value = txt.value.replace(/\[\[Fahrenheit\|([^\]]{1,30})\]\]/gi, '$1');
 
//Celsius or Fahrenheit
txt.value = txt.value.replace(/°\s([CF])/g, '°$1');
txt.value = txt.value.replace(/°\s?(celsius)/gi, '°C');
txt.value = txt.value.replace(/(\d)\s?(°[CF])/g, '$1 $2');
txt.value = txt.value.replace(/deg[^\(]([CF])/gi, '°$1');
txt.value = txt.value.replace(/deg\s([CF])/gi, '°$1');
txt.value = txt.value.replace(/deg\.?\s?([CF])/gi, '°$1');
txt.value = txt.value.replace(/degrees?\s([CF])(\W)/gi, '°$1$2');
txt.value = txt.value.replace(/(\d)\s?° ([CF])/g, '$1 °$2');
 
 
// Convert &sup to superscript
txt.value = txt.value.replace(/²/g, '²');
txt.value = txt.value.replace(/³/g, '³');
 
// Convert themicro wordsymbol ohm(s) or the html entity into theto actual Ωmicro symbol (Omega, not the actual ohm symbol Ω) and make sure it is's spaced
txt.value = txt.value.replace(/(\d)\s?(Yμ|Zμ|Eµ)(g|Ps|Tm|GA|MK|kmol|Kcd|hrad|dasr|dHz|cN|mJ|µW|μPa|µlm|nlx|pC|fV|aΩ|zF|y)?\s?(ΩWb|ohmT|OhmH|S|Bq|Gy|Sv|kat|M)s?([\s,.;:\)\(\\/)]W)/g, '$1 µ$3$4');
 
//metre
// Convert various micro symbols into the actual micro symbol, make sure it's spaced
//txt.value = txt.value.replace(/(\d)[\s[(metres?(μ|μ|µ)(g|s|m|A|K|mol|cd|rad|sr|Hz|N|J|W|Pa|lm|lx|C|V|Ω|F|Wb|T|H|S|Bq|Gy|Sv|kat|M)([\s,.;:]\)\(\\/)])/ggi, '$1 µ$3$4');
//txt.value = txt.value.replace(/\[\[(meters?)\]\]/gi, '$1');
//txt.value = txt.value.replace(/\[\[metres?\|([^\]]{1,30})\]\]/gi, '$1');
//space before 'm' only when lower case
txt.value = txt.value.replace(/(\d)\s?m(\W)/g, '$1 m$2');
txt.value = txt.value.replace(/(\d)\-m(\W)/g, '$1 m$2');
txt.value = txt.value.replace(/(\d)\s?sq\.?\s?m(\W)/gi, '$1 m²$2');
txt.value = txt.value.replace(/(\d)\-?sq\-?m(\W)/gi, '$1 m²$2');
txt.value = txt.value.replace(/m²\.\)/gi, 'm²)');
 
// millimetre
// Convert capital K to lowercase k in units
//txt.value = txt.value.replace(/(\d)[\s?K[(g|s|m|A|K|mol|cd|rad|sr|Hz|N|J|W|Pa|lm|lx|C|V|Ω|F|Wb|T|H|S|Bq|Gy|Sv|kat|Mmm)([\s,.;:]\)\(\\/)])/ggi, '$1 k$2$3');
//txt.value = txt.value.replace(/(\d)[\-K[(g|s|m|A|K|mol|cd|rad|sr|Hz|N|J|W|Pa|lm|lx|C|V|Ω|F|Wb|T|H|S|Bq|Gy|Sv|kat|Mmillimetres?)([\s,.;:]\)\(\\/)])/ggi, '$1 k$2$3');
//txt.value = txt.value.replace(/(\d) K(g|s|m|A|K|mol|cd|rad|sr|Hz|N|J|W|Pa|lm|lx|C|V|Ω|F|Wb|T|H|S|Bq|Gy|Sv|kat|M)([\s,.;:\[(millimeters?)\(]\\/)])/ggi, '$1 k$2$3');
//txt.value = txt.value.replace(/\[\[millimetres?\|([^\]]{1,30})\]\]/gi, '$1');
//txt.value = txt.value.replace(/\[\[millimeters?\|([^\]]{1,30})\]\]/gi, '$1');
txt.value = txt.value.replace(/(\d)\s?mm(\W)/g, '$1 mm$2');
txt.value = txt.value.replace(/(\d)\-mm(\W)/g, '$1 mm$2');
 
// Hertzcentimetre
//txt.value = txt.value.replace(/(\d)[\s?[(Y|Z|E|P|T|G|M|k|K|h|da|d|c|m|µ|μ|µ|n|p|f|a|z|ycm)?hz\]\]/gi, '$1 $2Hz');
//txt.value = txt.value.replace(/(\d)[\-[(Y|Z|E|P|T|G|M|k|K|h|da|d|c|m|µ|μ|µ|n|p|f|a|z|y)centimetres?hz)\]\]/gi, '$1 $2Hz');
//txt.value = txt.value.replace(/\[\[(centimeters?)\]\]/gi, '$1');
 
//txt.value = txt.value.replace(/\[\[centimetres?\|([^\]]{1,30})\]\]/gi, '$1');
// Fix kilometres
//txt.value = txt.value.replace(/(\d)[\s?kms[centimeters?\|([^\s]]{1,.;:\30})\(]\\/)])/gi, '$1 km$2');
txt.value = txt.value.replace(/(\d)\-kms?([\s,.;:\)\(\\/)])/gi, '$1 km$2');
txt.value = txt.value.replace(/(\d) kms?([\s,.;:\)\(\\/)])/gi, '$1 km$2');
 
// kilometre
//txt.value = txt.value.replace(/\[\[(km)\]\]/gi, '$1');
//txt.value = txt.value.replace(/\[\[(kilometres?)\]\]/gi, '$1');
//txt.value = txt.value.replace(/\[\[(kilometers?)\]\]/gi, '$1');
//txt.value = txt.value.replace(/\[\[kilometres?\|([^\]]{1,30})\]\]/gi, '$1');
//txt.value = txt.value.replace(/\[\[kilometers?\|([^\]]{1,30})\]\]/gi, '$1');
txt.value = txt.value.replace(/(\d)\s?kms?(\W)/gi, '$1 km$2');
txt.value = txt.value.replace(/(\d)\-kms?(\W)/gi, '$1 km$2');
txt.value = txt.value.replace(/(\d) kms?(\W)/gi, '$1 km$2');
//square kilometre
//txt.value = txt.value.replace(/\[\[square kilometres?\|([^\]]{1,30})\]\]/gi, '$1');
txt.value = txt.value.replace(/(\d)\s?sq\.?\s?kms?/gi, '$1 km²');
txt.value = txt.value.replace(/sq\.?\s?kms?/gi, 'km²');
txt.value = txt.value.replace(/(\d)\s?sq\.?\s?m([^i])/gi, '$1 m²$2');
txt.value = txt.value.replace(/m²\.\)/gi, 'm²)');
 
// Standardise kilometreskilometre per hour
txt.value = txt.value.replace(/km\/hr(\W)/gi, 'km/h$1');
txt.value = txt.value.replace(/kph(\W)/gi, 'km/h$1');
txt.value = txt.value.replace(/kmph(\W)/gi, 'km/h$1');
txt.value = txt.value.replace(/(\d)\s?kmh/gi, '$1 km/h');
txt.value = txt.value.replace(/km\/h/gi, 'km/h');
Line 57 ⟶ 93:
txt.value = txt.value.replace(/(\d) km\/h/gi, '$1 km/h');
 
// Standardisecubic 'per second'centimetre
txt.value = txt.value.replace(/(\d)\s?cm(\W)/gi, '$1 cm$2');
txt.value = txt.value.replace(/(\d)\s?cc(\W)/gi, '$1 cc$2');
txt.value = txt.value.replace(/(\d)\-cc(\W)/gi, '$1 cc$2');
 
// millilitre
txt.value = txt.value.replace(/(\d)\s?ml(\W)/g, '$1 ml$2');
txt.value = txt.value.replace(/(\d)\-ml(\W)/g, '$1 ml$2');
 
// second
txt.value = txt.value.replace(/\[\[(s)\]\]/gi, '$1');
txt.value = txt.value.replace(/\[\[(seconds?)\]\]/gi, '$1');
txt.value = txt.value.replace(/\[\[seconds?\|([^\]]{1,30})\]\]/gi, '$1');
txt.value = txt.value.replace(/\/sec(\W)/gi, '/s$1');
txt.value = txt.value.replace(/\/sec\)/gi, '/s)$1');
 
txt.value = txt.value.replace(/(\d)\s?ft\/second/gi, '$1 ft/s');
txt.value = txt.value.replace(/(\d)\s?ft\/sec/gi, '$1 ft/s');
txt.value = txt.value.replace(/(\d)\s?m\/second/gi, '$1 m/s');
txt.value = txt.value.replace(/(\d)\s?mkm\/sec(\W)/gi, '$1 mkm/s$2');
txt.value = txt.value.replace(/frames\/s(\dW)\s?km\/sec/gi, 'frame/s$1 km/s');
 
//minute
// Space before horsepower symbol
txt.value = txt.value.replace(/(\d)[\s?hp[(min)\W)]\]/gi, '$1 hp$2');
txt.value = txt.value.replace(/(\d)[\s[(minutes?bhp)\]\]/gi, '$1 bhp');
txt.value = txt.value.replace(/\[\[minutes?\|([^\d]]{1,30})\s?shp]\]/gi, '$1 shp');
 
// Space before unitshour
txt.value = txt.value.replace(/(\d)[\s?cc[([\s,.;:\h)\(]\\/)])/gi, '$1 cc$2');
txt.value = txt.value.replace(/(\d)[\-cc[([\s,.;:\hours?)\(]\\/)])/gi, '$1 cc$2');
txt.value = txt.value.replace(/(\d)[\s[hours?ft\|([^\s]]{1,.;:\30})\(]\\/)])/gi, '$1 ft$2');
txt.value = txt.value.replace(/(\d)\-ft/hr([\s,.;:\)\(\\/)]W)/gi, '/h$1 ft$2');
txt.value = txt.value.replace(/(\d)\s?rpm/gi, '$1 rpm');
txt.value = txt.value.replace(/(\d)\-rpm/gi, '$1 rpm');
txt.value = txt.value.replace(/(\d)\s?kg([\s,.;:\)\(\\/)])/gi, '$1 kg$2');
txt.value = txt.value.replace(/(\d)\-kg([\s,.;:\)\(\\/)])/gi, '$1 kg$2');
//space before 'm' only when lower case
txt.value = txt.value.replace(/(\d)\s?m([\s,.;:\)\(\\/)])/g, '$1 m$2');
txt.value = txt.value.replace(/(\d)\-m([\s,.;:\)\(\\/)])/g, '$1 m$2');
//txt.value = txt.value.replace(/(\d)\s?ml([\s,.;:\)\(\\/)])/g, '$1 ml$2');
//txt.value = txt.value.replace(/(\d)\-ml([\s,.;:\)\(\\/)])/g, '$1 ml$2');
txt.value = txt.value.replace(/(\d)\s?m(\W)/g, '$1 m$2');
txt.value = txt.value.replace(/(\d)\s?m(\w?\W)/g, '$1 m$2');
 
// miles per hourday
txt.value = txt.value.replace(/m\.p[\.h[(d)\.]\]/gi, 'mph$1');
txt.value = txt.value.replace(/mph(\[\s,.;:\[(days?)\(]\\/)])/gi, 'mph$1');
txt.value = txt.value.replace(/\[\[days?\|([^\d]]{1,30})\s?mph]\]/gi, '$1 mph');
txt.value = txt.value.replace(/(\d)\-mph/gi, '$1 mph');
 
// kilogram
// Standardise symbol for pounds
txt.value = txt.value.replace(/(kilogram)me/gi, '$1');
txt.value = txt.value.replace(/(\W)(gram)mes?(\W)/gi, '$1$2$3');
//txt.value = txt.value.replace(/\[\[(grams?)\]\]/gi, '$1');
//txt.value = txt.value.replace(/\[\[grams?\|([^\]]{1,30})\]\]/gi, '$1');
//txt.value = txt.value.replace(/\[\[(kg)\]\]/gi, '$1');
//txt.value = txt.value.replace(/\[\[(kilograms?)\]\]/gi, '$1');
//txt.value = txt.value.replace(/\[\[kilograms?\|([^\]]{1,30})\]\]/gi, '$1');
txt.value = txt.value.replace(/(\d)\s?kg(\W)/gi, '$1 kg$2');
txt.value = txt.value.replace(/(\d)\-kg(\W)/gi, '$1 kg$2');
 
// newton metre
//txt.value = txt.value.replace(/(\W)N[-.·•\/]m(\W)/gi, '$1N·m$2');
 
// kilowatt
txt.value = txt.value.replace(/(\d)\s?kW(\W)/gi, '$1 kW$2');
txt.value = txt.value.replace(/(\d)\-kW(\W)/gi, '$1 kW$2');
 
// Hertz
txt.value = txt.value.replace(/(\d)\s?(G|M|k)?hz/gi, '$1 $2Hz');
txt.value = txt.value.replace(/(\d)\-(G|M|k)?hz/gi, '$1 $2Hz');
txt.value = txt.value.replace(/khz/gi, 'kHz');
 
 
// ohm
txt.value = txt.value.replace(/(\d)\s?(Y|Z|E|P|T|G|M|k|K|h|da|d|c|m|µ|μ|µ|n|p|f|a|z|y)?\s?(Ω|ohm|Ohm)s?(\W)/g, '$1 $2Ω$4');
 
// pound weight
//txt.value = txt.value.replace(/(\d)\s?(\[\[lbs?\]\])/gi, '$1 lb');
//txt.value = txt.value.replace(/\[\[\pound\s\(mass\)\|([^\]]{1,30})\]\]/gi, '$1');
txt.value = txt.value.replace(/(\d)\s?lbs?/gi, '$1 lb');
txt.value = txt.value.replace(/(\d\+?)\s?lbs?/gi, '$1 lb');
txt.value = txt.value.replace(/(\d) lbs?/gi, '$1 lb');
txt.value = txt.value.replace(/(\d)\s?(slb.\[\[lbs\]\])/gi, '$1 \[\[Pound (masslb)|lb\]\]');
 
//inch
// Standardise symbol for yards
//txt.value = txt.value.replace(/\[\[(inch)\]\]/gi, '$1');
//txt.value = txt.value.replace(/\[\[(inches)\]\]/gi, '$1');
//txt.value = txt.value.replace(/\[\[inch\|([^\]]{1,30})\]\]/gi, '$1');
//txt.value = txt.value.replace(/\[\[inches\|([^\]]{1,30})\]\]/gi, '$1');
 
//foot
//txt.value = txt.value.replace(/\[\[foot\s\(unit\sof\slength\)\|([^\]]{1,30})\]\]/gi, '$1');
txt.value = txt.value.replace(/(\d)\s?ft(\W)/gi, '$1 ft$2');
txt.value = txt.value.replace(/(\d)\-ft(\W)/gi, '$1 ft$2');
txt.value = txt.value.replace(/(\W)ft\.\)/gi, '$1ft)');
 
// square foot
txt.value = txt.value.replace(/sq\.?\s?ft/gi, 'sq ft');
 
// foot and inch
//txt.value = txt.value.replace(/([^;°h]{1,4})(\d{1,4})\s?['’]\s?(\d{1,3})\s?["”]([^NESW])/g, '$1$2 ft $3 in$4');
txt.value = txt.value.replace(/(ength[.]{1,3})(\d{1,4})\s?['’]\s?(\d{1,3})\s?["”]/gi, '$1$2 ft $3 in');
txt.value = txt.value.replace(/(idth[.]{1,3})(\d{1,4})\s?['’]\s?(\d{1,3})\s?["”]/gi, '$1$2 ft $3 in');
txt.value = txt.value.replace(/([\(\|:]\s?\d{1,4})\s?['’]\s?(\d{1,3})\s?["”]([^NESW])/g, '$1 ft $2 in$3');
txt.value = txt.value.replace(/(\d)\s?ft\s?(\d{1,3})\s?in/gi, '$1 ft $2 in');
 
// yard
txt.value = txt.value.replace(/(\d)\s?yds(\W)/gi, '$1 yd$2');
txt.value = txt.value.replace(/(\d) yds(\W)/gi, '$1  yd$2');
txt.value = txt.value.replace(/sq\.?\s?yds?/gi, 'sq yd');
txt.value = txt.value.replace(/yd\.\)/gi, 'yd)');
 
// Standardisemile symboland formile newtonper metreshour
//txt.value = txt.value.replace(/\[\[miles?\|(N•m[^\]]{1,30})\]\]/gi, 'N·m$1');
//txt.value = txt.value.replace(/\[\[(miles?)\]\]/gi, '$1');
txt.value = txt.value.replace(/m\.p\.h\.(\W)/g, 'mph$1');
txt.value = txt.value.replace(/(\W)mph(\W)/gi, '$1mph$2');
txt.value = txt.value.replace(/(\d)\s?mph/gi, '$1 mph');
txt.value = txt.value.replace(/(\d)\-mph/gi, '$1 mph');
 
// Standardisesquare symbol for kilowattsmile
txt.value = txt.value.replace(/(sq\d).?\s?kW([\s,.;:\)\(\\/)])mi/ggi, '$1sq kW$2mi');
txt.value = txt.value.replace(/(\d)\-kW([\s,.;:\)\(\\/)])/g, '$1 kW$2');
 
// Standardise symbol for foot poundspound
txt.value = txt.value.replace(/ft[ -.·•ftlbs?(\/]lb[fs]W)/gi, 'ft·lbf$1');
txt.value = txt.value.replace(/ft[ -.·•\/]lb(\W)/gi, 'ft·lbf$1');
txt.value = txt.value.replace(/ft[ -.·•\/]lbs/gi, 'ft·lbf');
txt.value = txt.value.replace(/ft[ -.·•\/]lbf/gi, 'ft·lbf');
txt.value = txt.value.replace(/ft[ -.·•\/]lbff/gi, 'ft·lbf');
txt.value = txt.value.replace(/ft[ -.·•\/]lb\s/gi, 'ft·lbf ');
//the next two suspended until solution is found for wing loading (i.e. pounds per square foot)
//txt.value = txt.value.replace(/lb[fs][ -.•\/]ft/gi, 'ft·lbf');
//txt.value = txt.value.replace(/lb[ -.•\/]ft/gi, 'ft·lbf');
 
// Symbols for feet and inches
txt.value = txt.value.replace(/([^;°h][^;°h]\s)(\d{1,4})\s?['’]\s?(\d{1,2})\s?["”][^NESW]/g, '$1$2 ft $3 in');
txt.value = txt.value.replace(/([\(\|]\d{1,4})\s?['’]\s?(\d{1,2})\s?["”][^NESW]/g, '$1 ft $2 in');
txt.value = txt.value.replace(/(\d)\s?ft\s(\d{1,2})\s?in/gi, '$1 ft $2 in');
 
//Symbols for square feet
txt.value = txt.value.replace(/sq\.?\s?ft?/gi, 'sq ft');
txt.value = txt.value.replace(/ft\.\)/gi, 'ft)');
 
// Give digital value a percent symbol '%' instead of word
Line 131 ⟶ 218:
txt.value = txt.value.replace(/(\d)\-per ?cent([^aei])/gi, '$1%$2');
 
// Add a space before dBknot
txt.value = txt.value.replace(/(\d)\s?kts(dB)\bW)/ggi, '$1 knots$2');
txt.value = txt.value.replace(/(\d)\s?knt(\W)/gi, '$1 knots$2');
 
// horsepower
// Add a space before several units that were missed before
//txt.value = txt.value.replace(/(\d)\s?hp(G|M|k|K|h|da|d|c|m|µ|n)?(g|m|Hz|N|\W|Pa|V|Ω|F|)([\s,.;:\)\(\\/)])/ggi, '$1 hp$2$3$4');
txt.value = txt.value.replace(/(\d)\s?bhp/gi, '$1 bhp');
txt.value = txt.value.replace(/(\d)\s?shp/gi, '$1 shp');
 
// rpm
txt.value = txt.value.replace(/(\d)\s?rpm/gi, '$1 rpm');
txt.value = txt.value.replace(/(\d)\-rpm/gi, '$1 rpm');
 
// decibel
txt.value = txt.value.replace(/(\d)\s?(dB)\b/g, '$1 $2');
 
// bps or b/s or bits/s -->per bit/ssecond
txt.value = txt.value.replace(/([KkMmGg])(bps|bits?\/s|b\/s)/g, '$1bit/s');
txt.value = txt.value.replace(/(\d)\s?(bps)/gi, '$1 bit/s');
txt.value = txt.value.replace(/(\d) bps/gi, '$1 bit/s');
txt.value = txt.value.replace(/bits?\/sec(\W)/gi, 'bit/s$1');
 
// Bps or byte/s or bytes/s -->per B/ssecond
txt.value = txt.value.replace(/([KkMmGg])(Bps|bytes?\/s)/g, ' $1B/s');
txt.value = txt.value.replace(/bytes?\/s(\W)/gi, 'B/s$1');
 
// Make capitalization correctof prefix with bits and bytes
txt.value = txt.value.replace(/K(bit|B)\/s/g, 'k$1/s');
txt.value = txt.value.replace(/m(bit|B)\/s/g, 'M$1/s');
txt.value = txt.value.replace(/g(bit|B)\/s/g, 'G$1/s');
 
// Ensure space iswith bits and usedbytes
txt.value = txt.value.replace(/(\d)\s?(k|M|G)(bit|B)/g, '$1 $2$3');
 
// Common error with bits and bytes
txt.value = txt.value.replace(/mibi(bit|byte)/g, 'mebi$1');
 
//Remove 'Easter egg' diversions (linking unit name to orders of magnitude articles)
txt.value = txt.value.replace(/\[\[1\s?_?E\s?\-?\d{1,2}\s?..?\|([^\]]{1,50})\]\]/gi, '$1');
txt.value = txt.value.replace(/\[\[Orders\sof\smagnitude\s\([^\)]{1,30}\)\|([^\]]{1,50})\]\]/gi, '$1');
 
// Add a tag to the summary box
var txt = document.editform.wpSummary;
Line 169 ⟶ 274:
}
 
addOnloadHook$(function () {
if(document.forms.editform) {
addLink('p-cactions', 'javascript:formatunits()', 'units', 'ca-unitfixer', 'Fixes some unit formatting', '', '');