forked from shmuelie/Humanizer.Js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
romanNumeralExtensions.ts
120 lines (102 loc) · 2.73 KB
/
romanNumeralExtensions.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
119
120
interface String
{
fromRoman(): number;
}
interface Number
{
toRoman(): string;
}
module Humanizer
{
"use strict";
interface RomanNumberalsDictionary
{
[letter: string]: number;
}
var romanNumberals: RomanNumberalsDictionary = {
"M": 1000,
"CM": 900,
"D": 500,
"CD": 400,
"C": 100,
"XC": 90,
"L": 50,
"XL": 40,
"X": 10,
"IX": 9,
"V": 5,
"IV": 4,
"I": 1
};
var validRomanNumerals: RegExp = /^(?:(?=[MDCLXVI])((M{0,3})((C[DM])|(D?C{0,3}))?((X[LC])|(L?XX{0,2})|L)?((I[VX])|(V?(II{0,2}))|V)?))$/;
/**
* Converts Roman numbers into integer
* @returns {Number} Human-readable number
*/
String.prototype.fromRoman = function (): number
{
/// <summary>
/// Converts Roman numbers into integer
/// </summary>
/// <returns type="Number" integer="true">
/// Human-readable number
/// </returns>
var input: string = this.toUpperCase().trim();
var length: number = input.length;
if ((length === 0) || !validRomanNumerals.test(input))
{
throw new Error("Empty or invalid Roman numeral string.");
}
var total: number = 0;
var i: number = length;
while (i > 0)
{
var digit = romanNumberals[input.charAt(--i)];
if (i > 0)
{
var previousDigit: number = romanNumberals[input.charAt(i - 1)];
if (previousDigit < digit)
{
digit -= previousDigit;
i--;
}
}
total += digit;
}
return total;
};
/**
* Converts the input to Roman number
* @returns {String} Roman number
*/
Number.prototype.toRoman = function (): string
{
/// <summary>
/// Converts the input to Roman number
/// </summary>
/// <returns type="String">
/// Roman number
/// </returns>
var minValue: number = 1;
var maxValue: number = 3999;
if ((this < minValue) || (this > maxValue))
{
throw new Error("Out of range");
}
var sb: string[] = [];
var input: number = this;
for (var key in romanNumberals)
{
if (Object.prototype.hasOwnProperty.call(romanNumberals, romanNumberals))
{
var value: number = romanNumberals[key];
while (input / value > 0)
{
sb.push(key);
input -= value;
}
}
}
return sb.join("");
};
}