Skip to content

Commit 45a09dc

Browse files
committed
Updating type defs
1 parent 3027df7 commit 45a09dc

File tree

2 files changed

+128
-81
lines changed

2 files changed

+128
-81
lines changed

types/constants.d.ts

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,51 @@
1-
export const ARRAY: "array";
1+
// Error Messages
2+
export const INVALID_NUMBER: "Invalid number";
3+
export const INVALID_ROUND: "Invalid rounding method";
4+
5+
// Standard Types
6+
export const IEC: "iec";
7+
export const JEDEC: "jedec";
8+
export const SI: "si";
9+
10+
// Unit Types
211
export const BIT: "bit";
312
export const BITS: "bits";
413
export const BYTE: "byte";
514
export const BYTES: "bytes";
6-
export const EMPTY: "";
7-
export const EXPONENT: "exponent";
15+
export const SI_KBIT: "kbit";
16+
export const SI_KBYTE: "kB";
17+
18+
// Output Format Types
19+
export const ARRAY: "array";
820
export const FUNCTION: "function";
9-
export const IEC: "iec";
10-
export const INVALID_NUMBER: "Invalid number";
11-
export const INVALID_ROUND: "Invalid rounding method";
12-
export const JEDEC: "jedec";
1321
export const OBJECT: "object";
14-
export const PERIOD: ".";
22+
export const STRING: "string";
23+
24+
// Processing Constants
25+
export const EXPONENT: "exponent";
1526
export const ROUND: "round";
27+
28+
// Special Characters and Values
29+
export const EMPTY: "";
30+
export const PERIOD: ".";
1631
export const S: "s";
17-
export const SI: "si";
18-
export const SI_KBIT: "kbit";
19-
export const SI_KBYTE: "kB";
2032
export const SPACE: " ";
21-
export const STRING: "string";
2233
export const ZERO: "0";
23-
export namespace STRINGS {
24-
namespace symbol {
25-
namespace iec {
26-
const bits: string[];
27-
const bytes: string[];
28-
}
29-
namespace jedec {
30-
const bits_1: string[];
31-
export { bits_1 as bits };
32-
const bytes_1: string[];
33-
export { bytes_1 as bytes };
34-
}
35-
}
36-
namespace fullform {
37-
const iec_1: string[];
38-
export { iec_1 as iec };
39-
const jedec_1: string[];
40-
export { jedec_1 as jedec };
41-
}
42-
}
34+
35+
// Data Structures
36+
export const STRINGS: {
37+
symbol: {
38+
iec: {
39+
bits: ["bit", "Kibit", "Mibit", "Gibit", "Tibit", "Pibit", "Eibit", "Zibit", "Yibit"];
40+
bytes: ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];
41+
};
42+
jedec: {
43+
bits: ["bit", "Kbit", "Mbit", "Gbit", "Tbit", "Pbit", "Ebit", "Zbit", "Ybit"];
44+
bytes: ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
45+
};
46+
};
47+
fullform: {
48+
iec: ["", "kibi", "mebi", "gibi", "tebi", "pebi", "exbi", "zebi", "yobi"];
49+
jedec: ["", "kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta"];
50+
};
51+
};

types/filesize.d.ts

Lines changed: 87 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,94 @@
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;
1737
}
1838

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;
2151
}
2252

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];
4157

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;
4366

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>;
5482

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

Comments
 (0)