diff --git a/src/bigunit.ts b/src/bigunit.ts index c50f410..924ba41 100644 --- a/src/bigunit.ts +++ b/src/bigunit.ts @@ -21,6 +21,7 @@ export interface IBigUnitDTO extends IBigUnitObject { decimalValue: string; name?: string; } + export class BigUnit { constructor( public value: bigint, @@ -179,8 +180,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", ); @@ -191,8 +194,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; @@ -203,7 +207,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 a853b62..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); @@ -33,7 +60,7 @@ describe("BigUnit Class fraction handling methods", () => { }); describe("fraction method", () => { - test("should calculate the correct fraction with valid numerator and denominator", () => { + 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 +70,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