Jump to content

User:Omegatron/monobook.js/unitformatter.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Omegatron (talk | contribs) at 06:29, 12 January 2006. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
/*

== Unit formatter ==

This script adds a tab to fix some unit formatting in the article.  When you press the tab, it adds spaces between the number and the unit, as in SI, changes some HTML entities into the actual Unicode characters, and fixes some common unit errors, like KHZ → kHz.

Then it adds an edit summary and presses ''Show changes'' for you so you can check it for mistakes.

By [[User:Omegatron]]

<pre><nowiki> */

// ================================================================
// Unit formatter - new tab adds spaces between number and units, makes units consistent
// by User:Omegatron

function formatunits() {
    var txt = document.editform.wpTextbox1;

    // Convert all &deg; into actual ° symbol
    txt.value = txt.value.replace(/&deg;/g, '°');

    // 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 $2Ω$4');

    // 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 µ$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');

    // 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');

    // 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|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');

    // 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 = "[[User:Omegatron#Regular expressions|Regex unit formatter]]";
	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()
}

//</nowiki></pre>