Skip to content

Commit

Permalink
update comparing, fix value getter, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lukachi committed Mar 11, 2023
1 parent 62ace84 commit 6315a82
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions packages/tools/src/bn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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)
})
})

Expand Down
31 changes: 13 additions & 18 deletions packages/tools/src/bn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 6315a82

Please sign in to comment.