@massimo-cassandro/minimo 1.14.6 → 1.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -11,6 +11,7 @@ export { escapeHTML, unescapeHTML } from './src/utilities/escape-html.js';
11
11
  export { generatePwd } from './src/utilities/generate-pwd.js';
12
12
  export { getCookie, setCookie } from './src/utilities/cookies.js';
13
13
  export { linkedDates } from './src/utilities/linked-dates.js';
14
+ export { parseFileSize } from './src/utilities/parse-file-size.js';
14
15
  export { randomId } from './src/utilities/random-id.js';
15
16
  export { relativeDate } from './src/utilities/relative-date.js';
16
17
  export { sentenceCase } from './src/utilities/sentence-case.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@massimo-cassandro/minimo",
3
- "version": "1.14.6",
3
+ "version": "1.15.0",
4
4
  "description": "Light css framework & utilities",
5
5
  "type": "module",
6
6
  "main": "./index.js",
@@ -0,0 +1,32 @@
1
+ // @ts-check
2
+
3
+ /**
4
+ * Converts a file size in bytes to a human readable string,
5
+ * using the most appropriate unit (bytes, KB, MB or GB).
6
+ *
7
+ * @example
8
+ * parseFileSize(1536); // "1.5 KB"
9
+ * parseFileSize(10485760); // "10 MB"
10
+ *
11
+ * @param {number} bytes - file size in bytes
12
+ * @param {number} [decimals=1] - max number of decimal digits
13
+ * @returns {string} formatted size (e.g. "1.5 MB")
14
+ */
15
+ export function parseFileSize(bytes, decimals = 1) {
16
+
17
+ if(typeof bytes !== 'number' || isNaN(bytes) || bytes < 0) {
18
+ return '';
19
+ }
20
+
21
+ const units = ['bytes', 'KB', 'MB', 'GB'],
22
+ k = 1024;
23
+
24
+ const exp = bytes === 0
25
+ ? 0
26
+ : Math.min(Math.floor(Math.log(bytes) / Math.log(k)), units.length - 1);
27
+
28
+ const value = bytes / Math.pow(k, exp);
29
+
30
+ // \u202f = narrow no-break space
31
+ return `${parseFloat(value.toFixed(decimals))}\u202f${units[exp]}`;
32
+ }