|
| 1 | +/** |
| 2 | + * filesize |
| 3 | + * |
| 4 | + * @author Jason Mulligan <[email protected]> |
| 5 | + * @copyright 2015 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 3.1.0 |
| 10 | + */ |
| 11 | +( global ) => { |
| 12 | +const bit = /b$/; |
| 13 | + |
| 14 | +/** |
| 15 | + * filesize |
| 16 | + * |
| 17 | + * @method filesize |
| 18 | + * @param {Mixed} arg String, Int or Float to transform |
| 19 | + * @param {Object} descriptor [Optional] Flags |
| 20 | + * @return {String} Readable file size String |
| 21 | + */ |
| 22 | +let filesize = ( arg, descriptor ) => { |
| 23 | + let result = [], |
| 24 | + skip = false, |
| 25 | + val = 0, |
| 26 | + e, base, bits, ceil, neg, num, output, round, unix, spacer, suffixes; |
| 27 | + |
| 28 | + if ( isNaN( arg ) ) { |
| 29 | + throw new Error( "Invalid arguments" ); |
| 30 | + } |
| 31 | + |
| 32 | + descriptor = descriptor || {}; |
| 33 | + bits = ( descriptor.bits === true ); |
| 34 | + unix = ( descriptor.unix === true ); |
| 35 | + base = descriptor.base !== undefined ? descriptor.base : 2; |
| 36 | + round = descriptor.round !== undefined ? descriptor.round : unix ? 1 : 2; |
| 37 | + spacer = descriptor.spacer !== undefined ? descriptor.spacer : unix ? "" : " "; |
| 38 | + suffixes = descriptor.suffixes !== undefined ? descriptor.suffixes : {}; |
| 39 | + output = descriptor.output !== undefined ? descriptor.output : "string"; |
| 40 | + e = descriptor.exponent !== undefined ? descriptor.exponent : -1; |
| 41 | + num = Number( arg ); |
| 42 | + neg = ( num < 0 ); |
| 43 | + ceil = base > 2 ? 1000 : 1024; |
| 44 | + |
| 45 | + // Flipping a negative number to determine the size |
| 46 | + if ( neg ) { |
| 47 | + num = -num; |
| 48 | + } |
| 49 | + |
| 50 | + // Zero is now a special case because bytes divide by 1 |
| 51 | + if ( num === 0 ) { |
| 52 | + result[ 0 ] = 0; |
| 53 | + |
| 54 | + if ( unix ) { |
| 55 | + result[ 1 ] = ""; |
| 56 | + } else { |
| 57 | + result[ 1 ] = "B"; |
| 58 | + } |
| 59 | + } else { |
| 60 | + // Determining the exponent |
| 61 | + if ( e === -1 || isNaN( e ) ) { |
| 62 | + e = Math.floor( Math.log( num ) / Math.log( ceil ) ); |
| 63 | + } |
| 64 | + |
| 65 | + // Exceeding supported length, time to reduce & multiply |
| 66 | + if ( e > 8 ) { |
| 67 | + val = val * ( 1000 * ( e - 8 ) ); |
| 68 | + e = 8; |
| 69 | + } |
| 70 | + |
| 71 | + if ( base === 2 ) { |
| 72 | + val = num / Math.pow( 2, ( e * 10 ) ); |
| 73 | + } else { |
| 74 | + val = num / Math.pow( 1000, e ); |
| 75 | + } |
| 76 | + |
| 77 | + if ( bits ) { |
| 78 | + val = ( val * 8 ); |
| 79 | + |
| 80 | + if ( val > ceil ) { |
| 81 | + val = val / ceil; |
| 82 | + e++; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + result[ 0 ] = Number( val.toFixed( e > 0 ? round : 0 ) ); |
| 87 | + result[ 1 ] = si[ bits ? "bits" : "bytes" ][ e ]; |
| 88 | + |
| 89 | + if ( !skip && unix ) { |
| 90 | + if ( bits && bit.test( result[ 1 ] ) ) { |
| 91 | + result[ 1 ] = result[ 1 ].toLowerCase(); |
| 92 | + } |
| 93 | + |
| 94 | + result[ 1 ] = result[ 1 ].charAt( 0 ); |
| 95 | + |
| 96 | + if ( result[ 1 ] === "B" ) { |
| 97 | + result[ 0 ] = Math.floor( result[ 0 ] ); |
| 98 | + result[ 1 ] = ""; |
| 99 | + } else if ( !bits && result[ 1 ] === "k" ) { |
| 100 | + result[ 1 ] = "K"; |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + // Decorating a 'diff' |
| 106 | + if ( neg ) { |
| 107 | + result[ 0 ] = -result[ 0 ]; |
| 108 | + } |
| 109 | + |
| 110 | + // Applying custom suffix |
| 111 | + result[ 1 ] = suffixes[ result[ 1 ] ] || result[ 1 ]; |
| 112 | + |
| 113 | + // Returning Array, Object, or String (default) |
| 114 | + if ( output === "array" ) { |
| 115 | + return result; |
| 116 | + } else if ( output === "exponent" ) { |
| 117 | + return e; |
| 118 | + } else if ( output === "object" ) { |
| 119 | + return { value: result[ 0 ], suffix: result[ 1 ] }; |
| 120 | + } else { |
| 121 | + return result.join( spacer ); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * SI suffixes |
| 127 | + * |
| 128 | + * @type {Object} |
| 129 | + */ |
| 130 | +const si = { |
| 131 | + bits: [ "B", "kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb" ], |
| 132 | + bytes: [ "B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" ] |
| 133 | +}; |
| 134 | + |
| 135 | +// CommonJS, AMD, script tag |
| 136 | +if ( typeof exports !== "undefined" ) { |
| 137 | + module.exports = filesize; |
| 138 | +} else if ( typeof define === "function" ) { |
| 139 | + define( () => { |
| 140 | + return filesize; |
| 141 | + } ); |
| 142 | +} else { |
| 143 | + global.filesize = filesize; |
| 144 | +} |
| 145 | +}( this ); |
0 commit comments