@numerals/roman
Version:
45 lines (44 loc) • 1.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.convert = void 0;
const numerals_1 = require("./numerals");
function convert(source) {
if (!Number.isFinite(source)) {
throw new Error('Source is not a finite number');
}
if (source < 0) {
throw new Error('Source is negative, only positive numbers are supported');
}
if (source == 0) {
throw new Error('Source is zero, only positive numbers are supported');
}
if (source % 1 !== 0) {
throw new Error('Source is not an integer, only integers are supported');
}
if (source > 1000000) {
throw new Error('Source is too big, only numbers up to 1,000,000 are supported');
}
let result = '';
let reminder = source;
const anchorArr = [
100000, 90000, 50000, 40000, 10000, 9000, 5000, 4000,
1000, 900, 500, 400, 100, 90, 50, 40,
10, 5, 1,
];
const matchArr = [
12, 11, 9, 8, 7, 6, 4, 3, 2,
];
for (const n of anchorArr) {
const match = matchArr.find((it) => it === reminder);
if (match !== undefined) {
result += numerals_1.Ro[match];
break;
}
while (reminder >= n) {
result += numerals_1.Ro[n];
reminder -= n;
}
}
return result;
}
exports.convert = convert;