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 4.2.1 to 5.0.0

filesize-5.0.0.tgz

189

lib/filesize.js

@@ -1,188 +0,1 @@

"use strict";
/**
* filesize
*
* @copyright 2019 Jason Mulligan <jason.mulligan@avoidwork.com>
* @license BSD-3-Clause
* @version 4.2.1
*/
(function (global) {
var 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"]
}
},
fullform = {
iec: ["", "kibi", "mebi", "gibi", "tebi", "pebi", "exbi", "zebi", "yobi"],
jedec: ["", "kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta"]
};
/**
* 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) {
var descriptor = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var result = [],
val = 0,
e = void 0,
base = void 0,
bits = void 0,
ceil = void 0,
full = void 0,
fullforms = void 0,
locale = void 0,
localeOptions = void 0,
neg = void 0,
num = void 0,
output = void 0,
round = void 0,
unix = void 0,
separator = void 0,
spacer = void 0,
standard = void 0,
symbols = void 0;
if (isNaN(arg)) {
throw new TypeError("Invalid number");
}
bits = descriptor.bits === true;
unix = descriptor.unix === true;
base = descriptor.base || 2;
round = descriptor.round !== void 0 ? descriptor.round : unix ? 1 : 2;
locale = descriptor.locale !== void 0 ? descriptor.locale : "";
localeOptions = descriptor.localeOptions || {};
separator = descriptor.separator !== void 0 ? descriptor.separator : "";
spacer = descriptor.spacer !== void 0 ? descriptor.spacer : unix ? "" : " ";
symbols = descriptor.symbols || {};
standard = base === 2 ? descriptor.standard || "jedec" : "jedec";
output = descriptor.output || "string";
full = descriptor.fullform === true;
fullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : [];
e = descriptor.exponent !== void 0 ? 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;
} // Determining the exponent
if (e === -1 || isNaN(e)) {
e = Math.floor(Math.log(num) / Math.log(ceil));
if (e < 0) {
e = 0;
}
} // Exceeding supported length, time to reduce & multiply
if (e > 8) {
e = 8;
}
if (output === "exponent") {
return e;
} // Zero is now a special case because bytes divide by 1
if (num === 0) {
result[0] = 0;
result[1] = unix ? "" : symbol[standard][bits ? "bits" : "bytes"][e];
} else {
val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));
if (bits) {
val = val * 8;
if (val >= ceil && e < 8) {
val = val / ceil;
e++;
}
}
result[0] = Number(val.toFixed(e > 0 ? round : 0));
if (result[0] === ceil && e < 8 && descriptor.exponent === void 0) {
result[0] = 1;
e++;
}
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 (b.test(result[1])) {
result[0] = Math.floor(result[0]);
result[1] = "";
}
}
} // Decorating a 'diff'
if (neg) {
result[0] = -result[0];
} // Applying custom symbol
result[1] = symbols[result[1]] || result[1];
if (locale === true) {
result[0] = result[0].toLocaleString();
} else if (locale.length > 0) {
result[0] = result[0].toLocaleString(locale, localeOptions);
} else if (separator.length > 0) {
result[0] = result[0].toString().replace(".", separator);
} // Returning Array, Object, or String (default)
if (output === "array") {
return result;
}
if (full) {
result[1] = fullforms[e] ? fullforms[e] : fullform[standard][e] + (bits ? "bit" : "byte") + (result[0] === 1 ? "" : "s");
}
if (output === "object") {
return {
value: result[0],
symbol: result[1]
};
}
return result.join(spacer);
} // Partial application for functional programming
filesize.partial = function (opt) {
return function (arg) {
return filesize(arg, opt);
};
}; // CommonJS, AMD, script tag
if (typeof exports !== "undefined") {
module.exports = filesize;
} else if (typeof define === "function" && define.amd !== void 0) {
define(function () {
return filesize;
});
} else {
global.filesize = filesize;
}
})(typeof window !== "undefined" ? window : global);
!function(e,i){"object"==typeof exports&&"undefined"!=typeof module?module.exports=i():"function"==typeof define&&define.amd?define(i):(e=e||self).filesize=i()}(this,(function(){"use strict";const e=/^(b|B)$/,i={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"]}},b={iec:["","kibi","mebi","gibi","tebi","pebi","exbi","zebi","yobi"],jedec:["","kilo","mega","giga","tera","peta","exa","zetta","yotta"]};function t(t,o={}){if(isNaN(t))throw new TypeError("Invalid number");let n=[],a=0,B=!0===o.bits,r=!0===o.unix,d=o.base||2,s=void 0!==o.round?o.round:r?1:2,c=void 0!==o.locale?o.locale:"",f=o.localeOptions||{},u=void 0!==o.separator?o.separator:"",y=void 0!==o.spacer?o.spacer:r?"":" ",M=o.symbols||{},h=2===d&&o.standard||"jedec",l=o.output||"string",m=!0===o.fullform,p=o.fullforms instanceof Array?o.fullforms:[],v=void 0!==o.exponent?o.exponent:-1,j=Number(t),N=j<0,g=d>2?1e3:1024;return N&&(j=-j),(-1===v||isNaN(v))&&(v=Math.floor(Math.log(j)/Math.log(g)))<0&&(v=0),v>8&&(v=8),"exponent"===l?v:(0===j?(n[0]=0,n[1]=r?"":i[h][B?"bits":"bytes"][v]):(a=j/(2===d?Math.pow(2,10*v):Math.pow(1e3,v)),B&&(a*=8)>=g&&v<8&&(a/=g,v++),n[0]=Number(a.toFixed(v>0?s:0)),n[0]===g&&v<8&&void 0===o.exponent&&(n[0]=1,v++),n[1]=10===d&&1===v?B?"kb":"kB":i[h][B?"bits":"bytes"][v],r&&(n[1]="jedec"===h?n[1].charAt(0):v>0?n[1].replace(/B$/,""):n[1],e.test(n[1])&&(n[0]=Math.floor(n[0]),n[1]=""))),N&&(n[0]=-n[0]),n[1]=M[n[1]]||n[1],!0===c?n[0]=n[0].toLocaleString():c.length>0?n[0]=n[0].toLocaleString(c,f):u.length>0&&(n[0]=n[0].toString().replace(".",u)),"array"===l?n:(m&&(n[1]=p[v]?p[v]:b[h][v]+(B?"bit":"byte")+(1===n[0]?"":"s")),"object"===l?{value:n[0],symbol:n[1]}:n.join(y)))}return t.partial=e=>i=>t(i,e),t}));
{
"name": "filesize",
"description": "JavaScript library to generate a human readable String describing the file size",
"version": "4.2.1",
"version": "5.0.0",
"homepage": "https://filesizejs.com",

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

"license": "BSD-3-Clause",
"main": "lib/filesize",
"source": "src/filesize.js",
"browser": "lib/filesize.js",
"main": "lib/filesize.cjs.js",
"module": "lib/filesize.esm.js",
"types": "filesize.d.ts",
"engines": {

@@ -21,16 +25,8 @@ "node": ">= 0.4.0"

"scripts": {
"test": "grunt test"
"test": "nodeunit test/filesize_test.js",
"build": "tslib build"
},
"devDependencies": {
"babel-core": "^6.26.3",
"babel-preset-env": "^1.7.0",
"babel-minify": "^0.5.1",
"grunt": "^1.0.4",
"grunt-babel": "^8.0.0",
"grunt-cli": "^1.3.2",
"grunt-contrib-concat": "^1.0.1",
"grunt-contrib-nodeunit": "^2.0.0",
"grunt-contrib-uglify": "^4.0.1",
"grunt-contrib-watch": "^1.1.0",
"grunt-eslint": "^22.0.0"
"tslib-cli": "^5.0.22",
"nodeunit": "^0.11.3"
},

@@ -37,0 +33,0 @@ "keywords": [

@@ -85,9 +85,4 @@ # filesize.js

## 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.
An ES6 version is bundled with an npm install, but requires you load it with the full path, e.g. `require(path.join(__dirname, 'node_modules', 'filesize', 'lib', 'filesize.es6.js'))`.
## License
Copyright (c) 2019 Jason Mulligan
Licensed under the BSD-3 license.
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