diff --git a/README.md b/README.md index 16a4a1d..66d348d 100644 --- a/README.md +++ b/README.md @@ -302,6 +302,67 @@ console.log(roman.range(7)); // Returns [ 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII' ] ``` +### ⏭️ Get the next Roman numeral: `nextRoman` + +```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 +``` + +### ⏮️ Get the previous Roman numeral: `prevRoman` + +```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 +``` + +### 🔀 Map an array of general inputs to either numbers or Roman numerals: `map` + +```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' ] +``` + ## ✨ Found this project useful? 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 🙂. diff --git a/index.js b/index.js index b983a17..9ed1559 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,9 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.table = +exports.map = + exports.previousRoman = + exports.nextRoman = + exports.table = exports.random = exports.min = exports.max = @@ -431,3 +434,39 @@ function table(start, end) { return result; } exports.table = table; +/** + * 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 + */ +function nextRoman(value) { + return toRoman(fromRoman(value) + 1); +} +exports.nextRoman = nextRoman; +/** + * 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 + */ +function previousRoman(value) { + return toRoman(fromRoman(value) - 1); +} +exports.previousRoman = previousRoman; +/** + * 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 + */ +function map(expected, args) { + if (expected === "number") { + return args.map((item) => validateGeneral(item)); + } else if (expected === "roman") { + return args.map((item) => toRoman(validateGeneral(item))); + } + return []; +} +exports.map = map; diff --git a/package.json b/package.json index 4f6f14f..09d97b2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "toroman", - "version": "1.2.0", + "version": "2.0.1", "description": "Convert any number less than 4000 to roman numerals and back to integer", "main": "index.js", "scripts": { diff --git a/src/index.ts b/src/index.ts index d348852..dc2495e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -507,3 +507,43 @@ export function table( return result; } + +/** + * 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 { + return toRoman(fromRoman(value) as number + 1); +} + +/** + * 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 { + return toRoman(fromRoman(value) as number - 1); +} + +/** + * 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[] { + if (expected === "number") { + return args.map((item) => validateGeneral(item)); + } else if (expected === "roman") { + return args.map((item) => toRoman(validateGeneral(item)) as string); + } + + return []; +} diff --git a/src/tests/index.test.ts b/src/tests/index.test.ts index da4ab7c..1b3e216 100644 --- a/src/tests/index.test.ts +++ b/src/tests/index.test.ts @@ -9,7 +9,7 @@ import { sum, toRoman, max, - min, random, table, validateGeneral + min, random, table, validateGeneral, nextRoman, map, previousRoman } from "../index"; describe("getCount", () => { @@ -693,3 +693,24 @@ describe("validateGeneral", () => { } }); }); + +describe("nextRoman", () => { + test("should return II when I is given", () => { + expect(nextRoman("I")).toBe("II"); + }); +}); + +describe("previousRoman", () => { + test("should return I when II is given", () => { + expect(previousRoman("II")).toBe("I"); + }); +}); + +describe("map", () => { + test("should map an array of numbers to roman numerals", () => { + const input = [1, 2, 3, 4, 5]; + const expected = ["I", "II", "III", "IV", "V"]; + const result = map("roman", input); + expect(result).toEqual(expected); + }); +}); diff --git a/tests/index.test.js b/tests/index.test.js index bd114ea..3cef2c6 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -610,3 +610,21 @@ describe("validateGeneral", () => { } }); }); +describe("nextRoman", () => { + test("should return II when I is given", () => { + expect((0, index_1.nextRoman)("I")).toBe("II"); + }); +}); +describe("previousRoman", () => { + test("should return I when II is given", () => { + expect((0, index_1.previousRoman)("II")).toBe("I"); + }); +}); +describe("map", () => { + test("should map an array of numbers to roman numerals", () => { + const input = [1, 2, 3, 4, 5]; + const expected = ["I", "II", "III", "IV", "V"]; + const result = (0, index_1.map)("roman", input); + expect(result).toEqual(expected); + }); +});