Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: distributed-lab/web-kit
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 0.1.4
Choose a base ref
...
head repository: distributed-lab/web-kit
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 0.1.5
Choose a head ref
  • 4 commits
  • 5 files changed
  • 2 contributors

Commits on Mar 8, 2023

  1. Copy the full SHA
    e7039fe View commit details

Commits on Mar 9, 2023

  1. fix test

    napalmpapalam committed Mar 9, 2023
    Copy the full SHA
    e351a19 View commit details
  2. Merge pull request #5 from distributed-lab/fix/mul-div

    drop unexpected decimal part during multiplication and division
    napalmpapalam authored Mar 9, 2023
    Copy the full SHA
    89fba09 View commit details
  3. 0.1.5

    napalmpapalam committed Mar 9, 2023
    Copy the full SHA
    62ace84 View commit details
Showing with 22 additions and 8 deletions.
  1. +6 −1 CHANGELOG.md
  2. +1 −1 packages/jac/package.json
  3. +1 −1 packages/tools/package.json
  4. +9 −2 packages/tools/src/bn.test.ts
  5. +5 −3 packages/tools/src/bn.ts
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.1.5] - 2023-03-09
### Fixed
- `@distributedlab/tools`: `BN` drop unexpected decimal part during multiplication and division

## [0.1.4] - 2023-03-08
### Added
- `@distributedlab/tools`: `BN.clone()` method to safely clone `BN` instance
@@ -59,7 +63,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

[old repo]: https://github.com/distributed-lab/web-kit-old

[Unreleased]: https://github.com/distributed-lab/web-kit/compare/0.1.4...HEAD
[Unreleased]: https://github.com/distributed-lab/web-kit/compare/0.1.5...HEAD
[0.1.5]: https://github.com/distributed-lab/web-kit/compare/0.1.4...0.1.5
[0.1.4]: https://github.com/distributed-lab/web-kit/compare/0.1.3...0.1.4
[0.1.3]: https://github.com/distributed-lab/web-kit/compare/0.1.2...0.1.3
[0.1.2]: https://github.com/distributed-lab/web-kit/compare/0.1.1...0.1.2
2 changes: 1 addition & 1 deletion packages/jac/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@distributedlab/jac",
"version": "0.1.4",
"version": "0.1.5",
"description": "A library for constructing JSON-API compliant requests and responses",
"repository": {
"type": "git",
2 changes: 1 addition & 1 deletion packages/tools/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@distributedlab/tools",
"version": "0.1.4",
"version": "0.1.5",
"description": "Collection of common utility functions and classes",
"repository": {
"type": "git",
11 changes: 9 additions & 2 deletions packages/tools/src/bn.test.ts
Original file line number Diff line number Diff line change
@@ -31,6 +31,13 @@ describe('performs BN unit test', () => {
'6000000000000000000',
)
})

test('multiply should drop decimal part if value overflows decimals', () => {
expect(
BN.fromBigInt('194287666397830', 18).mul(BN.fromRaw(1.02, 18)).value,
).toBe('198173419725786')
})

test('divide should return correct value', () => {
expect(BN.fromRaw(2, 1).div(BN.fromRaw(3, 1)).value).toBe('6')
expect(BN.fromRaw(2, 18).div(BN.fromRaw(3, 16)).value).toBe(
@@ -109,11 +116,11 @@ describe('performs BN unit test', () => {
})

describe('performs values', () => {
test('value getter should return correct uint value', () => {
test('performs value, getter should return correct uint value', () => {
expect(BN.fromRaw(1, 18).value).toBe('1000000000000000000')
})

test('toString should return correct humanized value and not mutate value', () => {
test('preforms toString, should return correct humanized value and not mutate value', () => {
expect(BN.fromRaw(1, 18).toString()).toBe('1')
expect(BN.fromRaw(1, 18).value).toBe('1000000000000000000')
})
8 changes: 5 additions & 3 deletions packages/tools/src/bn.ts
Original file line number Diff line number Diff line change
@@ -89,7 +89,7 @@ export class BN {

static #getGreatestDecimal(...args: BN[]): BN {
return args.find(
el => el.cfg.decimals === Math.max(...args.map(el => el.cfg.decimals)),
el => el.decimals === Math.max(...args.map(el => el.decimals)),
)!
}

@@ -181,7 +181,8 @@ export class BN {
return new BN(
numA.bn
.multipliedBy(numB.bn)
.dividedBy(BN.#makeTenPower(greatestDecimals)),
.dividedBy(BN.#makeTenPower(greatestDecimals))
.toFixed(0), // Remove decimal part after dividing on power of ten
{
...this.#cfg,
decimals: greatestDecimals,
@@ -200,7 +201,8 @@ export class BN {
return new BN(
numA.bn
.dividedBy(numB.bn)
.multipliedBy(BN.#makeTenPower(greatestDecimals)),
.multipliedBy(BN.#makeTenPower(greatestDecimals))
.toFixed(0), // Remove decimal part after multiplying on power of ten
{
...this.#cfg,
decimals: greatestDecimals,