Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
```

🔵 <b>Example</b>

```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 {}
```

🔵 <b>Example</b>

```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[] {}
```

🔵 <b>Example</b>

```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 🙂.
41 changes: 40 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down Expand Up @@ -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;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
40 changes: 40 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
}
23 changes: 22 additions & 1 deletion src/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
sum,
toRoman,
max,
min, random, table, validateGeneral
min, random, table, validateGeneral, nextRoman, map, previousRoman
} from "../index";

describe("getCount", () => {
Expand Down Expand Up @@ -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);
});
});
18 changes: 18 additions & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});