1
- interface FileSizeOptionsBase {
2
- base ?: 10 | 2 ;
3
- bits ?: boolean ;
4
- exponent ?: number ;
5
- fullform ?: boolean ;
6
- fullforms ?: string [ ] ;
7
- locale ?: string | boolean ;
8
- localeOptions ?: Intl . DateTimeFormatOptions ;
9
- pad ?: boolean ;
10
- precision ?: number ;
11
- round ?: number ;
12
- roundingMethod ?: 'round' | 'floor' | 'ceil' ;
13
- separator ?: string ;
14
- spacer ?: string ;
15
- standard ?: 'si' | 'iec' | 'jedec' ;
16
- symbols ?: { } ;
1
+ /**
2
+ * Options interface for configuring filesize behavior
3
+ */
4
+ export interface FilesizeOptions {
5
+ /** If true, calculates bits instead of bytes */
6
+ bits ?: boolean ;
7
+ /** If true, pads decimal places to match round parameter */
8
+ pad ?: boolean ;
9
+ /** Number base (2 for binary, 10 for decimal, -1 for auto) */
10
+ base ?: number ;
11
+ /** Number of decimal places to round to */
12
+ round ?: number ;
13
+ /** Locale for number formatting, true for system locale */
14
+ locale ?: string | boolean ;
15
+ /** Additional options for locale formatting */
16
+ localeOptions ?: Intl . NumberFormatOptions ;
17
+ /** Custom decimal separator */
18
+ separator ?: string ;
19
+ /** String to separate value and unit */
20
+ spacer ?: string ;
21
+ /** Custom unit symbols */
22
+ symbols ?: Record < string , string > ;
23
+ /** Unit standard to use (SI, IEC, JEDEC) */
24
+ standard ?: "si" | "iec" | "jedec" | "" ;
25
+ /** Output format: "string", "array", "object", or "exponent" */
26
+ output ?: "string" | "array" | "object" | "exponent" ;
27
+ /** If true, uses full unit names instead of abbreviations */
28
+ fullform ?: boolean ;
29
+ /** Custom full unit names */
30
+ fullforms ?: string [ ] ;
31
+ /** Force specific exponent (-1 for auto) */
32
+ exponent ?: number ;
33
+ /** Math rounding method to use */
34
+ roundingMethod ?: "round" | "floor" | "ceil" ;
35
+ /** Number of significant digits (0 for auto) */
36
+ precision ?: number ;
17
37
}
18
38
19
- interface FileSizeOptionsArray extends FileSizeOptionsBase {
20
- output : 'array'
39
+ /**
40
+ * Object format return type when output is "object"
41
+ */
42
+ export interface FilesizeObject {
43
+ /** The numeric value */
44
+ value : number | string ;
45
+ /** The unit symbol */
46
+ symbol : string ;
47
+ /** The exponent used in calculation */
48
+ exponent : number ;
49
+ /** The original unit before symbol customization */
50
+ unit : string ;
21
51
}
22
52
23
- interface FileSizeOptionsExponent extends FileSizeOptionsBase {
24
- output : 'exponent'
25
- }
26
-
27
- interface FileSizeOptionsObject extends FileSizeOptionsBase {
28
- output : 'object'
29
- }
30
-
31
- interface FileSizeOptionsString extends FileSizeOptionsBase {
32
- output : 'string'
33
- }
34
-
35
- interface FileSizeReturnObject {
36
- value : string ,
37
- symbol : string ,
38
- exponent : number ,
39
- unit : string ,
40
- }
53
+ /**
54
+ * Array format return type when output is "array"
55
+ */
56
+ export type FilesizeArray = [ number | string , string ] ;
41
57
42
- type FileSizeReturnArray = [ number , string ]
58
+ /**
59
+ * Return type based on output option
60
+ */
61
+ export type FilesizeReturn < T extends FilesizeOptions = { } > =
62
+ T [ 'output' ] extends "object" ? FilesizeObject :
63
+ T [ 'output' ] extends "array" ? FilesizeArray :
64
+ T [ 'output' ] extends "exponent" ? number :
65
+ string ;
43
66
44
- type FileSizeOptionStringOrBase = FileSizeOptionsString | FileSizeOptionsBase ;
45
- type FileSizeOptions = FileSizeOptionsArray | FileSizeOptionsExponent | FileSizeOptionsObject | FileSizeOptionStringOrBase | undefined
46
- type FileSizeReturnType < Options extends FileSizeOptions > =
47
- Options extends FileSizeOptionsArray
48
- ? FileSizeReturnArray
49
- : Options extends FileSizeOptionsExponent
50
- ? number
51
- : Options extends FileSizeOptionsObject
52
- ? FileSizeReturnObject
53
- : string ;
67
+ /**
68
+ * Converts a file size in bytes to a human-readable string with appropriate units
69
+ * @param arg - The file size in bytes to convert
70
+ * @param options - Configuration options for formatting
71
+ * @returns Formatted file size based on output option
72
+ * @throws {TypeError } When arg is not a valid number or roundingMethod is invalid
73
+ * @example
74
+ * filesize(1024) // "1 KB"
75
+ * filesize(1024, {bits: true}) // "8 Kb"
76
+ * filesize(1024, {output: "object"}) // {value: 1, symbol: "KB", exponent: 1, unit: "KB"}
77
+ */
78
+ export function filesize < T extends FilesizeOptions = { } > (
79
+ arg : number | bigint ,
80
+ options ?: T
81
+ ) : FilesizeReturn < T > ;
54
82
55
- export function filesize < Options extends FileSizeOptions = undefined > ( byteCount : number | string | bigint , options ?: Options ) : FileSizeReturnType < Options >
56
- export function partial < Options extends FileSizeOptions = undefined > ( options ?: Options ) : ( byteCount : number | string | bigint ) => FileSizeReturnType < Options >
83
+ /**
84
+ * Creates a partially applied version of filesize with preset options
85
+ * @param options - Default options to apply to the returned function
86
+ * @returns A function that takes a file size and returns formatted output
87
+ * @example
88
+ * const formatBytes = partial({round: 1, standard: "iec"});
89
+ * formatBytes(1024) // "1.0 KiB"
90
+ * formatBytes(2048) // "2.0 KiB"
91
+ */
92
+ export function partial < T extends FilesizeOptions = { } > (
93
+ options ?: T
94
+ ) : ( arg : number | bigint ) => FilesizeReturn < T > ;
0 commit comments