Skip to content

Commit

Permalink
Export pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
filiptammergard committed Mar 6, 2024
1 parent 261ac43 commit aa325c1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/curvy-owls-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tammergard/roman": patch
---

Export pattern.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export declare function toRoman(arabic: number): string
export declare function fromRoman(roman: string): number
export declare const pattern: RegExp
11 changes: 5 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-check

const pattern =
export const pattern =
/^(M{1,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})|M{0,4}(CM|C?D|D?C{1,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})|M{0,4}(CM|CD|D?C{0,3})(XC|X?L|L?X{1,3})(IX|IV|V?I{0,3})|M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|I?V|V?I{1,3}))$/

const romanArabicMap = new Map([
Expand All @@ -19,8 +19,6 @@ const romanArabicMap = new Map([
["I", 1],
])

const arabicRomanMap = new Map(Array.from(romanArabicMap, (a) => a.reverse()))

/**
* @param {number} arabic An arabic number.
* @returns {string} The input converted to roman numeral.
Expand All @@ -45,10 +43,11 @@ export function toRoman(arabic) {
}
let roman = ""
let acc = arabic
const numerals = [...romanArabicMap.values()]
numerals.forEach((value) => {
const values = [...romanArabicMap.values()]
const keys = [...romanArabicMap.keys()]
values.forEach((value, index) => {
while (acc >= value) {
roman += arabicRomanMap.get(value)
roman += keys[index]
acc -= value
}
})
Expand Down

0 comments on commit aa325c1

Please sign in to comment.