Socket
Socket
Sign inDemoInstall

filesize

Package Overview
Dependencies
Maintainers
1
Versions
124
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

filesize - npm Package Compare versions

Comparing version 3.3.0 to 3.4.0

206

lib/filesize.es6.js
/**
* filesize
*
* @copyright 2016 Jason Mulligan <jason.mulligan@avoidwork.com>
* @copyright 2017 Jason Mulligan <jason.mulligan@avoidwork.com>
* @license BSD-3-Clause
* @version 3.3.0
* @version 3.4.0
*/
(function (global) {
const b = /^(b|B)$/;
const symbol = {
iec: {
bits: ["b", "Kib", "Mib", "Gib", "Tib", "Pib", "Eib", "Zib", "Yib"],
bytes: ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]
},
jedec: {
bits: ["b", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"],
bytes: ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
}
};
const b = /^(b|B)$/,
symbol = {
iec: {
bits: ["b", "Kib", "Mib", "Gib", "Tib", "Pib", "Eib", "Zib", "Yib"],
bytes: ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]
},
jedec: {
bits: ["b", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"],
bytes: ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
}
};
/**
* 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 = {}) {
let result = [],
val = 0,
e, base, bits, ceil, neg, num, output, round, unix, spacer, standard, symbols;
/**
* 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 = {}) {
let result = [],
val = 0,
e, base, bits, ceil, neg, num, output, round, unix, spacer, standard, symbols;
if (isNaN(arg)) {
throw new Error("Invalid arguments");
}
if (isNaN(arg)) {
throw new Error("Invalid arguments");
}
bits = descriptor.bits === true;
unix = descriptor.unix === true;
base = descriptor.base || 2;
round = descriptor.round !== undefined ? descriptor.round : unix ? 1 : 2;
spacer = descriptor.spacer !== undefined ? descriptor.spacer : unix ? "" : " ";
symbols = descriptor.symbols || descriptor.suffixes || {};
standard = base === 2 ? descriptor.standard || "jedec" : "jedec";
output = descriptor.output || "string";
e = descriptor.exponent !== undefined ? descriptor.exponent : -1;
num = Number(arg);
neg = num < 0;
ceil = base > 2 ? 1000 : 1024;
bits = descriptor.bits === true;
unix = descriptor.unix === true;
base = descriptor.base || 2;
round = descriptor.round !== undefined ? descriptor.round : unix ? 1 : 2;
spacer = descriptor.spacer !== undefined ? descriptor.spacer : unix ? "" : " ";
symbols = descriptor.symbols || descriptor.suffixes || {};
standard = base === 2 ? descriptor.standard || "jedec" : "jedec";
output = 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;
result[1] = unix ? "" : !bits ? "B" : "b";
} else {
// Determining the exponent
if (e === -1 || isNaN(e)) {
e = Math.floor(Math.log(num) / Math.log(ceil));
// Zero is now a special case because bytes divide by 1
if (num === 0) {
result[0] = 0;
result[1] = unix ? "" : !bits ? "B" : "b";
} else {
// Determining the exponent
if (e === -1 || isNaN(e)) {
e = Math.floor(Math.log(num) / Math.log(ceil));
if (e < 0) {
e = 0;
if (e < 0) {
e = 0;
}
}
}
// Exceeding supported length, time to reduce & multiply
if (e > 8) {
e = 8;
}
// Exceeding supported length, time to reduce & multiply
if (e > 8) {
e = 8;
}
val = base === 2 ? num / Math.pow(2, e * 10) : num / Math.pow(1000, e);
val = base === 2 ? num / Math.pow(2, e * 10) : num / Math.pow(1000, e);
if (bits) {
val = val * 8;
if (bits) {
val = val * 8;
if (val > ceil && e < 8) {
val = val / ceil;
e++;
if (val > ceil && e < 8) {
val = val / ceil;
e++;
}
}
}
result[0] = Number(val.toFixed(e > 0 ? round : 0));
result[1] = base === 10 && e === 1 ? bits ? "kb" : "kB" : symbol[standard][bits ? "bits" : "bytes"][e];
result[0] = Number(val.toFixed(e > 0 ? round : 0));
result[1] = base === 10 && e === 1 ? bits ? "kb" : "kB" : symbol[standard][bits ? "bits" : "bytes"][e];
if (unix) {
result[1] = standard === "jedec" ? result[1].charAt(0) : e > 0 ? result[1].replace(/B$/, "") : result[1];
if (unix) {
result[1] = standard === "jedec" ? result[1].charAt(0) : e > 0 ? result[1].replace(/B$/, "") : result[1];
if (b.test(result[1])) {
result[0] = Math.floor(result[0]);
result[1] = "";
if (b.test(result[1])) {
result[0] = Math.floor(result[0]);
result[1] = "";
}
}
}
}
// Decorating a 'diff'
if (neg) {
result[0] = -result[0];
}
// Decorating a 'diff'
if (neg) {
result[0] = -result[0];
}
// Applying custom symbol
result[1] = symbols[result[1]] || result[1];
// Applying custom symbol
result[1] = symbols[result[1]] || result[1];
// Returning Array, Object, or String (default)
if (output === "array") {
return result;
}
// Returning Array, Object, or String (default)
if (output === "array") {
return result;
}
if (output === "exponent") {
return e;
}
if (output === "exponent") {
return e;
}
if (output === "object") {
return {value: result[0], suffix: result[1], symbol: result[1]};
if (output === "object") {
return {value: result[0], suffix: result[1], symbol: result[1]};
}
return result.join(spacer);
}
return result.join(spacer);
}
// Partial application for functional programming
filesize.partial = (descriptor = {}) => arg => filesize(arg, descriptor);
// CommonJS, AMD, script tag
if (typeof exports !== "undefined") {
module.exports = filesize;
} else if (typeof define === "function" && define.amd) {
define(() => {
return filesize;
});
} else {
global.filesize = filesize;
}}(typeof window !== "undefined" ? window : global));
// CommonJS, AMD, script tag
if (typeof exports !== "undefined") {
module.exports = filesize;
} else if (typeof define === "function" && define.amd) {
define(() => {
return filesize;
});
} else {
global.filesize = filesize;
}
}(typeof window !== "undefined" ? window : global));

@@ -6,9 +6,9 @@ "use strict";

*
* @copyright 2016 Jason Mulligan <jason.mulligan@avoidwork.com>
* @copyright 2017 Jason Mulligan <jason.mulligan@avoidwork.com>
* @license BSD-3-Clause
* @version 3.3.0
* @version 3.4.0
*/
(function (global) {
var b = /^(b|B)$/;
var symbol = {
var b = /^(b|B)$/,
symbol = {
iec: {

@@ -33,3 +33,3 @@ bits: ["b", "Kib", "Mib", "Gib", "Tib", "Pib", "Eib", "Zib", "Yib"],

function filesize(arg) {
var descriptor = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var descriptor = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

@@ -140,2 +140,10 @@ var result = [],

// Partial application for functional programming
filesize.partial = function () {
var descriptor = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return function (arg) {
return filesize(arg, descriptor);
};
};
// CommonJS, AMD, script tag

@@ -142,0 +150,0 @@ if (typeof exports !== "undefined") {

{
"name": "filesize",
"description": "JavaScript library to generate a human readable String describing the file size",
"version": "3.3.0",
"version": "3.4.0",
"homepage": "http://filesizejs.com",

@@ -23,15 +23,13 @@ "author": "Jason Mulligan <jason.mulligan@avoidwork.com>",

"devDependencies": {
"babel-eslint": "^4.1.4",
"babel-preset-es2015": "^6.1.2",
"grunt": "^0.4.5",
"grunt-babel": "^6.0.0",
"grunt-cli": "^0.1.13",
"grunt-contrib-concat": "^0.1.3",
"grunt-contrib-watch": "^0.2.0",
"grunt-eslint": "^17.3.1",
"grunt-contrib-nodeunit": "^0.4.1",
"grunt-contrib-uglify": "^0.9.1",
"grunt-sed": "^0.1.1"
"babel-preset-es2015": "~6.18.0",
"grunt": "~1.0.1",
"grunt-babel": "~6.0.0",
"grunt-cli": "~1.2.0",
"grunt-contrib-concat": "~1.0.1",
"grunt-contrib-watch": "~1.0.0",
"grunt-eslint": "~19.0.0",
"grunt-contrib-nodeunit": "~1.0.0",
"grunt-contrib-uglify": "~2.0.0"
},
"keywords": ["file", "filesize", "size", "readable", "file system", "bytes", "diff"]
}
# filesize.js
[![build status](https://secure.travis-ci.org/avoidwork/filesize.js.png)](http://travis-ci.org/avoidwork/filesize.js) [![Join the chat at https://gitter.im/avoidwork/filesize.js](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/avoidwork/filesize.js?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![build status](https://secure.travis-ci.org/avoidwork/filesize.js.png)](http://travis-ci.org/avoidwork/filesize.js) [![downloads](https://img.shields.io/npm/dt/filesize.svg)](https://www.npmjs.com/package/filesize)

@@ -58,7 +58,18 @@ filesize.js provides a simple way to get a human readable file size string from a number (float or integer) or string.

## Partial Application
`filesize.partial()` takes the second parameter of `filesize()` and returns a new function with the configuration applied
upon execution. This can be used to reduce `Object` creation if you call `filesize()` without caching the `descriptor`
in lexical scope.
```javascript
const size = filesize.partial({standard: "iec"});
size(265318); // "259.1 KiB"
```
## How can I load filesize.js?
filesize.js supports AMD loaders (require.js, curl.js, etc.), node.js & npm (npm install filesize), or using a script tag.
filesize.js supports AMD loaders (require.js, curl.js, etc.), node.js & npm (```npm install filesize```), or using a script tag.
## License
Copyright (c) 2016 Jason Mulligan
Copyright (c) 2017 Jason Mulligan
Licensed under the BSD-3 license.

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc