@akadenia/helpers 1.8.0 → 1.9.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/README.MD CHANGED
@@ -933,6 +933,26 @@ console.log(FileHelpers.checkFileExtension("script.js", ["js", "ts", "jsx"])) //
933
933
  console.log(FileHelpers.checkFileExtension("file", ["txt", "md"])) // false (no extension)
934
934
  ```
935
935
 
936
+ ### `formatFileSize(bytes)`
937
+
938
+ Formats a file size in bytes to a human-readable string with appropriate units.
939
+
940
+ **Parameters:**
941
+
942
+ - `bytes`: `number | null` - The file size in bytes.
943
+
944
+ **Example:**
945
+
946
+ ```typescript
947
+ console.log(FileHelpers.formatFileSize(1024)) // "1.0 KB"
948
+ console.log(FileHelpers.formatFileSize(1024 * 1024)) // "1.0 MB"
949
+ console.log(FileHelpers.formatFileSize(1024 * 1024 * 1024)) // "1.0 GB"
950
+ console.log(FileHelpers.formatFileSize(512)) // "512 B"
951
+ console.log(FileHelpers.formatFileSize(1536)) // "1.5 KB"
952
+ console.log(FileHelpers.formatFileSize(0)) // "0 B"
953
+ console.log(FileHelpers.formatFileSize(null)) // "0 B"
954
+ ```
955
+
936
956
  ## Contributing
937
957
 
938
958
  We welcome contributions! Please feel free to submit a Pull Request.
package/dist/file.d.ts CHANGED
@@ -8,3 +8,12 @@
8
8
  * checkFileExtension("test.txt", ["md"]) // false
9
9
  */
10
10
  export declare const checkFileExtension: (filePath: string, validExtensions: string[]) => boolean;
11
+ /**
12
+ * Format a file size to a human readable string
13
+ * @param bytes The file size in bytes
14
+ * @returns The formatted file size
15
+ * @example
16
+ * formatFileSize(1024) // "1.0 KB"
17
+ * formatFileSize(1024 * 1024) // "1.0 MB"
18
+ */
19
+ export declare const formatFileSize: (bytes: number | null) => string;
package/dist/file.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.checkFileExtension = void 0;
3
+ exports.formatFileSize = exports.checkFileExtension = void 0;
4
4
  /**
5
5
  * Check if a file path has a valid extension
6
6
  * @param filePath The file path to check
@@ -15,3 +15,21 @@ const checkFileExtension = (filePath, validExtensions) => {
15
15
  return !!fileExtension && validExtensions.includes(fileExtension);
16
16
  };
17
17
  exports.checkFileExtension = checkFileExtension;
18
+ /**
19
+ * Format a file size to a human readable string
20
+ * @param bytes The file size in bytes
21
+ * @returns The formatted file size
22
+ * @example
23
+ * formatFileSize(1024) // "1.0 KB"
24
+ * formatFileSize(1024 * 1024) // "1.0 MB"
25
+ */
26
+ const formatFileSize = (bytes) => {
27
+ if (!bytes || bytes <= 0)
28
+ return "0 B";
29
+ const units = ["B", "KB", "MB", "GB", "TB"];
30
+ const unitIndex = Math.floor(Math.log2(bytes) / 10);
31
+ const clampedIndex = Math.min(unitIndex, units.length - 1);
32
+ const size = bytes / Math.pow(1024, clampedIndex);
33
+ return `${size.toFixed(clampedIndex === 0 ? 0 : 1)} ${units[clampedIndex]}`;
34
+ };
35
+ exports.formatFileSize = formatFileSize;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@akadenia/helpers",
3
- "version": "1.8.0",
3
+ "version": "1.9.0",
4
4
  "description": "Akadenia helpers",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -9,7 +9,7 @@
9
9
  "akadenia"
10
10
  ],
11
11
  "scripts": {
12
- "build": "rm -rf dist && tsc -p tsconfig.json",
12
+ "build": "rimraf dist && tsc -p tsconfig.json",
13
13
  "test": "jest",
14
14
  "format": "prettier --write \"./**/*.{ts,tsx,js,jsx,json,yml}\"",
15
15
  "lint": "prettier --check \"./**/*.{ts,tsx,js,jsx,json,yml}\"",
@@ -42,6 +42,7 @@
42
42
  "jest": "^29.7.0",
43
43
  "jsdoc-to-markdown": "^8.0.3",
44
44
  "prettier": "^3.3.3",
45
+ "rimraf": "^6.0.1",
45
46
  "semantic-release": "22.0.12",
46
47
  "ts-jest": "^29.2.5",
47
48
  "typescript": "^5.6.2"