Skip to content

Commit

Permalink
Add typescript support
Browse files Browse the repository at this point in the history
  • Loading branch information
the-icarus authored and davidyuk committed Mar 25, 2021
1 parent e295ec9 commit abde033
Show file tree
Hide file tree
Showing 8 changed files with 385 additions and 37 deletions.
5 changes: 4 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"presets": ["@babel/preset-env"],
"presets": [
"@babel/preset-env",
"@babel/preset-typescript"
],
"plugins": [
"@babel/plugin-proposal-object-rest-spread",
"@babel/plugin-transform-runtime",
Expand Down
16 changes: 8 additions & 8 deletions es/utils/amount-formatter.js → es/utils/amount-formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
* @module @aeternity/aepp-sdk/es/utils/amount-formatter
* @example import { format, toAettos, AE_AMOUNT_FORMATS } from '@aeternity/aepp-sdk/es/utils/amount-formatter'
*/
import { asBigNumber, isBigNumber } from './bignumber'
import BigNumber from 'bignumber.js'
import { asBigNumber, isBigNumber } from './bignumber'

/**
* AE amount formats
Expand Down Expand Up @@ -56,7 +56,7 @@ export const DENOMINATION_MAGNITUDE = {
* @param {String} [options.denomination='aettos'] denomination of amount, can be ['ae', 'aettos']
* @return {String}
*/
export const toAe = (value, { denomination = AE_AMOUNT_FORMATS.AETTOS } = {}) => formatAmount(value, { denomination, targetDenomination: AE_AMOUNT_FORMATS.AE })
export const toAe = (value: string | number | BigNumber, { denomination = AE_AMOUNT_FORMATS.AETTOS } = {}) => formatAmount(value, { denomination, targetDenomination: AE_AMOUNT_FORMATS.AE })

/**
* Convert amount to aettos
Expand All @@ -65,7 +65,7 @@ export const toAe = (value, { denomination = AE_AMOUNT_FORMATS.AETTOS } = {}) =>
* @param {String} [options.denomination='ae'] denomination of amount, can be ['ae', 'aettos']
* @return {String}
*/
export const toAettos = (value, { denomination = AE_AMOUNT_FORMATS.AE } = {}) => formatAmount(value, { denomination })
export const toAettos = (value: string | number | BigNumber, { denomination = AE_AMOUNT_FORMATS.AE } = {}) => formatAmount(value, { denomination })

/**
* Convert amount from one to other denomination
Expand All @@ -75,7 +75,7 @@ export const toAettos = (value, { denomination = AE_AMOUNT_FORMATS.AE } = {}) =>
* @param {String} [options.targetDenomination='aettos'] target denomination, can be ['ae', 'aettos']
* @return {String}
*/
export const formatAmount = (value, { denomination = AE_AMOUNT_FORMATS.AETTOS, targetDenomination = AE_AMOUNT_FORMATS.AETTOS } = {}) => {
export const formatAmount = (value: string | number | BigNumber, { denomination = AE_AMOUNT_FORMATS.AETTOS, targetDenomination = AE_AMOUNT_FORMATS.AETTOS } = {}) => {
if (!Object.values(AE_AMOUNT_FORMATS).includes(denomination)) throw new Error(`Invalid denomination. Current: ${denomination}, available [${Object.keys(AE_AMOUNT_FORMATS)}]`)
if (!Object.values(AE_AMOUNT_FORMATS).includes(targetDenomination)) throw new Error(`Invalid target denomination. Current: ${targetDenomination}, available [${Object.keys(AE_AMOUNT_FORMATS)}]`)
if (!isBigNumber(value)) throw new Error(`Value ${value} is not type of number`)
Expand All @@ -92,14 +92,14 @@ const prefixes = [
{ name: 'pico', magnitude: -12 }
]

const getNearestPrefix = exponent => prefixes.reduce((p, n) => (
const getNearestPrefix = (exponent: number) => prefixes.reduce((p, n) => (
Math.abs(n.magnitude - exponent) < Math.abs(p.magnitude - exponent) ? n : p))

const getLowerBoundPrefix = exponent => prefixes
const getLowerBoundPrefix = (exponent: number) => prefixes
.find(p => p.magnitude <= exponent) || prefixes[prefixes.length - 1]

export default (value) => {
if (!BigNumber.isBigNumber(value)) value = BigNumber(value)
export default (value: string | number | BigNumber) => {
if (!BigNumber.isBigNumber(value)) value = new BigNumber(value)

const { name, magnitude } = (value.e < 0 ? getNearestPrefix : getLowerBoundPrefix)(value.e)
const v = value
Expand Down
20 changes: 5 additions & 15 deletions es/utils/bignumber.js → es/utils/bignumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,25 @@ import BigNumber from 'bignumber.js'
* @param {String|Number|BigNumber} number number to convert
* @return {String}
*/
export function parseBigNumber (number) {
return BigNumber(number.toString()).toString(10)
}
export const parseBigNumber = (number: string | number | BigNumber) => new BigNumber(number.toString()).toString(10)

/**
* Convert number to BigNumber instance
* @param {String|Number|BigNumber} number number to convert
* @return {BigNumber}
*/
export function asBigNumber (number) {
return BigNumber(number.toString())
}
export const asBigNumber = (number: string | number | BigNumber) => new BigNumber(number.toString())

/**
* Check if value is BigNumber, Number or number string representation
* @param {String|Number|BigNumber} number number to convert
* @return {Boolean}
*/
export function isBigNumber (number) {
return !isNaN(number) || Number.isInteger(number) || BigNumber.isBigNumber(number)
}
export const isBigNumber = (number: string | number | BigNumber) => !isNaN(number as number) || Number.isInteger(number) || BigNumber.isBigNumber(number)

/**
* BigNumber ceil operation
* @param {String|Number|BigNumber} bigNumber
* @param {BigNumber} bigNumber
* @return {BigNumber}
*/
export function ceil (bigNumber) {
return bigNumber.integerValue(BigNumber.ROUND_CEIL)
}

export default { ceil, isBigNumber, asBigNumber, parseBigNumber }
export const ceil = (bigNumber: BigNumber) => bigNumber.integerValue(BigNumber.ROUND_CEIL)
Loading

0 comments on commit abde033

Please sign in to comment.