JavaScript library for work with Roman numbers.
- TypeScript and JSDoc support
- Conversion between Roman and Arabic numbers
- RomanNumber class
- Ability to perform basic mathematical operations
npm install @dunkelhaiser/numeri-romani
yarn add @dunkelhaiser/numeri-romani
pnpm add @dunkelhaiser/numeri-romani
bun add @dunkelhaiser/numeri-romani
import { RomanNumber, romanNumerals, romanize, romanizeSafe, arabicize, arabicizeSafe, isValidRoman, isValidRomanSafe, isValidArabic, isValidArabicSafe } from "@dunkelhaiser/numeri-romani";
const { RomanNumber, romanNumerals, romanize, romanizeSafe, arabicize, arabicizeSafe, isValidRoman, isValidRomanSafe, isValidArabic, isValidArabicSafe } = require("@dunkelhaiser/numeri-romani");
romanize(379); // => "CCCLXXIX"
arabicize("LIV"); // => 54
With the default implementation of these functions, if conversion fails, a corresponding error will be thrown explaining the reason for the failure. Default functions use isValidRoman
and isValidArabic
, so errors will be the same as theirs.
If you don't want to handle errors, you can use safe implementations of these functions romanizeSafe
or arabicizeSafe
which on fail will return "" or NaN correspondingly.
To check if a string is a valid Roman number you can use isValidRoman
or isValidRomanSafe
.
isValidRoman("LIV"); // => true
isValidRoman("IIV"); // => Error: Invalid roman number
isValidRomanSafe("LIV"); // => true
isValidRomanSafe("IIV"); // => false
To check if an Arabic number can be converted into a Roman number you can use isValidArabic
or isValidArabicSafe
.
isValidArabic("LIV"); // => true
isValidArabic(5.5); // => Error: Cannot convert non-integer number
isValidArabic(0); // => Error: Cannot convert zero
isValidArabic(-2); // => Error: Cannot convert negative numbers
isValidArabic(-4567); // => Error: Cannot convert numbers greater than 3999
isValidArabicSafe(14); // => true
isValidArabicSafe(5.5); // => false
isValidArabicSafe("14"); // => false
RomanNumber class provides an ability to create RomanNumber an object that will contain Roman and Arabic numeral values. Also, this object provides arithmetic operations methods.
const romanNumber = new RomanNumber(7); // { roman: "VII", arabic: 7 }
const romanNumber = new RomanNumber("VII"); // { roman: "VII, arabic: 7 }
romanNumber.getValue(); // => "VII"
romanNumber.getNumericValue(); // => 7
romanNumber.getValues(); // => { roman: "VII", arabic: 7 }
romanNumber.setValue(9); // { roman: "IX", arabic: 9 }
romanNumber.setValue("IX"); // { roman: "IX", arabic: 9 }
Arithmetic operations will update Roman and Arabic values of an instantiated object and return it, so it can be reassigned or method chained.
All methods support Arabic and Roman numbers as an argument.
The Roman number must correspond to the isValidRoman
function. Arabic number can be any integer value.
The results of operations must correspond to the isValidRoman
function or an error will be thrown.
romanNumber.add(4).add("IV");
romanNumber.substract(4).subtract("IV");
romanNumber.multiply(4).multiply("IV");
romanNumber.divide(4).divide("IV");
If you come across a bug or unexpected behavior, please take the time to report it. To file a bug report:
- Check if the issue has already been reported by searching the issues.
- If the issue hasn't been reported yet, open a new issue, providing as much detail as possible, including:
- A clear and concise title.
- A detailed description of the issue.
- Steps to reproduce the problem.
- Expected and actual behavior.
To propose a new feature:
- Check the issues to ensure it hasn't been proposed before.
- Open a new issue, clearly describing the new feature or enhancement you would like to see.
- Provide any relevant details or use cases that will help understand the use of the proposed feature.
To contribute code:
- Fork the repository.
- Create a new branch for your changes with a specific prefix:
git checkout -b feat/your-feature
. Accepted prefixes: feat, fix, refactor, docs. - Make your changes, following the coding style.
- Write unit tests for your changes.
- Update the README or documentation if necessary.
- Submit a pull request to the dev branch of the original repository.
- Provide a detailed description in the pull request, explaining the purpose of your changes.
Copyright (c) 2024 Kyrylo Tymchyshyn
Licensed under the MIT license.