Comparing version
@@ -0,1 +1,3 @@ | ||
"use strict"; | ||
/** | ||
@@ -9,149 +11,148 @@ * filesize | ||
* @module filesize | ||
* @version 3.0.2 | ||
* @version 3.1.0 | ||
*/ | ||
( function ( global ) { | ||
"use strict"; | ||
(function (global) { | ||
var bit = /b$/; | ||
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 | ||
*/ | ||
var filesize = function (arg, descriptor) { | ||
var result = [], | ||
skip = false, | ||
val = 0, | ||
e = undefined, | ||
base = undefined, | ||
bits = undefined, | ||
ceil = undefined, | ||
neg = undefined, | ||
num = undefined, | ||
output = undefined, | ||
round = undefined, | ||
unix = undefined, | ||
spacer = undefined, | ||
suffixes = undefined; | ||
/** | ||
* 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, output, round, unix, spacer, suffixes; | ||
if (isNaN(arg)) { | ||
throw new Error("Invalid arguments"); | ||
} | ||
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"; | ||
e = descriptor.exponent !== undefined ? descriptor.exponent : -1; | ||
num = Number(arg); | ||
neg = num < 0; | ||
ceil = base > 2 ? 1000 : 1024; | ||
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"; | ||
e = descriptor.exponent !== undefined ? descriptor.exponent : -1; | ||
num = Number( arg ); | ||
neg = ( num < 0 ); | ||
ceil = base > 2 ? 1000 : 1024; | ||
// Flipping a negative number to determine the size | ||
if (neg) { | ||
num = -num; | ||
} | ||
// 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; | ||
// 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 { | ||
// Determining the exponent | ||
if (e === -1 || isNaN(e)) { | ||
e = Math.floor(Math.log(num) / Math.log(ceil)); | ||
} | ||
if ( unix ) { | ||
result[ 1 ] = ""; | ||
} | ||
else { | ||
result[ 1 ] = "B"; | ||
} | ||
} | ||
else { | ||
// Determining the exponent | ||
if ( e === -1 || isNaN( e ) ) { | ||
e = Math.floor( Math.log( num ) / Math.log( ceil ) ); | ||
} | ||
// Exceeding supported length, time to reduce & multiply | ||
if (e > 8) { | ||
val = val * (1000 * (e - 8)); | ||
e = 8; | ||
} | ||
// Exceeding supported length, time to reduce & multiply | ||
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 ( base === 2 ) { | ||
val = num / Math.pow( 2, ( e * 10 ) ); | ||
} | ||
else { | ||
val = num / Math.pow( 1000, e ); | ||
} | ||
if (bits) { | ||
val = val * 8; | ||
if ( bits ) { | ||
val = ( val * 8 ); | ||
if (val > ceil) { | ||
val = val / ceil; | ||
e++; | ||
} | ||
} | ||
if ( val > ceil ) { | ||
val = val / ceil; | ||
e++; | ||
} | ||
} | ||
result[0] = Number(val.toFixed(e > 0 ? round : 0)); | ||
result[1] = si[bits ? "bits" : "bytes"][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(); | ||
} | ||
if ( !skip && unix ) { | ||
if ( bits && bit.test( result[ 1 ] ) ) { | ||
result[ 1 ] = result[ 1 ].toLowerCase(); | ||
} | ||
result[1] = result[1].charAt(0); | ||
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"; | ||
} | ||
} | ||
} | ||
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]; | ||
} | ||
// Decorating a 'diff' | ||
if ( neg ) { | ||
result[ 0 ] = -result[ 0 ]; | ||
} | ||
// Applying custom suffix | ||
result[1] = suffixes[result[1]] || result[1]; | ||
// Applying custom suffix | ||
result[ 1 ] = suffixes[ result[ 1 ] ] || result[ 1 ]; | ||
// Returning Array, Object, or String (default) | ||
if (output === "array") { | ||
return result; | ||
} else if (output === "exponent") { | ||
return e; | ||
} else if (output === "object") { | ||
return { value: result[0], suffix: result[1] }; | ||
} else { | ||
return result.join(spacer); | ||
} | ||
}; | ||
// Returning Array, Object, or String (default) | ||
if ( output === "array" ) { | ||
return result; | ||
} | ||
else if ( output === "exponent" ) { | ||
return e; | ||
} | ||
else if ( output === "object" ) { | ||
return { value: result[ 0 ], suffix: result[ 1 ] }; | ||
} | ||
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"] | ||
}; | ||
/** | ||
* 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 ); | ||
// 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); |
{ | ||
"name": "filesize", | ||
"description": "JavaScript library to generate a human readable String describing the file size", | ||
"version": "3.0.2", | ||
"version": "3.1.0", | ||
"homepage": "http://filesizejs.com", | ||
@@ -34,4 +34,4 @@ "author": { | ||
"grunt-sed": "~0.1", | ||
"grunt-6to5": "^2.0.0", | ||
"grunt-contrib-concat": "~0.1.3", | ||
"grunt-contrib-jshint": "~0.1", | ||
"grunt-contrib-nodeunit": "~0.1.2", | ||
@@ -38,0 +38,0 @@ "grunt-contrib-watch": "~0.2", |
# filesize.js | ||
[](http://travis-ci.org/avoidwork/filesize.js) | ||
[](http://travis-ci.org/avoidwork/filesize.js) [](https://gitter.im/avoidwork/filesize.js?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) | ||
@@ -5,0 +5,0 @@ filesize.js provides a simple way to get a human readable file size string from a number (float or integer) or string. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
43238
16.39%18
5.88%281
81.29%1
Infinity%