Skip to content

Commit cfc534c

Browse files
committed
Simplify toRomanNumerals function
1 parent fdfcfbc commit cfc534c

File tree

1 file changed

+7
-19
lines changed

1 file changed

+7
-19
lines changed

src/core/core_utils.js

+7-19
Original file line numberDiff line numberDiff line change
@@ -165,25 +165,13 @@ function toRomanNumerals(number, lowerCase = false) {
165165
Number.isInteger(number) && number > 0,
166166
"The number should be a positive integer."
167167
);
168-
const romanBuf = [];
169-
// Thousands
170-
while (number >= 1000) {
171-
number -= 1000;
172-
romanBuf.push("M");
173-
}
174-
// Hundreds
175-
let pos = (number / 100) | 0;
176-
number %= 100;
177-
romanBuf.push(ROMAN_NUMBER_MAP[pos]);
178-
// Tens
179-
pos = (number / 10) | 0;
180-
number %= 10;
181-
romanBuf.push(ROMAN_NUMBER_MAP[10 + pos]);
182-
// Ones
183-
romanBuf.push(ROMAN_NUMBER_MAP[20 + number]); // eslint-disable-line unicorn/no-array-push-push
184-
185-
const romanStr = romanBuf.join("");
186-
return lowerCase ? romanStr.toLowerCase() : romanStr;
168+
169+
const roman = `${"M".repeat((number / 1000) | 0)}${
170+
ROMAN_NUMBER_MAP[((number % 1000) / 100) | 0]
171+
}${ROMAN_NUMBER_MAP[10 + (((number % 100) / 10) | 0)]}${
172+
ROMAN_NUMBER_MAP[20 + (number % 10)]
173+
}`;
174+
return lowerCase ? roman.toLowerCase() : roman;
187175
}
188176

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

0 commit comments

Comments
 (0)