import Decimal from "break_infinity.js" ; import type { DecimalSource } from "break_infinity.js" ; import { Settings } from "./settings" ; import { formatWithCommas , noSpecialFormatting , showCommas } from "./utils" ; export abstract class Notation { public abstract get name ( ) : string ; public format ( value , places = 0 , placesUnder1000 = 0 ) { if ( typeof value === "number" && ! Number . isFinite ( value ) ) { return this . infinite ; } const decimal = Decimal . fromValue_noAlloc ( value ) ; if ( decimal . exponent < - 300 ) { return decimal . sign ( ) < 0 ? this . formatVerySmallNegativeDecimal ( decimal . abs ( ) , placesUnder1000 ) : this . formatVerySmallDecimal ( decimal , placesUnder1000 ) ; } if ( decimal . exponent < 3 ) { const number = decimal . toNumber ( ) ; return number < 0 ? this . formatNegativeUnder1000 ( Math . abs ( number ) , placesUnder1000 ) : this . formatUnder1000 ( number , placesUnder1000 ) ; } if ( Settings . isInfinite ( decimal . abs ( ) ) ) { return decimal . sign ( ) < 0 ? this . negativeInfinite : this . infinite ; } return decimal . sign ( ) < 0 ? this . formatNegativeDecimal ( decimal . abs ( ) , places ) : this . formatDecimal ( decimal , places ) ; } public get negativeInfinite ( ) : string { return `-${ this . infinite }` ; } public get infinite ( ) : string { return "Infinite" ; } public formatVerySmallNegativeDecimal ( value , places ) { return `-${ this . formatVerySmallDecimal ( value , places ) }` ; } public formatVerySmallDecimal ( value , places ) { return this . formatUnder1000 ( value . toNumber ( ) , places ) ; } public formatNegativeUnder1000 ( value , places ) { return `-${ this . formatUnder1000 ( value , places ) }` ; } public formatUnder1000 ( value , places ) { return value . toFixed ( places ) ; } public formatNegativeDecimal ( value , places ) { return `-${ this . formatDecimal ( value , places ) }` ; } abstract formatDecimal ( value , places ) ; protected formatExponent ( exponent , precision = 3 , specialFormat = ( ( n , _ ) => n . toString ( ) ) ) { if ( noSpecialFormatting ( exponent ) ) { return specialFormat ( exponent , Math . max ( precision , 1 ) ) ; } if ( showCommas ( exponent ) ) { return formatWithCommas ( specialFormat ( exponent , 0 ) ) ; } return this . formatDecimal ( new Decimal ( exponent ) , precision ) ; } }