Convert any number less than 4000 to roman numerals and back to integer
npm install toromanA minimalist library for Roman numeral operations.
- Convert ๐ Arabic numerals ๐ข to Roman numerals
- Convert ๐ Roman numerals to Arabic numerals ๐ข
- Validate Roman numerals โ
- Add Roman numerals โ
- Subtract Roman numerals โ
- Multiply Roman numerals โ๏ธ
- Divide Roman numerals โ
- Get maximum Roman numeral โฌ๏ธ
- Get minimum Roman numeral โฌ๏ธ
- Get a random Roman numeral ๐คน
- Generate a table of Roman numerals ๐
- Get Roman numerals within a range ๐ก
It can be installed with npm.
``sh`
npm i toroman
`js`
const roman = require("toRoman");
`ts`
/**
* Convert an integer to Roman numerals
* @param { number } value Integer to be converted to Roman numerals
* @returns { string } Roman numeral representation of the input value
* @throws { Error } When the input is not a valid integer or is out of range
*/
function toRoman(value: number): string {}
๐ต Example
`js
console.log(roman.toRoman(765));
// Returns DCCLXV
`
`ts`
/**
* Convert Roman numeral to integer
* @param { string } value Roman numeral to be converted to integer
* @returns { number } Integer representation of the input value
* @throws { Error } When the input is not a valid Roman numeral
*/
function fromRoman(value: string): number {}
๐ต Example
`js
console.log(roman.fromRoman("DCCLXV"));
// Returns 765
`
`ts`
/**
* Confirm that string is a valid roman numeral
* @param { string } value String to be tested
* @returns { boolean } true or false
* @throws { Error } When the input is not a valid roman numeral
*/
function isRoman(value: string): true {}
๐ต Example
`js
console.log(roman.isRoman("MMMCCXXXIV"));
// Returns true
`
`ts`
/**
* Sum roman numerals
* @param expected { string } Expected response type
* @param args { string[] } Roman numerals to be added
* @returns { string | number } Final roman numeral
* @throws { Error } When the result exceeds maximum value of 3999 or invalid numeral is provided
*/
function sum(expected: "number" | "roman", ...args: string[]): general {}
๐ต Example
`js
console.log(roman.sum("number", "X", "MXC"));
// Returns 1100
`
`ts`
/**
* Get difference between two roman numerals
* @param expected { string } Expected response type
* @param numerals { string[] } Roman numerals to subtract
* @returns { string | number }
* @throws { Error } When more than two numerals are provided
*/
function diff(
expected: "number" | "roman",
numerals: [string, string]
): general {}
๐ต Example
`js
console.log(roman.diff("number", ["X", "MXC"]));
// Returns 1080
`
`ts`
/**
* Multiply roman numerals
* @param expected { string } Expected response type
* @param args { string[] } Roman numerals to be added
* @returns { string | number } Final roman numeral
* @throws { Error } When the result exceeds maximum value of 3999 or invalid numeral is provided
*/
function multiply(expected: "number" | "roman", ...args: string[]): general {}
๐ต Example
`js
console.log(roman.multiply("roman", "X", "V"));
// Returns L
`
`ts`
/**
* Divide two roman numerals
* @param expected { string } Expected response type
* @param numerals { string[] } Roman numerals to divide
* @returns { string | number }
* @throws { Error } When more than two numerals are provided
*/
function divide(
expected: "number" | "roman",
numerals: [string, string]
): general {}
๐ต Example
`js
console.log(roman.divide("number", ["L", "X"]));
// Returns 5
`
`ts`
/**
* Get maximum roman numeral from a list
* @param args { string[] } Roman numerals to compare
* @returns { string } Maximum roman numeral
* @throws { Error } When an invalid numeral is provided
*/
function max(...args: string[]): string {}
๐ต Example
`js
console.log(roman.max("X", "V", "III", "VIII"));
// Returns X
`
`ts`
/**
* Get minimum roman numeral from a list
* @param args { string[] } Roman numerals to compare
* @returns { string } Minimum roman numeral
* @throws { Error } When an invalid numeral is provided
*/
function min(...args: string[]): string {}
๐ต Example
`js
console.log(roman.min("X", "V", "III", "VIII"));
// Returns III
`
`ts`
/**
* Generate a random Roman numeral within a specified range
* @param max Maximum value
* @param min Minimum value
* @returns { string } Random Roman numeral within the specified range
* @throws { Error } When the inputs are invalid or out of range
*/
function random(max: general = 3999, min: general = 1): string {}
๐ต Example
`js
console.log(roman.random(10));
// Returns a random Roman numeral between I and X
`
`ts`
/**
* Generate a table of Roman numerals within a specified range
* @param start Starting point for table
* @param end Stopping point for table
* @returns { number: number; roman: string }[] Array of objects containing number and its Roman numeral representation
* @throws { Error } When the inputs are invalid or out of range
*/
function table(
start: general,
end: general
): { number: number; roman: string }[] {}
๐ต Example
`js
console.log(roman.table("I", "V"));
/**
* Returns [
* { number: 1, roman: 'I' },
* { number: 2, roman: 'II' },
* { number: 3, roman: 'III' },
* { number: 4, roman: 'IV' },
* { number: 5, roman: 'V' }
* ]
*/
`
`ts`
/**
* Get range of roman numerals
* @param end { string | number } Value to stop at
* @param start { string | number } Value to start from
* @param intervals { string | number } Difference between values
* @returns { string[] } Array of roman numerals in the specified range
* @throws { Error } When any of the inputs are invalid or out of range
*/
function range(
end: general,
start: general = "I",
intervals: general = "I"
): string[] {}
๐ต Examples
`js
console.log(roman.range(7));
// Returns [ 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII' ]
`
`ts`
/**
* Get the next Roman numeral
* @param value Current Roman numeral
* @returns { string } Next Roman numeral
* @throws { Error } When the input is invalid or out of range
*/
export function nextRoman(value: string): string {}
๐ต Example
`js
console.log(roman.nextRoman("XIV"));
// Returns XV
`
`ts`
/**
* Get the previous Roman numeral
* @param value Current Roman numeral
* @returns { string } Previous Roman numeral
* @throws { Error } When the input is invalid or out of range
*/
export function previousRoman(value: string): string {}
๐ต Example
`js
console.log(roman.previousRoman("XIV"));
// Returns XIII
`
`ts`
/**
* Map an array of general inputs to either numbers or Roman numerals
* @param expected { string } Expected response type
* @param args { general[] } Array of general inputs
* @returns { general[] } Mapped array of numbers or Roman numerals
* @throws { Error } When any of the inputs are invalid or out of range
*/
export function map(expected: "number" | "roman", args: general[]): general[] {}
๐ต Example
`js
console.log(roman.map("roman", [1, 2, 3, 4, 5]));
// Returns [ 'I', 'II', 'III', 'IV', 'V' ]
``
If you found this project useful, or you like what you see, then please consider giving it a โญ๏ธ on GitHub and sharing it with your social media folks ๐.