import BigNumber from "bignumber.js" ; /** ===========================================================================
 * Types & Config
 * ============================================================================
 */ /**
 * Constructor helper for the bignumber library constructor.
 */ export const toBigNumber = ( x ) => new BigNumber ( x ) ; /**
 * Determine if any value is `NaN`.
 */ export const valueIsNaN = ( value ) => { return toBigNumber ( value ) . isNaN ( ) ; } ; /**
 * Generic number type.
 */ export type GenericNumberType = string | number | BigNumber ; /**
 * Method overrides for the generic math functions in this file.
 */ interface MathMethodSignatures { ( a , b , targetConstructorFn ? ) : string ; ( a , b , targetConstructorFn ) : number ; ( a , b , targetConstructorFn ) : BigNumber ; } interface AddListSignatures { ( values , targetConstructorFn ? ) : string ; ( values , targetConstructorFn ) : number ; ( values , targetConstructorFn ) : BigNumber ; } type ComparatorMethodSignature = ( a : GenericNumberType , b : GenericNumberType ) => boolean ; type ExecutionMethod = ( a : BigNumber , b : BigNumber ) => BigNumber ; type ComparatorMethod = ( a : BigNumber , b : BigNumber ) => boolean ; /** ===========================================================================
 * Math Utils
 * ----------------------------------------------------------------------------
 * This file contains some helper utils to perform basic math operations on
 * values. Values in the application may be in various formats depending on
 * the data source and other factors (e.g. returned from some other function),
 * so this file provides some generic helpers which can accept flexibly typed
 * arguments, perform basic math, and then return specific return types based
 * on the consumer's needs.
 * ============================================================================
 */ /**
 * Accept various inputs but always convert to `BigNumber`, of fails.
 */ export const valueToBigNumber = ( i ) : BigNumber => { try { let input = i ; if ( typeof i === "string" ) { input = i . replace ( /,/g , "" ) ; } /**
         * Try to convert any input value to the BigNumber constructor. If this
         * fails or if the result is NaN, throw an error. Otherwise return the
         * BigNumber.
         */ const result = new BigNumber ( input ) ; if ( result . isNaN ( ) ) { throw new Error ( `Invalid number input received, original input: '${ i }'` ) ; } return result ; } catch ( err ) { throw new Error ( err . message ) ; } } ; /**
 * Helper which creates and evaluates a regular math method.
 */ const createMathMethod = ( fn ) => { return ( a , b , targetConstructor = String ) => { const firstArgument = toBigNumber ( a ) ; const secondArgument = toBigNumber ( b ) ; const result = fn ( firstArgument , secondArgument ) . toString ( ) ; return targetConstructor ( result ) ; } ; } ; /**
 * Helper which creates and evaluates a comparator method.
 */ const createComparatorMethod = ( comparator ) : ComparatorMethodSignature => { return ( a , b ) => { const firstArgument = toBigNumber ( a ) ; const secondArgument = toBigNumber ( b ) ; const result = comparator ( firstArgument , secondArgument ) ; return result ; } ; } ; /**
 * Add two numbers: e.g. a + b
 */ export const add = createMathMethod ( ( a , b ) => a . plus ( b ) ) ; /**
 * Add two numbers: e.g. a - b
 */ export const subtract = createMathMethod ( ( a , b ) => a . minus ( b ) ) ; /**
 * Multiply two numbers: e.g. a * b
 */ export const multiply = createMathMethod ( ( a , b ) => a . times ( b ) ) ; /**
 * Multiply two numbers: e.g. a / b
 */ export const divide = createMathMethod ( ( a , b ) => a . dividedBy ( b ) ) ; /**
 * Compare two numbers: e.g. a > b
 */ export const isGreaterThan = createComparatorMethod ( ( a , b ) => a . isGreaterThan ( b ) ) ; /**
 * Compare two numbers: e.g. a >= b
 */ export const isGreaterThanOrEqualTo = createComparatorMethod ( ( a , b ) => a . isGreaterThanOrEqualTo ( b ) ) ; /**
 * Compare two numbers: e.g. a < b
 */ export const isLessThan = createComparatorMethod ( ( a , b ) => a . isLessThan ( b ) ) ; /**
 * Compare two numbers: e.g. a <= b
 */ export const isLessThanOrEqualTo = createComparatorMethod ( ( a , b ) => a . isLessThanOrEqualTo ( b ) ) ; /**
 * Compare equality of two numbers: e.g. a <= b
 */ export const isEqual = createComparatorMethod ( ( a , b ) => a . isEqualTo ( b ) ) ; /** ===========================================================================
 * Higher Order Methods
 * ============================================================================
 */ /**
 * Add a list of values.
 */ export const addValuesInList = ( values , targetConstructor = String ) => { const convertValues = values . map ( toBigNumber ) ; const result = convertValues . reduce ( ( sum , value ) => { return add ( sum , value ) ; } , toBigNumber ( 0 ) ) ; return targetConstructor ( result ) ; } ;