Skip to content

Simplify toRomanNumerals function #19011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 6, 2024
Merged
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
26 changes: 7 additions & 19 deletions src/core/core_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,25 +165,13 @@ function toRomanNumerals(number, lowerCase = false) {
Number.isInteger(number) && number > 0,
"The number should be a positive integer."
);
const romanBuf = [];
// Thousands
while (number >= 1000) {
number -= 1000;
romanBuf.push("M");
}
// Hundreds
let pos = (number / 100) | 0;
number %= 100;
romanBuf.push(ROMAN_NUMBER_MAP[pos]);
// Tens
pos = (number / 10) | 0;
number %= 10;
romanBuf.push(ROMAN_NUMBER_MAP[10 + pos]);
// Ones
romanBuf.push(ROMAN_NUMBER_MAP[20 + number]); // eslint-disable-line unicorn/no-array-push-push

const romanStr = romanBuf.join("");
return lowerCase ? romanStr.toLowerCase() : romanStr;

const roman =
"M".repeat((number / 1000) | 0) +
ROMAN_NUMBER_MAP[((number % 1000) / 100) | 0] +
ROMAN_NUMBER_MAP[10 + (((number % 100) / 10) | 0)] +
ROMAN_NUMBER_MAP[20 + (number % 10)];
return lowerCase ? roman.toLowerCase() : roman;
}

// Calculate the base 2 logarithm of the number `x`. This differs from the
Expand Down