User:Bobblewik/monobook.js/unitformatter.js
Jump to navigation
Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Bobblewik/monobook.js/unitformatter. |
function formatunits() {
var txt = document.editform.wpTextbox1;
// Convert ° into ° symbol
txt.value = txt.value.replace(/°/g, '°');
// Convert &sup into superscript ² symbol
txt.value = txt.value.replace(/²/g, '²');
txt.value = txt.value.replace(/³/g, '³');
// Convert the word ohm(s) or the html entity into the actual Ω symbol (Omega, not the actual ohm symbol Ω) and make sure it's 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 $2Ω$4');
// Convert various micro symbols into the 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|°C|M)([\s,.\/\)])/g, '$1 µ$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|°C|M)([\s,.\/\)])/g, '$1 k$2$3');
// Fix common spelling error
txt.value = txt.value.replace(/celcius/gi, 'Celsius');
// Capitalize units correctly
txt.value = txt.value.replace(/(\d)\s?(khz)([\s,.\/\)])/gi, '$1 kHz$3');
txt.value = txt.value.replace(/(\d)\s?(mhz)([\s,.\/\)])/gi, '$1 MHz$3');
txt.value = txt.value.replace(/(\d)\s?(ghz)([\s,.\/\)])/gi, '$1 GHz$3');
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|HZ)([\s,.\/\)])/g, '$1 $2Hz$4');
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)?(pa|PA)([\s,.\/\)])/g, '$1 $2Pa$4');
// Fix kilometres
txt.value = txt.value.replace(/(\d)\s?(kms)([\s,.\/\)])/gi, '$1 km$3');
txt.value = txt.value.replace(/(\d)\s?(km)([\s,.\/\)])/gi, '$1 km$3');
// Fix incorrect symbol for kilometres per hour and add space
txt.value = txt.value.replace(/(\d)\s?(km\/hr)([\s,.\/\)])/gi, '$1 km/h$3');
txt.value = txt.value.replace(/(\d)\s?(kph)([\s,.\/\)])/gi, '$1 km/h$3');
txt.value = txt.value.replace(/(\d)\s?(kmph)([\s,.\/\)])/gi, '$1 km/h$3');
// txt.value = txt.value.replace(/(\d)\s?(kmh)([\s,.\/\)])/gi, '$1 km/h$3');
// txt.value = txt.value.replace(/(\d)\s?(km\/h)([\s,.\/\)])/gi, '$1 km/h$3');
// Space before horsepower symbol
txt.value = txt.value.replace(/(\d)\s?(hp)([\s,.@])/gi, '$1 hp$3');
txt.value = txt.value.replace(/(\d)\s?(bhp)([\s,.@])/gi, '$1 bhp$3');
txt.value = txt.value.replace(/(\d)\s?(shp)([\s,.@])/gi, '$1 shp$3');
// Space before cc
txt.value = txt.value.replace(/(\d)\s?(cc)([\s,.\/\)])/gi, '$1 cc$3');
// Space before ml
txt.value = txt.value.replace(/(\d)\s?(ml)([\s,.\/\)])/gi, '$1 ml$3');
// Space before mm
txt.value = txt.value.replace(/(\d)\s?(mm)([\s,.\/\)])/gi, '$1 mm$3');
// Lower case miles per hour and rpm
txt.value = txt.value.replace(/(\d)\s?(mph)([\s,.\/\)])/gi, '$1 mph$3');
txt.value = txt.value.replace(/(\d) (mph)([\s,.\/\)])/gi, '$1 mph$3');
txt.value = txt.value.replace(/(\d)\s?(rpm)([\s,.\/\)])/gi, '$1 rpm$3');
txt.value = txt.value.replace(/(\d) (rpm)([\s,.\/\)])/gi, '$1 rpm$3');
// Standardise symbol for pounds
txt.value = txt.value.replace(/(\d)\s?(lbs)([\s,.\/\)])/gi, '$1 lb$3');
txt.value = txt.value.replace(/(\d)\s?(\[\[lbs\]\])/gi, '$1 \[\[Pound (mass)|lb\]\]');
// Standardise symbol for foot pounds
txt.value = txt.value.replace(/(\d)\s?(lb[ -.•\/]ft)([\s,.\/\)])/gi, '$1 ft·lbf$3');
txt.value = txt.value.replace(/(\d)\s?(lb[fs][ -.•\/]ft)([\s,.\/\)])/gi, '$1 ft·lbf$3');
txt.value = txt.value.replace(/(\d)\s?(ft[ -.•\/]lb)([\s,.\/\)])/gi, '$1 ft·lbf$3');
txt.value = txt.value.replace(/(\d)\s?(ft[ -.•\/]lb[fs])([\s,.\/\)])/gi, '$1 ft·lbf$3');
txt.value = txt.value.replace(/(\d) (lb[ -.•\/]ft)([\s,.\/\)])/gi, '$1 ft·lbf$3');
txt.value = txt.value.replace(/(\d) (lb[fs][ -.•\/]ft)([\s,.\/\)])/gi, '$1 ft·lbf$3');
txt.value = txt.value.replace(/(\d) (ft[ -.•\/]lb)([\s,.\/\)])/gi, '$1 ft·lbf$3');
txt.value = txt.value.replace(/(\d) (ft[ -.•\/]lb[fs])([\s,.\/\)])/gi, '$1 ft·lbf$3');
// Symbols for feet and inches
// txt.value = txt.value.replace(/(eight[:= ]{1,2})\s?(\d)\s?(['’])\s?(\d{1,2})\s?(["”])/gi, '$1 $2 ft $4 in');
txt.value = txt.value.replace(/([\s:=\/\(])([56])\s?(['’])\s?(1?\d)\s?(["”])/gi, '$1$2 ft $4 in');
txt.value = txt.value.replace(/(eight[:= ]{1,2})\s?(\d)-(\d{1,2})([\s,.\/\)])/gi, '$1 $2 ft $3 in');
// Give digital value a percent symbol '%' instead of word
txt.value = txt.value.replace(/(\d)[\s?-]per *cent([^aei])/gi, '$1%$2');
// Add a space before dB or B
txt.value = txt.value.replace(/(\d)\s?(dB|B)\b/g, '$1 $2');
// Add a space before any units that were missed before
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)?(g|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|°C|°F|M)([\s,.\\/)])/g, '$1 $2$3$4');
// Separate one for seconds since they give a lot of false positives like "1970s". Only difference is mandatory prefix.
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)([\s,.\/\)])/g, '$1 $2$3$4');
// bps or b/s or bits/s --> bit/s
txt.value = txt.value.replace(/([KkMmGgTtPpEeYyZz])(bps|bits?\/s|b\/s)/g, ' $1bit/s');
txt.value = txt.value.replace(/(\d)\s?(bps)/gi, '$1 bit/s');
// Bps or byte/s or bytes/s --> B/s
txt.value = txt.value.replace(/([KkMmGgTtPpEeYyZz])(Bps|bytes?\/s)/g, ' $1B/s');
// After that, make capitalization correct
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');
txt.value = txt.value.replace(/t(bit|B)\/s/g, 'T$1/s');
txt.value = txt.value.replace(/e(bit|B)\/s/g, 'E$1/s');
txt.value = txt.value.replace(/y(bit|B)\/s/g, 'Y$1/s');
txt.value = txt.value.replace(/z(bit|B)\/s/g, 'Z$1/s');
// Common error
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()
}
addOnloadHook(function () {
if(document.forms.editform) {
addLink('p-cactions', 'javascript:formatunits()', 'µ', 'ca-unitfixer', 'Fixes some unit formatting', '', '');
}
});