Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/bigunit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface IBigUnitDTO extends IBigUnitObject {
decimalValue: string;
name?: string;
}

export class BigUnit {
constructor(
public value: bigint,
Expand Down Expand Up @@ -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);

@bazmatic bazmatic Jan 9, 2024

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to do any number checking now... but you can't necessarily cast BigUnitish into number anyway

const denominatorNumber = Number(denominator);
if (isNaN(numeratorNumber) || isNaN(denominatorNumber)) {
throw new InvalidFractionError(
"Numerator and denominator must be valid numbers",
);
Expand All @@ -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;
Expand All @@ -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);
}

Expand Down
61 changes: 59 additions & 2 deletions test/bigunit.fractions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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
Expand Down