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