Skip to content

Commit

Permalink
add number format utils
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjah committed Aug 23, 2024
1 parent 06ec797 commit 867fab1
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './isNode'
export * from './networks'
export * from './events'
export * from './maths'
export * from './numberFormat'
91 changes: 91 additions & 0 deletions src/utils/numberFormat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { NB_DECIMALS } from '../basicElements/mas'

/**
* Divides a number by a given exponent of base 10 (10exponent), and formats it into a string representation of the number.
*
* This code snippet is taken from the Viem SDK.
* - Docs: https://viem.sh/docs/utilities/formatUnits
*
* @example
*
* formatUnits(420000000000n, 9)
* // '420'
*/
export function formatUnits(value: bigint, decimals: number): string {
let display = value.toString()

const negative = display.startsWith('-')
if (negative) display = display.slice(1)

display = display.padStart(decimals, '0')

// eslint-disable-next-line prefer-const
let [integer, fraction] = [
display.slice(0, display.length - decimals),
display.slice(display.length - decimals),
]
fraction = fraction.replace(/(0+)$/, '')
return `${negative ? '-' : ''}${integer || '0'}${
fraction ? `.${fraction}` : ''
}`
}

export function formatMas(value: bigint): string {
return formatUnits(value, NB_DECIMALS)
}

/**
* Multiplies a string representation of a number by a given exponent of base 10 (10exponent).
*
* This code snippet is taken from the Viem SDK.
* - Docs: https://viem.sh/docs/utilities/parseUnits
*
* @example
* import { parseUnits } from 'viem'
*
* parseUnits('420', 9)
* // 420000000000n
*/
export function parseUnits(value: string, decimals: number): bigint {
let [integer, fraction = '0'] = value.split('.')

const negative = integer.startsWith('-')
if (negative) integer = integer.slice(1)

// trim trailing zeros.
fraction = fraction.replace(/(0+)$/, '')

// round off if the fraction is larger than the number of decimals.
if (decimals === 0) {
if (Math.round(Number(`.${fraction}`)) === 1)
integer = `${BigInt(integer) + 1n}`
fraction = ''
} else if (fraction.length > decimals) {
const [left, unit, right] = [
fraction.slice(0, decimals - 1),
fraction.slice(decimals - 1, decimals),
fraction.slice(decimals),
]

const rounded = Math.round(Number(`${unit}.${right}`))
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
if (rounded > 9)
fraction = `${BigInt(left) + BigInt(1)}0`.padStart(left.length + 1, '0')
else fraction = `${left}${rounded}`

if (fraction.length > decimals) {
fraction = fraction.slice(1)
integer = `${BigInt(integer) + 1n}`
}

fraction = fraction.slice(0, decimals)
} else {
fraction = fraction.padEnd(decimals, '0')
}

return BigInt(`${negative ? '-' : ''}${integer}${fraction}`)
}

export function parseMas(value: string): bigint {
return parseUnits(value, NB_DECIMALS)
}

1 comment on commit 867fab1

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report for experimental massa-web3

St.
Category Percentage Covered / Total
🟡 Statements 63.05% 1109/1759
🔴 Branches 43.79% 187/427
🔴 Functions 46.19% 206/446
🟡 Lines 63.35% 1103/1741

Test suite run success

129 tests passing in 13 suites.

Report generated by 🧪jest coverage report action from 867fab1

Please sign in to comment.