filesize.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * filesize
  3. *
  4. * @author Jason Mulligan <jason.mulligan@avoidwork.com>
  5. * @copyright 2014 Jason Mulligan
  6. * @license BSD-3 <https://raw.github.com/avoidwork/filesize.js/master/LICENSE>
  7. * @link http://filesizejs.com
  8. * @module filesize
  9. * @version 2.0.4
  10. */
  11. ( function ( global ) {
  12. "use strict";
  13. var bit = /b$/,
  14. radix = 10,
  15. left = /.*\./,
  16. zero = /^0$/;
  17. /**
  18. * filesize
  19. *
  20. * @method filesize
  21. * @param {Mixed} arg String, Int or Float to transform
  22. * @param {Object} descriptor [Optional] Flags
  23. * @return {String} Readable file size String
  24. */
  25. function filesize ( arg, descriptor ) {
  26. var result = "",
  27. skip = false,
  28. val = 0,
  29. e, base, bits, ceil, neg, num, round, unix, spacer, suffix, z, suffixes;
  30. if ( isNaN( arg ) ) {
  31. throw new Error( "Invalid arguments" );
  32. }
  33. descriptor = descriptor || {};
  34. bits = ( descriptor.bits === true );
  35. unix = ( descriptor.unix === true );
  36. base = descriptor.base !== undefined ? descriptor.base : unix ? 2 : 10;
  37. round = descriptor.round !== undefined ? descriptor.round : unix ? 1 : 2;
  38. spacer = descriptor.spacer !== undefined ? descriptor.spacer : unix ? "" : " ";
  39. suffixes = descriptor.suffixes !== undefined ? descriptor.suffixes : {};
  40. num = Number( arg );
  41. neg = ( num < 0 );
  42. ceil = base > 2 ? 1000 : 1024;
  43. // Flipping a negative number to determine the size
  44. if ( neg ) {
  45. num = -num;
  46. }
  47. // Zero is now a special case because bytes divide by 1
  48. if ( num === 0 ) {
  49. if ( unix ) {
  50. result = "0";
  51. }
  52. else {
  53. suffix = "B";
  54. result = "0" + spacer + ( suffixes[suffix] || suffix );
  55. }
  56. }
  57. else {
  58. e = Math.floor( Math.log( num ) / Math.log( 1000 ) );
  59. // Exceeding supported length, time to reduce & multiply
  60. if ( e > 8 ) {
  61. val = val * ( 1000 * ( e - 8 ) );
  62. e = 8;
  63. }
  64. if ( base === 2 ) {
  65. val = num / Math.pow( 2, ( e * 10 ) );
  66. }
  67. else {
  68. val = num / Math.pow( 1000, e );
  69. }
  70. if ( bits ) {
  71. val = ( val * 8 );
  72. if ( val > ceil ) {
  73. val = val / ceil;
  74. e++;
  75. }
  76. }
  77. result = val.toFixed( e > 0 ? round : 0 );
  78. suffix = si[bits ? "bits" : "bytes"][e];
  79. if ( !skip && unix ) {
  80. if ( bits && bit.test( suffix ) ) {
  81. suffix = suffix.toLowerCase();
  82. }
  83. suffix = suffix.charAt( 0 );
  84. z = result.replace( left, "" );
  85. if ( suffix === "B" ) {
  86. suffix = "";
  87. }
  88. else if ( !bits && suffix === "k" ) {
  89. suffix = "K";
  90. }
  91. if ( zero.test( z ) ) {
  92. result = parseInt( result, radix ).toString();
  93. }
  94. result += spacer + ( suffixes[suffix] || suffix );
  95. }
  96. else if ( !unix ) {
  97. result += spacer + ( suffixes[suffix] || suffix );
  98. }
  99. }
  100. // Decorating a 'diff'
  101. if ( neg ) {
  102. result = "-" + result;
  103. }
  104. return result;
  105. }
  106. /**
  107. * SI suffixes
  108. *
  109. * @type {Object}
  110. */
  111. var si = {
  112. bits : ["B", "kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"],
  113. bytes : ["B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
  114. };
  115. // CommonJS, AMD, script tag
  116. if ( typeof exports !== "undefined" ) {
  117. module.exports = filesize;
  118. }
  119. else if ( typeof define === "function" ) {
  120. define( function () {
  121. return filesize;
  122. } );
  123. }
  124. else {
  125. global.filesize = filesize;
  126. }
  127. } )( this );