From 60b92c9de18aedad7570b07aedc627be576e0a7e Mon Sep 17 00:00:00 2001 From: OptimusOpus Date: Tue, 9 Jan 2024 16:19:09 +1000 Subject: [PATCH 1/3] Allow fraction method to take bigunitish values --- src/bigunit.ts | 11 ++++++----- test/bigunit.fractions.test.ts | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/bigunit.ts b/src/bigunit.ts index c50f410..6b93ff6 100644 --- a/src/bigunit.ts +++ b/src/bigunit.ts @@ -5,9 +5,7 @@ import { InvalidValueTypeError, MissingPrecisionError, } from "./errors"; -import { bigintAbs, numberToDecimalString } from "./utils"; - -export * as utils from "./utils"; +import { bigintAbs, bigintCloseTo, numberToDecimalString } from "./utils"; export interface IBigUnitObject { value: string; @@ -21,6 +19,7 @@ export interface IBigUnitDTO extends IBigUnitObject { decimalValue: string; name?: string; } + export class BigUnit { constructor( public value: bigint, @@ -179,8 +178,10 @@ export class BigUnit { * @param fraction * @returns new BigUnit with the result value in the same precision */ - public fraction(numerator: number, denominator: number): BigUnit { - if (isNaN(numerator) || isNaN(denominator)) { + public fraction(numerator: BigUnitish, denominator: BigUnitish): BigUnit { + const numeratorNumber = Number(numerator); + const denominatorNumber = Number(denominator); + if (isNaN(numeratorNumber) || isNaN(denominatorNumber)) { throw new InvalidFractionError( "Numerator and denominator must be valid numbers", ); diff --git a/test/bigunit.fractions.test.ts b/test/bigunit.fractions.test.ts index a853b62..f502317 100644 --- a/test/bigunit.fractions.test.ts +++ b/test/bigunit.fractions.test.ts @@ -32,8 +32,8 @@ describe("BigUnit Class fraction handling methods", () => { }); }); - describe("fraction method", () => { - test("should calculate the correct fraction with valid numerator and denominator", () => { + describe.only("fraction method", () => { + test("should calculate the correct fraction with valid number type numerator and denominator", () => { const numerator = 1; const denominator = 2; // 1/2 const result = bigUnit.fraction(numerator, denominator); @@ -43,6 +43,36 @@ describe("BigUnit Class fraction handling methods", () => { expect(result.precision).toBe(precision); }); + test("should calculate the correct fraction with valid string type numerator and denominator", () => { + const numerator = "1"; + const denominator = "2"; // 1/2 + const result = bigUnit.fraction(numerator, denominator); + const expectedValue = 5000n; // 1/2 of 10000n is 5000n + + expect(result.value).toBe(expectedValue); + expect(result.precision).toBe(precision); + }); + + test("should calculate the correct fraction with valid BigInt type numerator and denominator", () => { + const numerator = 1n; + const denominator = 2n; // 1/2 + const result = bigUnit.fraction(numerator, denominator); + const expectedValue = 5000n; // 1/2 of 10000n is 5000n + + expect(result.value).toBe(expectedValue); + expect(result.precision).toBe(precision); + }); + + test("should calculate the correct fraction with valid Bigunit type numerator and denominator", () => { + const numerator = new BigUnit(1n, 0); + const denominator = new BigUnit(2n, 0); // 1/2 + const result = bigUnit.fraction(numerator, denominator); + const expectedValue = 5000n; // 1/2 of 10000n is 5000n + + expect(result.value).toBe(expectedValue); + expect(result.precision).toBe(precision); + }); + test("should handle zero numerator correctly", () => { const numerator = 0; const denominator = 2; // 0/2 From 318b61d8c68e2a56f7e4ce099c4e6e97c79534fe Mon Sep 17 00:00:00 2001 From: OptimusOpus Date: Tue, 9 Jan 2024 16:34:21 +1000 Subject: [PATCH 2/3] Allow percent method to take bigunitish values --- src/bigunit.ts | 7 ++++--- test/bigunit.fractions.test.ts | 31 +++++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/bigunit.ts b/src/bigunit.ts index 6b93ff6..70cd61d 100644 --- a/src/bigunit.ts +++ b/src/bigunit.ts @@ -192,8 +192,9 @@ export class BigUnit { throw new DivisionByZeroError(); } - const bigUnitNumerator = BigUnit.from(numerator, this.precision); - const bigUnitDenominator = BigUnit.from(denominator, this.precision); + const bigUnitNumerator = BigUnit.from(numeratorNumber, this.precision); + const bigUnitDenominator = BigUnit.from(denominatorNumber, this.precision); + const resultValue = this.mul(bigUnitNumerator).div(bigUnitDenominator); // Return a new BigUnit with the result value and the same precision return resultValue; @@ -204,7 +205,7 @@ export class BigUnit { * @param percent * @returns new BigUnit with the result value in the same precision */ - public percent(percent: number): BigUnit { + public percent(percent: BigUnitish): BigUnit { return this.fraction(percent, 100); } diff --git a/test/bigunit.fractions.test.ts b/test/bigunit.fractions.test.ts index f502317..138c843 100644 --- a/test/bigunit.fractions.test.ts +++ b/test/bigunit.fractions.test.ts @@ -6,7 +6,7 @@ describe("BigUnit Class fraction handling methods", () => { const bigUnit = new BigUnit(value, precision); describe("percent method", () => { - test("should calculate the correct percent with a valid percentage", () => { + test("should calculate the correct percent with a valid number percentage", () => { const percentage = 10; // 10% const result = bigUnit.percent(percentage); const expectedValue = 1000n; // 10% of 10000n is 1000n @@ -15,6 +15,33 @@ describe("BigUnit Class fraction handling methods", () => { expect(result.precision).toBe(precision); }); + test("should calculate the correct percent with a valid string percentage", () => { + const percentage = "10"; // 10% + const result = bigUnit.percent(percentage); + const expectedValue = 1000n; // 10% of 10000n is 1000n + + expect(result.value).toBe(expectedValue); + expect(result.precision).toBe(precision); + }); + + test("should calculate the correct percent with a valid BigInt percentage", () => { + const percentage = 10n; // 10% + const result = bigUnit.percent(percentage); + const expectedValue = 1000n; // 10% of 10000n is 1000n + + expect(result.value).toBe(expectedValue); + expect(result.precision).toBe(precision); + }); + + test("should calculate the correct percent with a valid Bigunit percentage", () => { + const percentage = new BigUnit(10n, 0); // 10% + const result = bigUnit.percent(percentage); + const expectedValue = 1000n; // 10% of 10000n is 1000n + + expect(result.value).toBe(expectedValue); + expect(result.precision).toBe(precision); + }); + test("should handle zero percent correctly", () => { const percentage = 0; // 0% const result = bigUnit.percent(percentage); @@ -32,7 +59,7 @@ describe("BigUnit Class fraction handling methods", () => { }); }); - describe.only("fraction method", () => { + describe("fraction method", () => { test("should calculate the correct fraction with valid number type numerator and denominator", () => { const numerator = 1; const denominator = 2; // 1/2 From dd5fb968e407452254ac453bd2f481176f4a743c Mon Sep 17 00:00:00 2001 From: OptimusOpus Date: Tue, 9 Jan 2024 16:36:40 +1000 Subject: [PATCH 3/3] Add missing import --- src/bigunit.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/bigunit.ts b/src/bigunit.ts index 70cd61d..924ba41 100644 --- a/src/bigunit.ts +++ b/src/bigunit.ts @@ -5,7 +5,9 @@ import { InvalidValueTypeError, MissingPrecisionError, } from "./errors"; -import { bigintAbs, bigintCloseTo, numberToDecimalString } from "./utils"; +import { bigintAbs, numberToDecimalString } from "./utils"; + +export * as utils from "./utils"; export interface IBigUnitObject { value: string;