User:Karl Dickman/units.js
From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Firefox/Mozilla/Safari: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Internet Explorer: press Ctrl-F5, Opera/Konqueror: press F5.
//Script to add a link button function addClassicLinkToTopbar() { var td = document.getElementById( 'topbar' ).getElementsByTagName( 'td' )[ 1 ]; var loc = td.getElementsByTagName( 'p' )[ 0 ]; var link = document.createElement( 'a' ) link.appendChild( document.createTextNode( 'Units' ) ); link.href = 'javascript:formatunits()' link.id = 'topbar_unitformatter' link.title = 'Standardises formatting of units in an article' td.insertBefore( link , loc.nextSibling ); td.insertBefore( document.createTextNode( ' | ' ), loc.nextSibling ); } if ( window.addEventListener ) window.addEventListener( 'load', addClassicLinkToTopbar, false ); else if ( window.attachEvent ) window.attachEvent( 'onload', addClassicLinkToTopbar ); function formatunits() { var txt = document.editform.wpTextbox1; // Convert degree symbols into ° symbol, ensure preceding space txt.value = txt.value.replace(/°/g, '°'); txt.value = txt.value.replace(/º/g, '°'); 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\.?\s?([CF])/g, '°$1'); // Celsius spelling errors txt.value = txt.value.replace(/celsius/gi, 'Celsius'); txt.value = txt.value.replace(/celcius/gi, 'Celsius'); //Fix common naming error (be careful with this one) txt.value = txt.value.replace(/centigrade/gi, 'Celsius'); // Convert &sup to superscript txt.value = txt.value.replace(/²/g, '²'); txt.value = txt.value.replace(/³/g, '³'); // Convert micro symbol to actual micro symbol, make sure it's spaced txt.value = txt.value.replace(/(\d)\s?(μ|μ|µ)(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)(\W)/g, '$1 µ$3$4'); //metre: 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²)'); txt.value = txt.value.replace(/m2/gi, 'm²'); // millimetre 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'); // kilometre 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(/(\d)\s?sq\.?\s?kms?/gi, '$1 km²'); txt.value = txt.value.replace(/sq\.?\s?kms?/gi, 'km²'); // kilometre 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'); txt.value = txt.value.replace(/(\d)\s?km\/h/gi, '$1 km/h'); txt.value = txt.value.replace(/(\d)\-km\/h/gi, '$1 km/h'); txt.value = txt.value.replace(/(\d) km\/h/gi, '$1 km/h'); // cubic 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'); // per second txt.value = txt.value.replace(/(\d)\s?ft\/second/gi, '$1 ft/s'); txt.value = txt.value.replace(/(\d)\s?ft\/sec(\W)/gi, '$1 ft/s$2'); txt.value = txt.value.replace(/(\d)\s?m\/second/gi, '$1 m/s'); txt.value = txt.value.replace(/(\d)\s?m\/sec(\W)/gi, '$1 m/s$2'); txt.value = txt.value.replace(/(\d)\s?km\/sec(\W)/gi, '$1 km/s$2'); // kilogram 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?(Y|Z|E|P|T|G|M|k|K|h|da|d|c|m|µ|μ|µ|n|p|f|a|z|y)?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)?hz/gi, '$1 $2Hz'); // 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'); // mile per hour 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'); // pound weight 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?(\[\[lbs\]\])/gi, '$1 \[\[Pound (mass)|lb\]\]'); // foot and inch txt.value = txt.value.replace(/([^;°h][^;°h]\s)(\d{1,4})\s?['’]\s?(\d{1,2})\s?["”]([^NESW])/g, '$1$2 ft $3 in$4'); txt.value = txt.value.replace(/([\(\|]\d{1,4})\s?['’]\s?(\d{1,2})\s?["”]([^NESW])/g, '$1 ft $2 in$3'); txt.value = txt.value.replace(/(\d)\s?ft\s(\d{1,2})\s?in/gi, '$1 ft $2 in'); 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, 'ft²'); // 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)'); // square mile txt.value = txt.value.replace(/sq\.?\s?mi/gi, 'sq mi'); // foot pound 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'); //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'); // Give digital value a percent symbol '%' instead of word txt.value = txt.value.replace(/(\d)\s?per ?cent([^aei])/gi, '$1%$2'); txt.value = txt.value.replace(/(\d)\-per ?cent([^aei])/gi, '$1%$2'); // knot txt.value = txt.value.replace(/(\d)\s?kts(\W)/gi, '$1 knots$2'); txt.value = txt.value.replace(/(\d)\s?knt(\W)/gi, '$1 knots$2'); // horsepower txt.value = txt.value.replace(/(\d)\s?hp(\W)/gi, '$1 hp$2'); 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'); // bits per second 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(/bits?\/sec(\W)/gi, 'bit/s$1'); // bytes per second txt.value = txt.value.replace(/([KkMmGg])(Bps|bytes?\/s)/g, ' $1B/s'); txt.value = txt.value.replace(/bytes?\/sec(\W)/gi, 'B/s$1'); txt.value = txt.value.replace(/B\/sec(\W)/g, 'B/s$1'); // capitalization of 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'); // space with bits and bytes 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'); // Add a tag to the summary box var txt = document.editform.wpSummary; var summary = "units"; if (txt.value.indexOf(summary) == -1) { if (txt.value.match(/[^\*\/\s][^\/\s]?\s*$/)) { txt.value += " | "; } txt.value += summary; } // Press the diff button to check it document.editform.wpDiff.click() }