* Translates given roman numerals to decimal. * @protected * @param {string} romanNumerals * @return {?number} Decimal or null, if not defined.
(romanNumerals)
| 245 | * @return {?number} Decimal or null, if not defined. |
| 246 | */ |
| 247 | static romanNumeralsToDecimal (romanNumerals) { |
| 248 | romanNumerals = romanNumerals.toString().toUpperCase() |
| 249 | |
| 250 | let index = 0 |
| 251 | let decimal = 0 |
| 252 | let error = false |
| 253 | |
| 254 | let previousNumeral = 0 |
| 255 | let numeral |
| 256 | |
| 257 | while (!error && index < romanNumerals.length) { |
| 258 | // Find first roman numeral with the highest value |
| 259 | numeral = romanNumeralSymbols.findIndex(symbol => |
| 260 | romanNumerals.substr(index, symbol.length) === symbol) |
| 261 | |
| 262 | if (numeral !== -1 && numeral >= previousNumeral) { |
| 263 | // Append roman numeral |
| 264 | decimal += romanNumeralValues[numeral] |
| 265 | // Move cursor |
| 266 | index += romanNumeralSymbols[numeral].length |
| 267 | // Track previous numeral |
| 268 | previousNumeral = numeral |
| 269 | } else { |
| 270 | // Unexpected numeral |
| 271 | error = true |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | return !error ? decimal : null |
| 276 | } |
| 277 | } |
no test coverage detected