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

Content deleted Content added
No edit summary
m Maintenance: Fixing deprecated call to addPortletLink (mw:ResourceLoader/Migration_guide_(users)#addPortletLink)
 
(14 intermediate revisions by 2 users not shown)
Line 1:
// See also [[User:Atrian/monobook.js/unitformatter.js]] and [[User:Bobblewik/monobook.js/unitformatter.js]]
// <pre><nowiki>
 
// ================================================================
// Unit formatter - new tab adds spaces between number and units, makes units consistent
// by User:Omegatron
 
function formatunits() {
Line 12 ⟶ 8:
 
// Convert the word ohm(s) or the html entity into the actual Ω symbol (Omega, not the actual ohm symbol &#8486;) 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|-)?(&Omega;|ohm|Ohm)s?([\s,.])/g, '$1 &nbsp;$2Ω$45');
 
// Convert various micro symbols into the actual micro symbol, make sure it's spaced
txt.value = txt.value.replace(/(\d)\s?(&mu;|μ|&micro;)(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 &nbsp;µ$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 &nbsp;k$2$3');
 
// Capitalize units correctly
Line 24 ⟶ 20:
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 &nbsp;$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 &nbsp;$2Pa$4');
 
// Add a space before dB or B
txt.value = txt.value.replace(/(\d)\s?(dB|B\w*)\b/g, '$1 &nbsp;$2');
txt.value = txt.value.replace(/(\d)\s?B\b/g, '$1&nbsp;B');
 
// 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|M)([\s,.])/g, '$1 &nbsp;$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 &nbsp;$2$3$4');
 
// bps or b/s or bits/s --> bit/s
Line 68 ⟶ 65:
}
 
$(function () {
//</nowiki></pre>
if(document.forms.editform) {
mw.util.addPortletLink('p-cactions', 'javascript:formatunits()', 'µ', 'ca-unitfixer', 'Fixes some unit formatting', '', '');
}
});