Skip to content

Commit

Permalink
feat: Add asFraction method (pancakeswap#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chef-Cheems authored Feb 17, 2022
1 parent 143cdbe commit 2ab187f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/entities/fractions/fraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,11 @@ export class Fraction {
Big.RM = toFixedRounding[rounding]
return new Big(this.numerator.toString()).div(this.denominator.toString()).toFormat(decimalPlaces, format)
}

/**
* Helper method for converting any super class back to a fraction
*/
public get asFraction(): Fraction {
return new Fraction(this.numerator, this.denominator)
}
}
7 changes: 7 additions & 0 deletions test/fraction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,11 @@ describe.only('Fraction', () => {
).toEqual(new Fraction(JSBI.BigInt(60), JSBI.BigInt(48)))
})
})
describe('#asFraction', () => {
it('returns an equivalent but not the same reference fraction', () => {
const f = new Fraction(1, 2)
expect(f.asFraction).toEqual(f)
expect(f === f.asFraction).toEqual(false)
})
})
})

0 comments on commit 2ab187f

Please sign in to comment.