Project

General

Profile

1
"use strict";
2

    
3
/**
4
 * filesize
5
 *
6
 * @copyright 2016 Jason Mulligan <jason.mulligan@avoidwork.com>
7
 * @license BSD-3-Clause
8
 * @version 3.2.1
9
 */
10
(function (global) {
11
	var b = /^(b|B)$/;
12
	var symbol = {
13
		bits: ["b", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"],
14
		bytes: ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
15
	};
16

    
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) {
26
		var descriptor = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
27

    
28
		var result = [],
29
		    val = 0,
30
		    e = undefined,
31
		    base = undefined,
32
		    bits = undefined,
33
		    ceil = undefined,
34
		    neg = undefined,
35
		    num = undefined,
36
		    output = undefined,
37
		    round = undefined,
38
		    unix = undefined,
39
		    spacer = undefined,
40
		    symbols = undefined;
41

    
42
		if (isNaN(arg)) {
43
			throw new Error("Invalid arguments");
44
		}
45

    
46
		bits = descriptor.bits === true;
47
		unix = descriptor.unix === true;
48
		base = descriptor.base || 2;
49
		round = descriptor.round !== undefined ? descriptor.round : unix ? 1 : 2;
50
		spacer = descriptor.spacer !== undefined ? descriptor.spacer : unix ? "" : " ";
51
		symbols = descriptor.symbols || descriptor.suffixes || {};
52
		output = descriptor.output || "string";
53
		e = descriptor.exponent !== undefined ? descriptor.exponent : -1;
54
		num = Number(arg);
55
		neg = num < 0;
56
		ceil = base > 2 ? 1000 : 1024;
57

    
58
		// Flipping a negative number to determine the size
59
		if (neg) {
60
			num = -num;
61
		}
62

    
63
		// Zero is now a special case because bytes divide by 1
64
		if (num === 0) {
65
			result[0] = 0;
66
			result[1] = unix ? "" : !bits ? "B" : "b";
67
		} else {
68
			// Determining the exponent
69
			if (e === -1 || isNaN(e)) {
70
				e = Math.floor(Math.log(num) / Math.log(ceil));
71

    
72
				if (e < 0) {
73
					e = 0;
74
				}
75
			}
76

    
77
			// Exceeding supported length, time to reduce & multiply
78
			if (e > 8) {
79
				e = 8;
80
			}
81

    
82
			val = base === 2 ? num / Math.pow(2, e * 10) : num / Math.pow(1000, e);
83

    
84
			if (bits) {
85
				val = val * 8;
86

    
87
				if (val > ceil && e < 8) {
88
					val = val / ceil;
89
					e++;
90
				}
91
			}
92

    
93
			result[0] = Number(val.toFixed(e > 0 ? round : 0));
94
			result[1] = base === 10 && e === 1 ? bits ? "kb" : "kB" : symbol[bits ? "bits" : "bytes"][e];
95

    
96
			if (unix) {
97
				result[1] = result[1].charAt(0);
98

    
99
				if (b.test(result[1])) {
100
					result[0] = Math.floor(result[0]);
101
					result[1] = "";
102
				}
103
			}
104
		}
105

    
106
		// Decorating a 'diff'
107
		if (neg) {
108
			result[0] = -result[0];
109
		}
110

    
111
		// Applying custom suffix
112
		result[1] = symbols[result[1]] || result[1];
113

    
114
		// Returning Array, Object, or String (default)
115
		if (output === "array") {
116
			return result;
117
		}
118

    
119
		if (output === "exponent") {
120
			return e;
121
		}
122

    
123
		if (output === "object") {
124
			return { value: result[0], suffix: result[1], symbol: result[1] };
125
		}
126

    
127
		return result.join(spacer);
128
	}
129

    
130
	// CommonJS, AMD, script tag
131
	if (typeof exports !== "undefined") {
132
		module.exports = filesize;
133
	} else if (typeof define === "function" && define.amd) {
134
		define(function () {
135
			return filesize;
136
		});
137
	} else {
138
		global.filesize = filesize;
139
	}
140
})(typeof window !== "undefined" ? window : global);
(4-4/10)