-
Notifications
You must be signed in to change notification settings - Fork 4
/
romans.d.ts
118 lines (108 loc) · 3.36 KB
/
romans.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
declare module 'romans' {
/**
* Valid decimal range for Roman numerals (1 to 3999)
* This ensures compile-time checking of valid input ranges
*/
type ValidDecimal = number & {
readonly __brand: 'ValidDecimal';
};
/**
* Valid Roman numeral characters (single characters)
*/
type SingleRomanChar = 'M' | 'D' | 'C' | 'L' | 'X' | 'V' | 'I';
/**
* Valid Roman numeral subtractive combinations
*/
type SubtractiveRomanChar = 'CM' | 'CD' | 'XC' | 'XL' | 'IX' | 'IV';
/**
* All valid Roman numeral components
*/
type RomanChar = SingleRomanChar | SubtractiveRomanChar;
/**
* A branded type representing a valid Roman numeral string.
* This type ensures compile-time safety when working with Roman numerals.
*
* @example
* // Valid Roman numerals
* const valid: RomanNumeral = 'MCMLIV' as RomanNumeral // ✓
* const invalid: RomanNumeral = 'ABC' as RomanNumeral // ✗ (runtime error)
*/
type RomanNumeral = string & {
readonly __brand: 'RomanNumeral';
};
/**
* A mapping of Roman numeral symbols to their decimal values.
* The map is ordered from highest to lowest value to ensure proper conversion.
*/
interface RomanMap {
readonly M: 1000;
readonly CM: 900;
readonly D: 500;
readonly CD: 400;
readonly C: 100;
readonly XC: 90;
readonly L: 50;
readonly XL: 40;
readonly X: 10;
readonly IX: 9;
readonly V: 5;
readonly IV: 4;
readonly I: 1;
}
/**
* An ordered array of valid Roman numeral symbols and combinations.
* Arranged in descending order of their decimal values.
*
* @readonly
*/
const allChars: ReadonlyArray<RomanChar>;
/**
* An ordered array of decimal values corresponding to Roman numerals.
* Arranged in descending order, matching the order of `allChars`.
*
* @readonly
*/
const allNumerals: ReadonlyArray<RomanMap[RomanChar]>;
/**
* Converts a decimal number to its Roman numeral representation.
*
* @param decimal - A positive integer to convert (must be less than 4000)
* @returns A valid Roman numeral string
* @throws {Error} When input is not a positive integer
* @throws {Error} When input is greater than or equal to 4000
*
* @example
* romanize(1994) // Returns 'MCMXCIV'
* romanize(2023) // Returns 'MMXXIII'
* romanize(0) // Throws Error: requires an unsigned integer
* romanize(4000) // Throws Error: requires max value of less than 4000
*/
function romanize(decimal: number): RomanNumeral;
/**
* Converts a Roman numeral string to its decimal representation.
*
* @param romanStr - A string containing valid Roman numerals
* @returns The decimal value of the Roman numeral (1-3999)
* @throws {Error} When input contains invalid Roman numeral characters
* @throws {Error} When input contains valid characters in an invalid sequence
*
* @example
* deromanize('MCMXCIV') // Returns 1994
* deromanize('MMXXIII') // Returns 2023
* deromanize('INVALID') // Throws Error: requires valid roman numeral string
* deromanize('IC') // Throws Error: requires valid roman numeral string
*/
function deromanize(romanStr: RomanNumeral | string): ValidDecimal;
export {
RomanNumeral,
RomanMap,
RomanChar,
SingleRomanChar,
SubtractiveRomanChar,
ValidDecimal,
deromanize,
romanize,
allChars,
allNumerals
};
}