User:Bobblewik/monobook.js/units nbsp.js: Difference between revisions

Content deleted Content added
Bobblewik (talk | contribs)
mNo edit summary
 
Bobblewik (talk | contribs)
m updated from unitformatter.js
Line 3:
winc('User:Omegatron/monobook.js/addlink.js');
 
function units_nbspformatunits() {
var txt = document.editform.wpTextbox1;
 
Line 11:
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&nbsp1nbsp;$2');
//Fix spelling errorerrors
txt.value = txt.value.replace(/(\d)\s?cu[\s\-\.]incelsius/gi, '$1 in³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 into superscript ² symbol
txt.value = txt.value.replace(/²/g, '²');
txt.value = txt.value.replace(/³/g, '³');
 
// Convert cubic inches into in³
txt.value = txt.value.replace(/(\d)\s?cu[\s\-\.]in/gi, '$1 in³');
 
 
// Convert the word ohm(s) or the html entity into the actual Ω symbol (Omega, not the actual ohm symbol Ω) and make sure it is spaced
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?([\s,.;:\)\(\\/)])/g, '$1&nbsp1nbsp;$2Ω$4');
 
// Convert various micro symbols into the actual micro symbol, make sure it is'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)([\s,.;:\)\(\\/)])/g, '$1&nbsp1nbsp;µ$3$4');
 
// 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|M)([\s,.;:\)\(\\/)])/g, '$1&nbsp1nbsp;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,.;:\)\(\\/)])/g, '$1&nbsp1nbsp;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,.;:\)\(\\/)])/g, '$1 k$2$3');
 
// 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&nbsp1nbsp;$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&nbsp1nbsp;$2Hz');
 
// Fix kilometres
txt.value = txt.value.replace(/(\d)\s?kms?([\s,.;:\)\(\\/)])/gi, '$1&nbsp1nbsp;km$2');
txt.value = txt.value.replace(/(\d)\-kms?([\s,.;:\)\(\\/)])/gi, '$1&nbsp1nbsp;km$2');
txt.value = txt.value.replace(/(\d) kms?([\s,.;:\)\(\\/)])/gi, '$1 km$2');
 
txt.value = txt.value.replace(/(\d)\s?sq\.?\s?kms?/gi, '$1nbsp;km²');
txt.value = txt.value.replace(/sq\.?\s?kms?/gi, 'km²');
txt.value = txt.value.replace(/(\d)\s?sq\.?\s?m([^i])/gi, '$1nbsp;m²$2');
txt.value = txt.value.replace(/m²\.\)/gi, 'm²)');
 
// Standardise kilometres per hour
Line 50 ⟶ 51:
txt.value = txt.value.replace(/kph/gi, 'km/h');
txt.value = txt.value.replace(/kmph/gi, 'km/h');
txt.value = txt.value.replace(/(\d)\s?kmh/gi, '$1&nbsp1nbsp;km/h');
txt.value = txt.value.replace(/km\/h/gi, 'km/h');
txt.value = txt.value.replace(/(\d)\s?km\/h/gi, '$1&nbsp1nbsp;km/h');
txt.value = txt.value.replace(/(\d)\-km\/h/gi, '$1&nbsp1nbsp;km/h');
txt.value = txt.value.replace(/(\d) km\/h/gi, '$1 km/h');
 
// Standardise 'per second'
txt.value = txt.value.replace(/(\d)\s?ft\/second/gi, '$1nbsp;ft/s');
txt.value = txt.value.replace(/(\d)\s?ft\/sec/gi, '$1nbsp;ft/s');
txt.value = txt.value.replace(/(\d)\s?m\/second/gi, '$1nbsp;m/s');
txt.value = txt.value.replace(/(\d)\s?m\/sec/gi, '$1nbsp;m/s');
txt.value = txt.value.replace(/(\d)\s?km\/sec/gi, '$1nbsp;km/s');
 
// Space before horsepower symbol
txt.value = txt.value.replace(/(\d)\s?hp/gi, '$1&nbsp1nbsp;hp');
txt.value = txt.value.replace(/(\d)\s?bhp/gi, '$1&nbsp1nbsp;bhp');
txt.value = txt.value.replace(/(\d)\s?shp/gi, '$1&nbsp1nbsp;shp');
 
// Space before units
txt.value = txt.value.replace(/(\d)\s?cc([\s,.;:\)\(\\/)])/gi, '$1&nbsp1nbsp;cc$2');
txt.value = txt.value.replace(/(\d)\-cc([\s,.;:\)\(\\/)])/gi, '$1&nbsp1nbsp;cc$2');
txt.value = txt.value.replace(/(\d)\s?ft([\s,.;:\)\(\\/)])/gi, '$1&nbsp1nbsp;ft$2');
txt.value = txt.value.replace(/(\d)\-ft([\s,.;:\)\(\\/)])/gi, '$1&nbsp1nbsp;ft$2');
txt.value = txt.value.replace(/(\d)\s?rpm/gi, '$1&nbsp1nbsp;rpm');
txt.value = txt.value.replace(/(\d)\-rpm/gi, '$1&nbsp1nbsp;rpm');
txt.value = txt.value.replace(/(\d)\s?kg([\s,.;:\)\(\\/)])/gi, '$1&nbsp1nbsp;kg$2');
txt.value = txt.value.replace(/(\d)\-kg([\s,.;:\)\(\\/)])/gi, '$1&nbsp1nbsp;kg$2');
//space before 'm' only when lower case
txt.value = txt.value.replace(/(\d)\s?m([\s,.;:\)\(\\/)])/g, '$1&nbsp1nbsp;m$2');
txt.value = txt.value.replace(/(\d)\-m([\s,.;:\)\(\\/)])/g, '$1&nbsp1nbsp;m$2');
//txt.value = txt.value.replace(/(\d)\s?ml([\s,.;:\)\(\\/)])/g, '$1&nbsp1nbsp;ml$2');
//txt.value = txt.value.replace(/(\d)\-ml([\s,.;:\)\(\\/)])/g, '$1&nbsp1nbsp;ml$2');
txt.value = txt.value.replace(/(\d)\s?m(\W)/g, '$1&nbsp1nbsp;m$2');
txt.value = txt.value.replace(/(\d)\s?m(\w?\W)/g, '$1&nbsp1nbsp;m$2');
 
// miles per hour
txt.value = txt.value.replace(/m\.p\.h\./gi, 'mph');
txt.value = txt.value.replace(/mph([\s,.;:\)\(\\/)])/gi, 'mph$1');
txt.value = txt.value.replace(/(\d)\s?mph/gi, '$1&nbsp1nbsp;mph');
txt.value = txt.value.replace(/(\d)\-mph/gi, '$1&nbsp1nbsp;mph');
 
// Standardise symbol for pounds
txt.value = txt.value.replace(/(\d)\s?lbs?/gi, '$1&nbsp1nbsp;lb');
txt.value = txt.value.replace(/(\d\+?)\s?lbs?/gi, '$1&nbsp1nbsp;lb');
txt.value = txt.value.replace(/(\d) lbs?/gi, '$1 lb');
txt.value = txt.value.replace(/(\d)\s?(\[\[lbs\]\])/gi, '$1&nbsp1nbsp;\[\[Pound (mass)|lb\]\]');
 
// Standardise symbol for newton metres
Line 100 ⟶ 102:
 
// Standardise symbol for kilowatts
//txt.value = txt.value.replace(/(\d)\s?kW([\s,.;:\)\(\\/)])/gig, '$1&nbsp1nbsp;kW$2');
//txt.value = txt.value.replace(/(\d)\-kW([\s,.;:\)\(\\/)])/gig, '$1&nbsp1nbsp;kW$2');
 
// Standardise symbol for foot pounds
Line 107 ⟶ 109:
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');
Line 112 ⟶ 115:
 
// Symbols for feet and inches
txt.value = txt.value.replace(/([^;°][^;°]\s)(\d{1,4})\s?['’]\s?(\d{1,2})\s?["”][^NESW]/gig, '$1$2 2nbsp;ft $3 3nbsp;in');
txt.value = txt.value.replace(/([\(\|]\d{1,4})\s?['’]\s?(\d{1,2})\s?["”][^NESW]/g, '$1nbsp;ft $2nbsp;in');
txt.value = txt.value.replace(/(\d)\s?ft\s(\d{1,2})\s?in/gi, '$1nbsp;ft $2nbsp;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 119 ⟶ 128:
 
// Add a space before dB
txt.value = txt.value.replace(/(\d)\s?(dB)\b/g, '$1&nbsp1nbsp;$2');
 
// Add a space before several units that were missed before
//txt.value = txt.value.replace(/(\d)\s?(G|M|k|K|h|da|d|c|m|µ|n)?(g|m|Hz|N|W|Pa|V|Ω|F|)([\s,.;:\)\(\\/)])/g, '$1&nbsp1nbsp;$2$3$4');
 
// bps or b/s or bits/s --> bit/s
txt.value = txt.value.replace(/([KkMmGgTtPpEeYyZzKkMmGg])(bps|bits?\/s|b\/s)/g, '$1bit/s');
txt.value = txt.value.replace(/(\d)\s?(bps)/gi, '$1&nbsp1nbsp;bit/s');
 
// Bps or byte/s or bytes/s --> B/s
txt.value = txt.value.replace(/([KkMmGgTtPpEeYyZzKkMmGg])(Bps|bytes?\/s)/g, ' $1B/s');
 
// Make capitalization correct
Line 137 ⟶ 146:
 
// Ensure space is used
txt.value = txt.value.replace(/(\d)\s?(k|M|G)(bit|B)/g, '$1&nbsp1nbsp;$2$3');
 
// Common error
Line 158 ⟶ 167:
addOnloadHook(function () {
if(document.forms.editform) {
addLink('p-cactions', 'javascript:units_nbspformatunits()', 'units_nbspunits', 'ca-unitfixer', 'Fixes some unit formatting', '', '');
}
});