mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-05-08 15:15:02 -04:00
fix(roman-numeral-converter): checks for valid input and conversion enhancements
Validates if numeral values are between 1 and 3999999. Validates if a roman number is valid.
This commit is contained in:
parent
f350dc19aa
commit
3fe9dc4c0c
3 changed files with 79 additions and 15 deletions
|
@ -13,14 +13,17 @@ describe('roman-numeral-converter', () => {
|
|||
expect(arabicToRoman(0.9)).toEqual('');
|
||||
});
|
||||
|
||||
it('should convert numbers greater than 3999999 to empty string', () => {
|
||||
expect(arabicToRoman(3999999.1)).toEqual('');
|
||||
expect(arabicToRoman(4000000)).toEqual('');
|
||||
expect(arabicToRoman(10000000)).toEqual('');
|
||||
});
|
||||
|
||||
it('should convert floating points number to the lower integer in roman version', () => {
|
||||
expect(arabicToRoman(-100)).toEqual('');
|
||||
expect(arabicToRoman(-42)).toEqual('');
|
||||
expect(arabicToRoman(-26)).toEqual('');
|
||||
expect(arabicToRoman(-10)).toEqual('');
|
||||
expect(arabicToRoman(0)).toEqual('');
|
||||
expect(arabicToRoman(0.5)).toEqual('');
|
||||
expect(arabicToRoman(0.9)).toEqual('');
|
||||
expect(arabicToRoman(1.1)).toEqual('I');
|
||||
expect(arabicToRoman(1.9)).toEqual('I');
|
||||
expect(arabicToRoman(17.6)).toEqual('XVII');
|
||||
expect(arabicToRoman(29.999)).toEqual('XXIX');
|
||||
});
|
||||
|
||||
it('should convert positive integers to roman numbers', () => {
|
||||
|
@ -67,7 +70,10 @@ describe('roman-numeral-converter', () => {
|
|||
expect(arabicToRoman(999)).toEqual('CMXCIX');
|
||||
expect(arabicToRoman(1000)).toEqual('M');
|
||||
expect(arabicToRoman(2000)).toEqual('MM');
|
||||
expect(arabicToRoman(9000)).toEqual('MMMMMMMMM');
|
||||
expect(arabicToRoman(4000)).toEqual('<span style="text-decoration: overline">IV</span>');
|
||||
expect(arabicToRoman(5000)).toEqual('<span style="text-decoration: overline">V</span>');
|
||||
expect(arabicToRoman(9000)).toEqual('<span style="text-decoration: overline">IX</span>');
|
||||
expect(arabicToRoman(10000)).toEqual('<span style="text-decoration: overline">X</span>');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,7 +1,21 @@
|
|||
export const MIN_ARABIC_TO_ROMAN = 1;
|
||||
export const MAX_ARABIC_TO_ROMAN = 3999999;
|
||||
export function arabicToRoman(num: number) {
|
||||
if (num < 1) return '';
|
||||
if (num < MIN_ARABIC_TO_ROMAN || num > MAX_ARABIC_TO_ROMAN) return '';
|
||||
|
||||
const lookup: { [key: string]: number } = {
|
||||
'<span style="text-decoration: overline">M</span>': 1000000,
|
||||
'<span style="text-decoration: overline">CM</span>': 900000,
|
||||
'<span style="text-decoration: overline">D</span>': 500000,
|
||||
'<span style="text-decoration: overline">CD</span>': 400000,
|
||||
'<span style="text-decoration: overline">C</span>': 100000,
|
||||
'<span style="text-decoration: overline">XC</span>': 90000,
|
||||
'<span style="text-decoration: overline">L</span>': 50000,
|
||||
'<span style="text-decoration: overline">XL</span>': 40000,
|
||||
'<span style="text-decoration: overline">X</span>': 10000,
|
||||
'<span style="text-decoration: overline">IX</span>': 9000,
|
||||
'<span style="text-decoration: overline">V</span>': 5000,
|
||||
'<span style="text-decoration: overline">IV</span>': 4000,
|
||||
M: 1000,
|
||||
CM: 900,
|
||||
D: 500,
|
||||
|
@ -26,7 +40,16 @@ export function arabicToRoman(num: number) {
|
|||
return roman;
|
||||
}
|
||||
|
||||
const ROMAN_NUMBER_REGEX = new RegExp(/^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/);
|
||||
|
||||
export function isValidRomanNumber(romanNumber: string) {
|
||||
return ROMAN_NUMBER_REGEX.test(romanNumber);
|
||||
}
|
||||
|
||||
export function romanToArabic(s: string) {
|
||||
if (!isValidRomanNumber(s)) {
|
||||
return null;
|
||||
}
|
||||
const map: { [key: string]: number } = { I: 1, V: 5, X: 10, L: 50, C: 100, D: 500, M: 1000 };
|
||||
return [...s].reduce((r, c, i, s) => (map[s[i + 1]] > map[c] ? r - map[c] : r + map[c]), 0);
|
||||
}
|
||||
|
|
|
@ -2,21 +2,29 @@
|
|||
<div>
|
||||
<n-card title="Arabic to roman">
|
||||
<n-space align="center" justify="space-between">
|
||||
<n-input-number v-model:value="inputNumeral" :min="1" style="width: 200px" :show-button="false" />
|
||||
<n-form-item :feedback="validationNumeral.message" :validation-status="validationNumeral.status">
|
||||
<n-input-number v-model:value="inputNumeral" :min="1" style="width: 200px" :show-button="false" />
|
||||
</n-form-item>
|
||||
<div class="result">
|
||||
{{ outputRoman }}
|
||||
<span v-html="outputRoman"></span>
|
||||
</div>
|
||||
<n-button secondary autofocus @click="copyRoman"> Copy </n-button>
|
||||
<n-button secondary autofocus :disabled="validationNumeral.status === 'error'" @click="copyRoman">
|
||||
Copy
|
||||
</n-button>
|
||||
</n-space>
|
||||
</n-card>
|
||||
<br />
|
||||
<n-card title="Roman to arabic">
|
||||
<n-space align="center" justify="space-between">
|
||||
<n-input v-model:value="inputRoman" style="width: 200px" />
|
||||
<n-form-item :feedback="validationRoman.message" :validation-status="validationRoman.status">
|
||||
<n-input v-model:value="inputRoman" style="width: 200px" />
|
||||
</n-form-item>
|
||||
<div class="result">
|
||||
{{ outputNumeral }}
|
||||
</div>
|
||||
<n-button secondary autofocus @click="copyArabic"> Copy </n-button>
|
||||
<n-button secondary autofocus :disabled="validationRoman.status === 'error'" @click="copyArabic">
|
||||
Copy
|
||||
</n-button>
|
||||
</n-space>
|
||||
</n-card>
|
||||
</div>
|
||||
|
@ -25,14 +33,41 @@
|
|||
<script setup lang="ts">
|
||||
import { useCopy } from '@/composable/copy';
|
||||
import { ref, computed } from 'vue';
|
||||
import { arabicToRoman, romanToArabic } from './roman-numeral-converter.service';
|
||||
import { useValidation } from '@/composable/validation';
|
||||
import {
|
||||
arabicToRoman,
|
||||
romanToArabic,
|
||||
MAX_ARABIC_TO_ROMAN,
|
||||
MIN_ARABIC_TO_ROMAN,
|
||||
isValidRomanNumber,
|
||||
} from './roman-numeral-converter.service';
|
||||
|
||||
const inputNumeral = ref(42);
|
||||
const outputRoman = computed(() => arabicToRoman(inputNumeral.value));
|
||||
|
||||
const validationNumeral = useValidation({
|
||||
source: inputNumeral,
|
||||
rules: [
|
||||
{
|
||||
validator: (value) => value >= MIN_ARABIC_TO_ROMAN && value <= MAX_ARABIC_TO_ROMAN,
|
||||
message: `We can only convert numbers between ${MIN_ARABIC_TO_ROMAN.toLocaleString()} and ${MAX_ARABIC_TO_ROMAN.toLocaleString()}`,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const inputRoman = ref('XLII');
|
||||
const outputNumeral = computed(() => romanToArabic(inputRoman.value));
|
||||
|
||||
const validationRoman = useValidation({
|
||||
source: inputRoman,
|
||||
rules: [
|
||||
{
|
||||
validator: (value) => isValidRomanNumber(value),
|
||||
message: `The input you entered is not a valid roman number`,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const { copy: copyRoman } = useCopy({ source: outputRoman, text: 'Roman number copied to the clipboard' });
|
||||
const { copy: copyArabic } = useCopy({ source: outputNumeral, text: 'Arabic number copied to the clipboard' });
|
||||
</script>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue