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
33 changes: 33 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env node
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("./index");
const arg = process.argv[2];
if (!arg) {
console.log("Usage: toroman <number|roman>");
process.exit(1);
}
const input = arg.toUpperCase();
// try {
// console.log(input);
// console.log(Number(input));
// const isValidRoman = isRoman(input);
// console.log(fromRoman(input));
// console.log(isValidRoman);
// if (isValidRoman === true) {
// console.log(`${input} is a valid Roman numeral.`);
// }
// } catch (error) {
// console.error("Error: ", error);
// process.exit(1);
// }
if ((0, index_1.isRoman)(input) === true) {
console.log((0, index_1.fromRoman)(input));
} else {
const num = Number(arg);
if (isNaN(num)) {
console.error("Invalid input");
process.exit(1);
}
console.log((0, index_1.toRoman)(num));
}
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ function isRoman(value) {
if (!value) {
return new Error("Roman numeral cannot be empty");
}
// Input must be a string
if (typeof value != "string") {
// Input must be a string and not be a number
if (typeof value !== "string" || Number(value)) {
return new Error("Roman numeral must be of type string");
}
value = value.toUpperCase();
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,8 @@
},
"lint-staged": {
"*.{js,css,md}": "prettier --write"
},
"bin": {
"toroman": "./cli.js"
}
}
21 changes: 21 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env node
import { toRoman, fromRoman, isRoman } from "./index";

const arg = process.argv[2];

if (!arg) {
console.log("Usage: toroman <number|roman>");
process.exit(1);
}

const input = arg.toUpperCase();
if (isRoman(input) === true) {
console.log(fromRoman(input));
} else {
const num = Number(arg);
if (isNaN(num)) {
console.error("Invalid input");
process.exit(1);
}
console.log(toRoman(num));
}
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export function isRoman(value: string): true | Error {
return new Error("Roman numeral cannot be empty");
}

// Input must be a string
if (typeof value != "string") {
// Input must be a string and not be a number
if (typeof value !== "string" || Number(value)) {
return new Error("Roman numeral must be of type string");
}

Expand Down
Loading