Skip to content

Commit

Permalink
Building with revision dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Sep 28, 2022
1 parent 3b40756 commit 00ff1ae
Show file tree
Hide file tree
Showing 24 changed files with 2,480 additions and 12,279 deletions.
10 changes: 9 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
"es6": true,
"amd": true
},
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module"
},
"globals": {
"describe": "readonly",
"it": "readonly"
},
"rules": {
"arrow-parens": [2, "as-needed"],
"arrow-spacing": [2, {"before": true, "after": true}],
Expand Down Expand Up @@ -162,4 +170,4 @@
"wrap-regex": [2],
"yoda": [2, "never", { "exceptRange": true }]
}
}
}
10 changes: 0 additions & 10 deletions .travis.yml

This file was deleted.

17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# filesize.js

[![build status](https://secure.travis-ci.org/avoidwork/filesize.js.svg)](http://travis-ci.org/avoidwork/filesize.js) [![downloads](https://img.shields.io/npm/dt/filesize.svg)](https://www.npmjs.com/package/filesize) [![CDNJS version](https://img.shields.io/cdnjs/v/filesize.svg)](https://cdnjs.com/libraries/filesize)

filesize.js provides a simple way to get a human readable file size string from a number (float or integer) or string.

```javascript
import {filesize} from "filesize";
filesize(265318, {base: 2, standard: "jedec"}); // "259.1 KB"
```

## Optional settings

`filesize()` accepts an optional descriptor Object as a second argument, so you can customize the output.
Expand Down Expand Up @@ -58,21 +61,17 @@ _*(object)*_ Dictionary of IEC/JEDEC symbols to replace for localization, defaul


## Partial Application
`filesize.partial()` takes the second parameter of `filesize()` and returns a new function with the configuration applied
`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({base: 2, standard: "jedec"});
import {partial} from "filesize";
const size = partial({base: 2, standard: "jedec"});

size(265318); // "259.1 KB"
```

## 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) 2022 Jason Mulligan
Licensed under the BSD-3 license.
203 changes: 203 additions & 0 deletions dist/filesize.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
/**
* filesize
*
* @copyright 2022 Jason Mulligan <jason.mulligan@avoidwork.com>
* @license BSD-3-Clause
* @version 10.0.0
*/
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const ARRAY = "array";
const BIT = "bit";
const BITS = "bits";
const BYTE = "byte";
const BYTES = "bytes";
const EMPTY = "";
const EXPONENT = "exponent";
const FUNCTION = "function";
const IEC = "iec";
const INVALID_NUMBER = "Invalid number";
const INVALID_ROUND = "Invalid rounding method";
const JEDEC = "jedec";
const OBJECT = "object";
const PERIOD = ".";
const ROUND = "round";
const S = "s";
const SI_KBIT = "kbit";
const SI_KBYTE = "kB";
const SPACE = " ";
const STRING = "string";
const ZERO = "0";
const STRINGS = {
symbol: {
iec: {
bits: ["bit", "Kibit", "Mibit", "Gibit", "Tibit", "Pibit", "Eibit", "Zibit", "Yibit"],
bytes: ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]
},
jedec: {
bits: ["bit", "Kbit", "Mbit", "Gbit", "Tbit", "Pbit", "Ebit", "Zbit", "Ybit"],
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"]
}
};

function filesize (arg, {
bits = false,
pad = false,
base = -1,
round = 2,
locale = EMPTY,
localeOptions = {},
separator = EMPTY,
spacer = SPACE,
symbols = {},
standard = EMPTY,
output = STRING,
fullform = false,
fullforms = [],
exponent = -1,
roundingMethod = ROUND,
precision = 0
} = {}) {
let e = exponent,
num = Number(arg),
result = [],
val = 0,
u = EMPTY;

// Sync base & standard
if (base === -1 && standard.length === 0) {
base = 10;
standard = JEDEC;
} else if (base === -1 && standard.length > 0) {
standard = standard === IEC ? IEC : JEDEC;
base = standard === IEC ? 2 : 10;
} else {
base = base === 2 ? 2 : 10;
standard = base === 10 ? JEDEC : standard === JEDEC ? JEDEC : IEC;
}

const ceil = base === 10 ? 1000 : 1024,
full = fullform === true,
neg = num < 0,
roundingFunc = Math[roundingMethod];

if (isNaN(arg)) {
throw new TypeError(INVALID_NUMBER);
}

if (typeof roundingFunc !== FUNCTION) {
throw new TypeError(INVALID_ROUND);
}

// 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) {
if (precision > 0) {
precision += 8 - e;
}

e = 8;
}

if (output === EXPONENT) {
return e;
}

// Zero is now a special case because bytes divide by 1
if (num === 0) {
result[0] = 0;
u = result[1] = STRINGS.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++;
}
}

const p = Math.pow(10, e > 0 ? round : 0);
result[0] = roundingFunc(val * p) / p;

if (result[0] === ceil && e < 8 && exponent === -1) {
result[0] = 1;
e++;
}

u = result[1] = base === 10 && e === 1 ? bits ? SI_KBIT : SI_KBYTE : STRINGS.symbol[standard][bits ? BITS : BYTES][e];
}

// Decorating a 'diff'
if (neg) {
result[0] = -result[0];
}

// Setting optional precision
if (precision > 0) {
result[0] = result[0].toPrecision(precision);
}

// 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(PERIOD, separator);
}

if (pad && Number.isInteger(result[0]) === false && round > 0) {
const x = separator || PERIOD,
tmp = result[0].toString().split(x),
s = tmp[1] || EMPTY,
l = s.length,
n = round - l;

result[0] = `${tmp[0]}${x}${s.padEnd(l + n, ZERO)}`;
}

if (full) {
result[1] = fullforms[e] ? fullforms[e] : STRINGS.fullform[standard][e] + (bits ? BIT : BYTE) + (result[0] === 1 ? EMPTY : S);
}

// Returning Array, Object, or String (default)
return output === ARRAY ? result : output === OBJECT ? {
value: result[0],
symbol: result[1],
exponent: e,
unit: u
} : result.join(spacer);
}

// Partial application for functional programming
function partial (opt) {
return arg => filesize(arg, opt);
}

exports.filesize = filesize;
exports.partial = partial;
29 changes: 9 additions & 20 deletions lib/filesize.esm.js → dist/filesize.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* @copyright 2022 Jason Mulligan <jason.mulligan@avoidwork.com>
* @license BSD-3-Clause
* @version 9.0.11
* @version 10.0.0
*/
const ARRAY = "array";
const BIT = "bit";
Expand All @@ -26,8 +26,7 @@ const SI_KBYTE = "kB";
const SPACE = " ";
const STRING = "string";
const ZERO = "0";

const strings = {
const STRINGS = {
symbol: {
iec: {
bits: ["bit", "Kibit", "Mibit", "Gibit", "Tibit", "Pibit", "Eibit", "Zibit", "Yibit"],
Expand All @@ -42,17 +41,7 @@ const strings = {
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, {
};function filesize (arg, {
bits = false,
pad = false,
base = -1,
Expand Down Expand Up @@ -131,7 +120,7 @@ function filesize (arg, {
// Zero is now a special case because bytes divide by 1
if (num === 0) {
result[0] = 0;
u = result[1] = strings.symbol[standard][bits ? BITS : BYTES][e];
u = result[1] = STRINGS.symbol[standard][bits ? BITS : BYTES][e];
} else {
val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));

Expand All @@ -152,7 +141,7 @@ function filesize (arg, {
e++;
}

u = result[1] = base === 10 && e === 1 ? bits ? SI_KBIT : SI_KBYTE : strings.symbol[standard][bits ? BITS : BYTES][e];
u = result[1] = base === 10 && e === 1 ? bits ? SI_KBIT : SI_KBYTE : STRINGS.symbol[standard][bits ? BITS : BYTES][e];
}

// Decorating a 'diff'
Expand Down Expand Up @@ -187,7 +176,7 @@ function filesize (arg, {
}

if (full) {
result[1] = fullforms[e] ? fullforms[e] : strings.fullform[standard][e] + (bits ? BIT : BYTE) + (result[0] === 1 ? EMPTY : S);
result[1] = fullforms[e] ? fullforms[e] : STRINGS.fullform[standard][e] + (bits ? BIT : BYTE) + (result[0] === 1 ? EMPTY : S);
}

// Returning Array, Object, or String (default)
Expand All @@ -200,6 +189,6 @@ function filesize (arg, {
}

// Partial application for functional programming
filesize.partial = opt => arg => filesize(arg, opt);

export { filesize as default };
function partial (opt) {
return arg => filesize(arg, opt);
}export{filesize,partial};
5 changes: 2 additions & 3 deletions lib/filesize.esm.min.js → dist/filesize.esm.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 00ff1ae

Please sign in to comment.