diff --git a/CHANGELOG.md b/CHANGELOG.md index 6772e190..071e7172 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added +- `@distributedlab/tools`: `BN` `isEqualTo` compare + +### Changed +- `@distributedlab/tools`: `BN` comparing methods + +### Fixed +- `@distributedlab/tools`: `BN` return formatted string for value getter instead toString method ## [0.1.5] - 2023-03-09 ### Fixed diff --git a/packages/tools/src/bn.test.ts b/packages/tools/src/bn.test.ts index 80da59fb..806eef0e 100644 --- a/packages/tools/src/bn.test.ts +++ b/packages/tools/src/bn.test.ts @@ -9,6 +9,11 @@ describe('performs BN unit test', () => { expect(BN.fromRaw(1, 18).value).toBe('1000000000000000000') expect(BN.fromRaw(0.1, 6).value).toBe('100000') expect(BN.fromRaw(0.123, 6).value).toBe('123000') + expect(BN.fromRaw(9, 18).value).toBe('9000000000000000000') + expect(BN.fromRaw(99999, 18).value).toBe('99999000000000000000000') + expect(BN.fromBigInt('99999000000000000000000', 18).toString()).toBe( + '99999', + ) }) test('fromFraction should return correct value', () => { @@ -65,6 +70,12 @@ describe('performs BN unit test', () => { expect(BN.fromRaw(2, 18).isLessThanOrEqualTo(BN.fromRaw(2, 18))).toBe( true, ) + expect(BN.fromRaw(2, 18).isEqualTo(BN.fromRaw(2, 18))).toBe(true) + expect( + BN.fromBigInt('2000000000000000000', 18).isEqualTo( + BN.fromBigInt('2000000000000000000', 18), + ), + ).toBe(true) }) }) diff --git a/packages/tools/src/bn.ts b/packages/tools/src/bn.ts index 12be90a6..eefb6d0b 100644 --- a/packages/tools/src/bn.ts +++ b/packages/tools/src/bn.ts @@ -167,7 +167,11 @@ export class BN { } public get value(): string { - return this.#bn.toString() + return this.#bn.toFormat({ + groupSeparator: '', + decimalSeparator: '.', + fractionGroupSeparator: '', + }) } public clone(cfg?: BnCfg): BN { @@ -238,19 +242,23 @@ export class BN { } public isGreaterThan(other: BN): boolean { - return this.#compare(other) === 1 + return this.#bn.isGreaterThan(other.bn) } public isGreaterThanOrEqualTo(other: BN): boolean { - return this.#compare(other) >= 0 + return this.#bn.isGreaterThanOrEqualTo(other.bn) } public isLessThan(other: BN): boolean { - return this.#compare(other) === -1 + return this.#bn.isLessThan(other.bn) } public isLessThanOrEqualTo(other: BN): boolean { - return this.#compare(other) <= 0 + return this.#bn.isLessThanOrEqualTo(other.bn) + } + + public isEqualTo(other: BN): boolean { + return this.#bn.isEqualTo(other.bn) } public round(precision: number, mode?: BN_ROUNDING): string { @@ -302,17 +310,4 @@ export class BN { fractionGroupSeparator: '', }) } - - /** - * this > other => 1; - * this < other => -1; - * this === other => 0; - * - * @param {BnLike} other - * @returns {number} - */ - #compare(other: BN): number { - const [numA, numB] = BN.#toGreatestDecimals(this, other) - return numA.bn.comparedTo(numB.bn) - } }