-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve math with formats/parsing unit related functions (#518)
- Loading branch information
1 parent
039f930
commit 658b065
Showing
9 changed files
with
327 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@fuel-ts/math": patch | ||
--- | ||
|
||
Added format/parsing helpers to `bn`. Now `bn` accepts `undefined` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const DEFAULT_PRECISION = 3; | ||
export const DEFAULT_MIN_PRECISION = 1; | ||
export const DECIMAL_UNITS = 9; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { DEFAULT_MIN_PRECISION, DEFAULT_PRECISION } from './constants'; | ||
import type { ToFixedConfig } from './types'; | ||
|
||
export function toFixed(value?: string | number, options?: ToFixedConfig) { | ||
const { precision = DEFAULT_PRECISION, minPrecision = DEFAULT_MIN_PRECISION } = options || {}; | ||
|
||
const [valueUnits = '0', valueDecimals = '0'] = String(value || '0.0').split('.'); | ||
const groupRegex = /(\d)(?=(\d{3})+\b)/g; | ||
const units = valueUnits.replace(groupRegex, '$1,'); | ||
let decimals = valueDecimals.slice(0, precision); | ||
|
||
// strip traling zeros limited by minPrecision | ||
if (minPrecision < precision) { | ||
const firstNonZero = decimals.match(/[1-9]/); | ||
const firstNonZeroIndex = firstNonZero?.index == null ? -1 : firstNonZero.index; | ||
const keepChars = Math.max(minPrecision, firstNonZeroIndex + 1); | ||
decimals = decimals.slice(0, keepChars); | ||
} | ||
|
||
const decimalPortion = decimals ? `.${decimals}` : ''; | ||
return `${units}${decimalPortion}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* Simple tests to make sure functional shortcuts are working. | ||
* | ||
* Deeper tests are in ./bn.test.ts | ||
*/ | ||
|
||
import { format, formatUnits, toBytes, toHex, toNumber } from './functional'; | ||
|
||
describe('Math - Functional shortcuts', () => { | ||
it('should toNumber return a number', () => { | ||
expect(toNumber('50000')).toEqual(50000); | ||
}); | ||
it('should toHex return a Hex string', () => { | ||
expect(toHex('50000')).toEqual('0xc350'); | ||
}); | ||
it('should toBytes return a bytes array (Uint8Array)', () => { | ||
expect(JSON.stringify(toBytes('50000'))).toEqual(JSON.stringify({ 0: 195, 1: 80 })); | ||
}); | ||
it('should formatUnits return a formatted string', () => { | ||
expect(formatUnits('1000000000')).toEqual('1.000000000'); | ||
}); | ||
it('should format return a formatted string', () => { | ||
expect(format('1000000000')).toEqual('1.0'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
export * from './convert'; | ||
export * from './types'; | ||
export * from './math'; | ||
export * from './bn'; | ||
export * from './constants'; | ||
export * from './decimal'; | ||
export * from './functional'; | ||
export * from './math'; | ||
export * from './types'; |
Oops, something went wrong.
658b065
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage report
Test suite run success
515 tests passing in 46 suites.
Report generated by 🧪jest coverage report action from 658b065