Skip to content

Commit 00ff1ae

Browse files
committed
Building with revision dependencies
1 parent 3b40756 commit 00ff1ae

24 files changed

+2480
-12279
lines changed

.eslintrc

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
"es6": true,
66
"amd": true
77
},
8+
"parserOptions": {
9+
"ecmaVersion": 2020,
10+
"sourceType": "module"
11+
},
12+
"globals": {
13+
"describe": "readonly",
14+
"it": "readonly"
15+
},
816
"rules": {
917
"arrow-parens": [2, "as-needed"],
1018
"arrow-spacing": [2, {"before": true, "after": true}],
@@ -162,4 +170,4 @@
162170
"wrap-regex": [2],
163171
"yoda": [2, "never", { "exceptRange": true }]
164172
}
165-
}
173+
}

.travis.yml

-10
This file was deleted.

README.md

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# filesize.js
22

3-
[![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)
4-
53
filesize.js provides a simple way to get a human readable file size string from a number (float or integer) or string.
64

5+
```javascript
6+
import {filesize} from "filesize";
7+
filesize(265318, {base: 2, standard: "jedec"}); // "259.1 KB"
8+
```
9+
710
## Optional settings
811

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

5962

6063
## Partial Application
61-
`filesize.partial()` takes the second parameter of `filesize()` and returns a new function with the configuration applied
64+
`partial()` takes the second parameter of `filesize()` and returns a new function with the configuration applied
6265
upon execution. This can be used to reduce `Object` creation if you call `filesize()` without caching the `descriptor`
6366
in lexical scope.
6467

6568
```javascript
66-
const size = filesize.partial({base: 2, standard: "jedec"});
69+
import {partial} from "filesize";
70+
const size = partial({base: 2, standard: "jedec"});
6771

6872
size(265318); // "259.1 KB"
6973
```
7074

71-
## How can I load filesize.js?
72-
filesize.js supports AMD loaders (require.js, curl.js, etc.), node.js & npm (```npm install filesize```), or using a script tag.
73-
74-
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'))`.
75-
7675
## License
7776
Copyright (c) 2022 Jason Mulligan
7877
Licensed under the BSD-3 license.

dist/filesize.cjs

+203
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
/**
2+
* filesize
3+
*
4+
* @copyright 2022 Jason Mulligan <[email protected]>
5+
* @license BSD-3-Clause
6+
* @version 10.0.0
7+
*/
8+
'use strict';
9+
10+
Object.defineProperty(exports, '__esModule', { value: true });
11+
12+
const ARRAY = "array";
13+
const BIT = "bit";
14+
const BITS = "bits";
15+
const BYTE = "byte";
16+
const BYTES = "bytes";
17+
const EMPTY = "";
18+
const EXPONENT = "exponent";
19+
const FUNCTION = "function";
20+
const IEC = "iec";
21+
const INVALID_NUMBER = "Invalid number";
22+
const INVALID_ROUND = "Invalid rounding method";
23+
const JEDEC = "jedec";
24+
const OBJECT = "object";
25+
const PERIOD = ".";
26+
const ROUND = "round";
27+
const S = "s";
28+
const SI_KBIT = "kbit";
29+
const SI_KBYTE = "kB";
30+
const SPACE = " ";
31+
const STRING = "string";
32+
const ZERO = "0";
33+
const STRINGS = {
34+
symbol: {
35+
iec: {
36+
bits: ["bit", "Kibit", "Mibit", "Gibit", "Tibit", "Pibit", "Eibit", "Zibit", "Yibit"],
37+
bytes: ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]
38+
},
39+
jedec: {
40+
bits: ["bit", "Kbit", "Mbit", "Gbit", "Tbit", "Pbit", "Ebit", "Zbit", "Ybit"],
41+
bytes: ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
42+
}
43+
},
44+
fullform: {
45+
iec: ["", "kibi", "mebi", "gibi", "tebi", "pebi", "exbi", "zebi", "yobi"],
46+
jedec: ["", "kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta"]
47+
}
48+
};
49+
50+
function filesize (arg, {
51+
bits = false,
52+
pad = false,
53+
base = -1,
54+
round = 2,
55+
locale = EMPTY,
56+
localeOptions = {},
57+
separator = EMPTY,
58+
spacer = SPACE,
59+
symbols = {},
60+
standard = EMPTY,
61+
output = STRING,
62+
fullform = false,
63+
fullforms = [],
64+
exponent = -1,
65+
roundingMethod = ROUND,
66+
precision = 0
67+
} = {}) {
68+
let e = exponent,
69+
num = Number(arg),
70+
result = [],
71+
val = 0,
72+
u = EMPTY;
73+
74+
// Sync base & standard
75+
if (base === -1 && standard.length === 0) {
76+
base = 10;
77+
standard = JEDEC;
78+
} else if (base === -1 && standard.length > 0) {
79+
standard = standard === IEC ? IEC : JEDEC;
80+
base = standard === IEC ? 2 : 10;
81+
} else {
82+
base = base === 2 ? 2 : 10;
83+
standard = base === 10 ? JEDEC : standard === JEDEC ? JEDEC : IEC;
84+
}
85+
86+
const ceil = base === 10 ? 1000 : 1024,
87+
full = fullform === true,
88+
neg = num < 0,
89+
roundingFunc = Math[roundingMethod];
90+
91+
if (isNaN(arg)) {
92+
throw new TypeError(INVALID_NUMBER);
93+
}
94+
95+
if (typeof roundingFunc !== FUNCTION) {
96+
throw new TypeError(INVALID_ROUND);
97+
}
98+
99+
// Flipping a negative number to determine the size
100+
if (neg) {
101+
num = -num;
102+
}
103+
104+
// Determining the exponent
105+
if (e === -1 || isNaN(e)) {
106+
e = Math.floor(Math.log(num) / Math.log(ceil));
107+
108+
if (e < 0) {
109+
e = 0;
110+
}
111+
}
112+
113+
// Exceeding supported length, time to reduce & multiply
114+
if (e > 8) {
115+
if (precision > 0) {
116+
precision += 8 - e;
117+
}
118+
119+
e = 8;
120+
}
121+
122+
if (output === EXPONENT) {
123+
return e;
124+
}
125+
126+
// Zero is now a special case because bytes divide by 1
127+
if (num === 0) {
128+
result[0] = 0;
129+
u = result[1] = STRINGS.symbol[standard][bits ? BITS : BYTES][e];
130+
} else {
131+
val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));
132+
133+
if (bits) {
134+
val = val * 8;
135+
136+
if (val >= ceil && e < 8) {
137+
val = val / ceil;
138+
e++;
139+
}
140+
}
141+
142+
const p = Math.pow(10, e > 0 ? round : 0);
143+
result[0] = roundingFunc(val * p) / p;
144+
145+
if (result[0] === ceil && e < 8 && exponent === -1) {
146+
result[0] = 1;
147+
e++;
148+
}
149+
150+
u = result[1] = base === 10 && e === 1 ? bits ? SI_KBIT : SI_KBYTE : STRINGS.symbol[standard][bits ? BITS : BYTES][e];
151+
}
152+
153+
// Decorating a 'diff'
154+
if (neg) {
155+
result[0] = -result[0];
156+
}
157+
158+
// Setting optional precision
159+
if (precision > 0) {
160+
result[0] = result[0].toPrecision(precision);
161+
}
162+
163+
// Applying custom symbol
164+
result[1] = symbols[result[1]] || result[1];
165+
166+
if (locale === true) {
167+
result[0] = result[0].toLocaleString();
168+
} else if (locale.length > 0) {
169+
result[0] = result[0].toLocaleString(locale, localeOptions);
170+
} else if (separator.length > 0) {
171+
result[0] = result[0].toString().replace(PERIOD, separator);
172+
}
173+
174+
if (pad && Number.isInteger(result[0]) === false && round > 0) {
175+
const x = separator || PERIOD,
176+
tmp = result[0].toString().split(x),
177+
s = tmp[1] || EMPTY,
178+
l = s.length,
179+
n = round - l;
180+
181+
result[0] = `${tmp[0]}${x}${s.padEnd(l + n, ZERO)}`;
182+
}
183+
184+
if (full) {
185+
result[1] = fullforms[e] ? fullforms[e] : STRINGS.fullform[standard][e] + (bits ? BIT : BYTE) + (result[0] === 1 ? EMPTY : S);
186+
}
187+
188+
// Returning Array, Object, or String (default)
189+
return output === ARRAY ? result : output === OBJECT ? {
190+
value: result[0],
191+
symbol: result[1],
192+
exponent: e,
193+
unit: u
194+
} : result.join(spacer);
195+
}
196+
197+
// Partial application for functional programming
198+
function partial (opt) {
199+
return arg => filesize(arg, opt);
200+
}
201+
202+
exports.filesize = filesize;
203+
exports.partial = partial;

lib/filesize.esm.js dist/filesize.esm.js

+9-20
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* @copyright 2022 Jason Mulligan <[email protected]>
55
* @license BSD-3-Clause
6-
* @version 9.0.11
6+
* @version 10.0.0
77
*/
88
const ARRAY = "array";
99
const BIT = "bit";
@@ -26,8 +26,7 @@ const SI_KBYTE = "kB";
2626
const SPACE = " ";
2727
const STRING = "string";
2828
const ZERO = "0";
29-
30-
const strings = {
29+
const STRINGS = {
3130
symbol: {
3231
iec: {
3332
bits: ["bit", "Kibit", "Mibit", "Gibit", "Tibit", "Pibit", "Eibit", "Zibit", "Yibit"],
@@ -42,17 +41,7 @@ const strings = {
4241
iec: ["", "kibi", "mebi", "gibi", "tebi", "pebi", "exbi", "zebi", "yobi"],
4342
jedec: ["", "kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta"]
4443
}
45-
};
46-
47-
/**
48-
* filesize
49-
*
50-
* @method filesize
51-
* @param {Mixed} arg String, Int or Float to transform
52-
* @param {Object} descriptor [Optional] Flags
53-
* @return {String} Readable file size String
54-
*/
55-
function filesize (arg, {
44+
};function filesize (arg, {
5645
bits = false,
5746
pad = false,
5847
base = -1,
@@ -131,7 +120,7 @@ function filesize (arg, {
131120
// Zero is now a special case because bytes divide by 1
132121
if (num === 0) {
133122
result[0] = 0;
134-
u = result[1] = strings.symbol[standard][bits ? BITS : BYTES][e];
123+
u = result[1] = STRINGS.symbol[standard][bits ? BITS : BYTES][e];
135124
} else {
136125
val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));
137126

@@ -152,7 +141,7 @@ function filesize (arg, {
152141
e++;
153142
}
154143

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

158147
// Decorating a 'diff'
@@ -187,7 +176,7 @@ function filesize (arg, {
187176
}
188177

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

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

202191
// Partial application for functional programming
203-
filesize.partial = opt => arg => filesize(arg, opt);
204-
205-
export { filesize as default };
192+
function partial (opt) {
193+
return arg => filesize(arg, opt);
194+
}export{filesize,partial};

lib/filesize.esm.min.js dist/filesize.esm.min.js

+2-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)