summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/UploadWizard/resources/uw.units.js
diff options
context:
space:
mode:
Diffstat (limited to 'www/wiki/extensions/UploadWizard/resources/uw.units.js')
-rw-r--r--www/wiki/extensions/UploadWizard/resources/uw.units.js25
1 files changed, 25 insertions, 0 deletions
diff --git a/www/wiki/extensions/UploadWizard/resources/uw.units.js b/www/wiki/extensions/UploadWizard/resources/uw.units.js
new file mode 100644
index 00000000..c8ee73ee
--- /dev/null
+++ b/www/wiki/extensions/UploadWizard/resources/uw.units.js
@@ -0,0 +1,25 @@
+( function ( mw, uw ) {
+
+ var scaleMsgKeys = [ 'size-bytes', 'size-kilobytes', 'size-megabytes', 'size-gigabytes' ];
+
+ uw.units = {
+ /**
+ * Format a size in bytes for output, using an appropriate
+ * unit (bytes, K, MB, GB, or TB) according to the magnitude in question
+ *
+ * Units above K get 2 fixed decimal places.
+ *
+ * @param {number} size Number of bytes
+ * @return {string} formatted size
+ */
+ bytes: function ( size ) {
+ var i = 0;
+ while ( size >= 1024 && i < scaleMsgKeys.length - 1 ) {
+ size /= 1024.0;
+ i++;
+ }
+ return mw.message( scaleMsgKeys[ i ], size.toFixed( i > 1 ? 2 : 0 ) ).text();
+ }
+ };
+
+}( mediaWiki, mediaWiki.uploadWizard ) );