Skip to content

Commit 03c8ae6

Browse files
Merge pull request #6 from distributed-lab/fix/util-bn-value
update comparing, fix value getter, add tests
2 parents 62ace84 + 6315a82 commit 03c8ae6

3 files changed

Lines changed: 32 additions & 18 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Added
9+
- `@distributedlab/tools`: `BN` `isEqualTo` compare
10+
11+
### Changed
12+
- `@distributedlab/tools`: `BN` comparing methods
13+
14+
### Fixed
15+
- `@distributedlab/tools`: `BN` return formatted string for value getter instead toString method
816

917
## [0.1.5] - 2023-03-09
1018
### Fixed

packages/tools/src/bn.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ describe('performs BN unit test', () => {
99
expect(BN.fromRaw(1, 18).value).toBe('1000000000000000000')
1010
expect(BN.fromRaw(0.1, 6).value).toBe('100000')
1111
expect(BN.fromRaw(0.123, 6).value).toBe('123000')
12+
expect(BN.fromRaw(9, 18).value).toBe('9000000000000000000')
13+
expect(BN.fromRaw(99999, 18).value).toBe('99999000000000000000000')
14+
expect(BN.fromBigInt('99999000000000000000000', 18).toString()).toBe(
15+
'99999',
16+
)
1217
})
1318

1419
test('fromFraction should return correct value', () => {
@@ -65,6 +70,12 @@ describe('performs BN unit test', () => {
6570
expect(BN.fromRaw(2, 18).isLessThanOrEqualTo(BN.fromRaw(2, 18))).toBe(
6671
true,
6772
)
73+
expect(BN.fromRaw(2, 18).isEqualTo(BN.fromRaw(2, 18))).toBe(true)
74+
expect(
75+
BN.fromBigInt('2000000000000000000', 18).isEqualTo(
76+
BN.fromBigInt('2000000000000000000', 18),
77+
),
78+
).toBe(true)
6879
})
6980
})
7081

packages/tools/src/bn.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,11 @@ export class BN {
167167
}
168168

169169
public get value(): string {
170-
return this.#bn.toString()
170+
return this.#bn.toFormat({
171+
groupSeparator: '',
172+
decimalSeparator: '.',
173+
fractionGroupSeparator: '',
174+
})
171175
}
172176

173177
public clone(cfg?: BnCfg): BN {
@@ -238,19 +242,23 @@ export class BN {
238242
}
239243

240244
public isGreaterThan(other: BN): boolean {
241-
return this.#compare(other) === 1
245+
return this.#bn.isGreaterThan(other.bn)
242246
}
243247

244248
public isGreaterThanOrEqualTo(other: BN): boolean {
245-
return this.#compare(other) >= 0
249+
return this.#bn.isGreaterThanOrEqualTo(other.bn)
246250
}
247251

248252
public isLessThan(other: BN): boolean {
249-
return this.#compare(other) === -1
253+
return this.#bn.isLessThan(other.bn)
250254
}
251255

252256
public isLessThanOrEqualTo(other: BN): boolean {
253-
return this.#compare(other) <= 0
257+
return this.#bn.isLessThanOrEqualTo(other.bn)
258+
}
259+
260+
public isEqualTo(other: BN): boolean {
261+
return this.#bn.isEqualTo(other.bn)
254262
}
255263

256264
public round(precision: number, mode?: BN_ROUNDING): string {
@@ -302,17 +310,4 @@ export class BN {
302310
fractionGroupSeparator: '',
303311
})
304312
}
305-
306-
/**
307-
* this > other => 1;
308-
* this < other => -1;
309-
* this === other => 0;
310-
*
311-
* @param {BnLike} other
312-
* @returns {number}
313-
*/
314-
#compare(other: BN): number {
315-
const [numA, numB] = BN.#toGreatestDecimals(this, other)
316-
return numA.bn.comparedTo(numB.bn)
317-
}
318313
}

0 commit comments

Comments
 (0)