@rsdoctor/utils 1.5.10 → 1.5.12

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.
@@ -1,58 +1,98 @@
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?: {};
17
- }
18
-
19
- interface FileSizeOptionsArray extends FileSizeOptionsBase {
20
- output: 'array'
21
- }
22
-
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
- }
41
-
42
- type FileSizeReturnArray = [ number, string ]
43
-
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;
54
-
55
- declare function filesize<Options extends FileSizeOptions = undefined>(byteCount: number | string | bigint, options?: Options): FileSizeReturnType<Options>
56
- declare function partial<Options extends FileSizeOptions = undefined>(options?: Options): (byteCount: number | string | bigint) => FileSizeReturnType<Options>
1
+ /**
2
+ * Options interface for configuring filesize behavior
3
+ */
4
+ 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;
37
+ }
38
+
39
+ /**
40
+ * Object format return type when output is "object"
41
+ */
42
+ 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;
51
+ }
52
+
53
+ /**
54
+ * Array format return type when output is "array"
55
+ */
56
+ type FilesizeArray = [number | string, string];
57
+
58
+ /**
59
+ * Return type based on output option
60
+ */
61
+ 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;
66
+
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.02 kB"
75
+ * filesize(1024, {bits: true}) // "8.19 kbit"
76
+ * filesize(1024, {output: "object"}) // {value: 1.02, symbol: "kB", exponent: 1, unit: "kB"}
77
+ */
78
+ declare function filesize<T extends FilesizeOptions = {}>(
79
+ arg: number | string | bigint,
80
+ options?: T
81
+ ): FilesizeReturn<T>;
82
+
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 KiB"
90
+ * formatBytes(2048) // "2 KiB"
91
+ * formatBytes(1536) // "1.5 KiB"
92
+ */
93
+ declare function partial<T extends FilesizeOptions = {}>(
94
+ options?: T
95
+ ): (arg: number | string | bigint) => FilesizeReturn<T>;
57
96
 
58
97
  export { filesize, partial };
98
+ export type { FilesizeArray, FilesizeObject, FilesizeOptions, FilesizeReturn };