Skip to content

Commit

Permalink
chore: more refactoring, create parsers dir
Browse files Browse the repository at this point in the history
  • Loading branch information
EricRovell committed Mar 22, 2024
1 parent 8acb823 commit 54933e8
Show file tree
Hide file tree
Showing 17 changed files with 340 additions and 345 deletions.
6 changes: 3 additions & 3 deletions src/exponent.ts → src/helpers/exponent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { factorize } from "./utils";
import type { Rational } from "./rational";
import type { Input, Ratio } from "./types";
import { factorize } from "../utils";
import type { Rational } from "../rational";
import type { Input, Ratio } from "../types";

interface Factors {
type: "num" | "den";
Expand Down
36 changes: 8 additions & 28 deletions src/ratio.ts → src/helpers/ratio.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { gcd } from "./utils";
import type { Ratio, Fraction, FractionUnknown } from "./types";
import { isValidInteger } from "./validators";
import { isValidInteger } from "../validators";
import type { Ratio, Fraction } from "../types";

/**
* Handles the sign of the input ratio.
Expand All @@ -13,14 +12,6 @@ function handleRatioSign(n: number, m: number): Ratio {
: [ -Math.abs(n), Math.abs(m) ];
}

/**
* Simplifies the ratio.
*/
export function simplifyRatio([ n = 0, d = 1 ]: Ratio): Ratio {
const divisor = gcd(n, d);
return [ n / divisor, d / divisor ];
}

/**
* Handles the sign of the input fraction.
*
Expand All @@ -44,29 +35,18 @@ function handleFractionSign({ int = 0, n, d = 1 }: Fraction): Ratio {
/**
* Produces a ratio from fraction.
*/
export function getRatioFromFraction({ int = 0, n, d = 1 }: FractionUnknown): Ratio | null {
const [ integer, num, den ] = [ int, n, d ].map(value => {
return Math.floor(Number(value));
});

if (!isValidInteger(integer) || !isValidInteger(num) || !isValidInteger(den)) {
export function getRatio({ int = 0, n, d = 1 }: Fraction): Ratio | null {
if (!isValidInteger(int) || !isValidInteger(n) || !isValidInteger(d)) {
return null;
}

if (den === 0) {
if (d === 0) {
return null;
}

return handleFractionSign({
int: integer,
n: num,
d: den
int,
n,
d
});
}

/**
* Produces a ratio.
*/
export function getRatio(n: unknown, d: unknown): Ratio | null {
return getRatioFromFraction({ n, d });
}
27 changes: 13 additions & 14 deletions src/repeating-decimal.ts → src/helpers/repeating-decimal.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getRatio } from "./ratio";
import { factorize } from "./utils";
import type { Rational } from "./rational";
import type { Ratio, RepeatingDecimal } from "./types";
import { factorize } from "../utils";
import type { Rational } from "../rational";
import type { Ratio, RepeatingDecimal } from "../types";

/**
* Detects if the given rational number is terminated decimal.
Expand Down Expand Up @@ -56,16 +56,15 @@ export function ratio2repeatingDecimal(a: number, b: number): string {
remainder = remainder % d;
}

if (remainder == 0) {
if (remainder == 0 || !remainders.has(remainder)) {
return "";
} else if (remainders.has(remainder)) {
const position = remainders.get(remainder);
const nonrepeat = result.slice(0, position);
const repeat = result.slice(position);
return `${int}.${nonrepeat}(${repeat})`;
}

return "";
const position = remainders.get(remainder);
const nonrepeat = result.slice(0, position);
const repeat = result.slice(position);

return `${int}.${nonrepeat}(${repeat})`;
}

/**
Expand All @@ -79,8 +78,8 @@ export function getRatioFromRepeatingDecimal({ sign = 1, int = 0, nonrepeat = ""
const denominator = Number(`${"9".repeat(period.length)}${"0".repeat(nonperiod.length)}`);
const numerator = Number(`${nonperiod}${repeat}`) - Number(nonperiod);

return getRatio(
sign * (integralPart * denominator + numerator),
denominator
);
return getRatio({
n: sign * (integralPart * denominator + numerator),
d: denominator
});
}
41 changes: 0 additions & 41 deletions src/matchers.ts

This file was deleted.

18 changes: 0 additions & 18 deletions src/parser.ts

This file was deleted.

177 changes: 0 additions & 177 deletions src/parsers.ts

This file was deleted.

14 changes: 14 additions & 0 deletions src/parsers/arguments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { getRatio } from "../helpers/ratio";
import { isValidInteger } from "../validators";
import { Parser } from "../types";

/**
* Parses a ratio from two integers.
*/
export const parseArguments: Parser = (n, d = 1) => {
if (!isValidInteger(n) || !isValidInteger(d)) {
return null;
}

return getRatio({ n, d });
};
Loading

0 comments on commit 54933e8

Please sign in to comment.