Skip to content

Commit 4f6a402

Browse files
authored
Taking a mulligan on 'precision' (#203)
* Fixing the output when supplying 'precision' (#201) * Fixing the output when supplying 'precision' * Changing github actions to build the earliest supported version and the current * Changing github actions to build the current * Version bump * Fixing how 'precision' adjusts the exponent if there's digits (#202) * Version bump * Taking a mulligan on this fix * Fixing tests to use 'src' * Simplifying the new logic * Minor tweaks * Updating tests
1 parent 5deff97 commit 4f6a402

15 files changed

+288
-43
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212

1313
strategy:
1414
matrix:
15-
node-version: [20.x, 22.x]
15+
node-version: [24.x]
1616

1717
steps:
1818
- name: Checkout Repository

CHANGELOG.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,34 @@ All notable changes to this project will be documented in this file. Dates are d
44

55
Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
66

7+
#### [11.0.4](https://github.com/avoidwork/filesize.js/compare/11.0.3...11.0.4)
8+
9+
- Fixing how 'precision' adjusts the exponent if there's digits [`#202`](https://github.com/avoidwork/filesize.js/pull/202)
10+
11+
#### [11.0.3](https://github.com/avoidwork/filesize.js/compare/11.0.2...11.0.3)
12+
13+
> 19 September 2025
14+
15+
- Fixing the output when supplying 'precision' [`#201`](https://github.com/avoidwork/filesize.js/pull/201)
16+
- Bump rollup from 4.50.1 to 4.50.2 [`#199`](https://github.com/avoidwork/filesize.js/pull/199)
17+
- Bump actions/setup-node from 4 to 5 [`#198`](https://github.com/avoidwork/filesize.js/pull/198)
18+
- Bump rollup from 4.50.0 to 4.50.1 [`#197`](https://github.com/avoidwork/filesize.js/pull/197)
19+
- Bump eslint from 9.34.0 to 9.35.0 [`#196`](https://github.com/avoidwork/filesize.js/pull/196)
20+
- Bump mocha from 11.7.1 to 11.7.2 [`#195`](https://github.com/avoidwork/filesize.js/pull/195)
21+
- Bump rollup from 4.49.0 to 4.50.0 [`#194`](https://github.com/avoidwork/filesize.js/pull/194)
22+
- Bump rollup from 4.44.2 to 4.49.0 [`#193`](https://github.com/avoidwork/filesize.js/pull/193)
23+
- Bump eslint from 9.30.1 to 9.34.0 [`#192`](https://github.com/avoidwork/filesize.js/pull/192)
24+
- Bump actions/checkout from 4 to 5 [`#191`](https://github.com/avoidwork/filesize.js/pull/191)
25+
- Updating tests [`017e5b1`](https://github.com/avoidwork/filesize.js/commit/017e5b1fe328b0624f067d5790f55c6694d95a78)
26+
- Adding 'Mathematical Foundation' section to 'TECHNICAL_DOCUMENTATION.md' [`ec20636`](https://github.com/avoidwork/filesize.js/commit/ec206366285c6c6b4c29ccc87f6e5450ca128c79)
27+
- Updating documentation [`ef70bf8`](https://github.com/avoidwork/filesize.js/commit/ef70bf8e7889e7f86bee32841ad0ec48faadc82a)
28+
729
#### [11.0.2](https://github.com/avoidwork/filesize.js/compare/11.0.1...11.0.2)
830

31+
> 16 July 2025
32+
933
- Returning 'bigint' to 'filesize.d.ts' & fixing docblock [`#190`](https://github.com/avoidwork/filesize.js/pull/190)
34+
- Updating CHANGELOG.md [`d9909d9`](https://github.com/avoidwork/filesize.js/commit/d9909d94113244c21f636ab6e049a56bf39be3e1)
1035

1136
#### [11.0.1](https://github.com/avoidwork/filesize.js/compare/11.0.0...11.0.1)
1237

dist/filesize.cjs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* @copyright 2025 Jason Mulligan <[email protected]>
55
* @license BSD-3-Clause
6-
* @version 11.0.2
6+
* @version 11.0.4
77
*/
88
'use strict';
99

@@ -35,6 +35,7 @@ const EXPONENT = "exponent";
3535
const ROUND = "round";
3636

3737
// Special Characters and Values
38+
const E = "e";
3839
const EMPTY = "";
3940
const PERIOD = ".";
4041
const S = "s";
@@ -166,9 +167,15 @@ function filesize (arg, {
166167
// Zero is now a special case because bytes divide by 1
167168
if (num === 0) {
168169
result[0] = 0;
170+
171+
if (precision > 0) {
172+
result[0] = result[0].toPrecision(precision);
173+
}
174+
169175
u = result[1] = STRINGS.symbol[standard][bits ? BITS : BYTES][e];
170176
} else {
171-
val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));
177+
let d = base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e);
178+
val = num / d;
172179

173180
if (bits) {
174181
val = val * 8;
@@ -179,14 +186,26 @@ function filesize (arg, {
179186
}
180187
}
181188

182-
const p = Math.pow(10, e > 0 ? round : 0);
189+
let p = Math.pow(10, e > 0 ? round : 0);
183190
result[0] = roundingFunc(val * p) / p;
184191

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

197+
// Setting optional precision
198+
if (precision > 0) {
199+
result[0] = result[0].toPrecision(precision);
200+
201+
if (result[0].includes(E)) {
202+
e++;
203+
d = base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e);
204+
val = num / d;
205+
result[0] = roundingFunc(val * p) / p;
206+
}
207+
}
208+
190209
u = result[1] = base === 10 && e === 1 ? bits ? SI_KBIT : SI_KBYTE : STRINGS.symbol[standard][bits ? BITS : BYTES][e];
191210
}
192211

@@ -195,11 +214,6 @@ function filesize (arg, {
195214
result[0] = -result[0];
196215
}
197216

198-
// Setting optional precision
199-
if (precision > 0) {
200-
result[0] = result[0].toPrecision(precision);
201-
}
202-
203217
// Applying custom symbol
204218
result[1] = symbols[result[1]] || result[1];
205219

dist/filesize.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* @copyright 2025 Jason Mulligan <[email protected]>
55
* @license BSD-3-Clause
6-
* @version 11.0.2
6+
* @version 11.0.4
77
*/
88
// Error Messages
99
const INVALID_NUMBER = "Invalid number";
@@ -33,6 +33,7 @@ const EXPONENT = "exponent";
3333
const ROUND = "round";
3434

3535
// Special Characters and Values
36+
const E = "e";
3637
const EMPTY = "";
3738
const PERIOD = ".";
3839
const S = "s";
@@ -162,9 +163,15 @@ function filesize (arg, {
162163
// Zero is now a special case because bytes divide by 1
163164
if (num === 0) {
164165
result[0] = 0;
166+
167+
if (precision > 0) {
168+
result[0] = result[0].toPrecision(precision);
169+
}
170+
165171
u = result[1] = STRINGS.symbol[standard][bits ? BITS : BYTES][e];
166172
} else {
167-
val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));
173+
let d = base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e);
174+
val = num / d;
168175

169176
if (bits) {
170177
val = val * 8;
@@ -175,14 +182,26 @@ function filesize (arg, {
175182
}
176183
}
177184

178-
const p = Math.pow(10, e > 0 ? round : 0);
185+
let p = Math.pow(10, e > 0 ? round : 0);
179186
result[0] = roundingFunc(val * p) / p;
180187

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

193+
// Setting optional precision
194+
if (precision > 0) {
195+
result[0] = result[0].toPrecision(precision);
196+
197+
if (result[0].includes(E)) {
198+
e++;
199+
d = base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e);
200+
val = num / d;
201+
result[0] = roundingFunc(val * p) / p;
202+
}
203+
}
204+
186205
u = result[1] = base === 10 && e === 1 ? bits ? SI_KBIT : SI_KBYTE : STRINGS.symbol[standard][bits ? BITS : BYTES][e];
187206
}
188207

@@ -191,11 +210,6 @@ function filesize (arg, {
191210
result[0] = -result[0];
192211
}
193212

194-
// Setting optional precision
195-
if (precision > 0) {
196-
result[0] = result[0].toPrecision(precision);
197-
}
198-
199213
// Applying custom symbol
200214
result[1] = symbols[result[1]] || result[1];
201215

dist/filesize.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)