Skip to content

Commit

Permalink
Merge pull request #348 from Etherna/fix/EDD-415-decimals
Browse files Browse the repository at this point in the history
fix fraction parsing without decimals
  • Loading branch information
mattiaz9 authored Nov 24, 2024
2 parents 6eb5bc1 + e49bb7d commit 94214de
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/classes/Fraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ export default class Fraction {
}

static findPeriodDigits(decimal: number) {
const [num, den] = decimal.toString().split(".")
let [num, den] = decimal.toString().split(".")

if (!num || !den) {
if (!num) {
throw new Error("Invalid decimal")
}

den ??= "0"

const isStringRepeating = (s: string) => (s + s).indexOf(s, 1) < s.length
const repeatingString = (s: string) => s.slice(0, (s + s).indexOf(s, 1))
const isRoundedDenominator =
Expand All @@ -52,12 +54,15 @@ export default class Fraction {
}

static fromDecimal(decimal: number) {
const [num, den] = decimal.toString().split(".")
let [num, den] = decimal.toString().split(".")


Check warning on line 59 in src/classes/Fraction.ts

View workflow job for this annotation

GitHub Actions / Build

Delete `⏎`
if (!num || !den) {
if (!num) {
throw new Error("Invalid decimal")
}

den ??= "0"

const { isPeriodic, repeatingDigits } = Fraction.findPeriodDigits(decimal)

if (isPeriodic) {
Expand Down

0 comments on commit 94214de

Please sign in to comment.