UNPKG

@signumjs/util

Version:

Useful utilities and tools for building Signum Network applications

197 lines (196 loc) 5.52 kB
/** * Original work Copyright (c) 2020 Burst Apps Team * Modified work Copyright (c) 2022 Signum Network */ import BigNumber from 'bignumber.js'; /** * Structure to determine the representation format of [Amount] string * * @category value-objects */ export interface AmountFormat { /** * string to prepend, Default: CurrencySymbol */ prefix: string; /** * Decimal separator, Default: '.' */ decimalSeparator: string; /** * grouping separator of the integer part, Default: ',' */ groupSeparator: string; /** * Primary grouping size of the integer part, Default: 3 */ groupSize: number; /** * Secondary grouping size of the integer part, Default 0 */ secondaryGroupSize: number; /** * Grouping separator of the fraction part, Default: '' */ fractionGroupSeparator: string; /** * Grouping size of the fraction part, Default: 0 */ fractionGroupSize: number; /** * String to append, Default: '' */ suffix: string; } /** * Amount formatting preset for dot decimal formatting 'Ꞩ 1,000,000.123456' * * @category value-objects */ export declare const FormatDotDecimal: AmountFormat; /** * Amount formatting preset for comma decimal formatting 'Ꞩ 1.000.000,123456' * * @category value-objects */ export declare const FormatCommaDecimal: AmountFormat; /** * Amount formatting presets, see {@link Amount.toString} * * @category value-objects */ export declare const AmountFormats: { /** * 1,000,000.123456 */ DotDecimal: AmountFormat; /** * 1.000.000,123456 */ CommaDecimal: AmountFormat; }; /** * A Value Object to facilitate SIGNA and Planck conversions/calculations. * * This class is a convenient wrapper around {@link ChainValue} with `decimals = 8` * * Note: This class uses a big number representation (ES5 compatible) under the hood, so * number limits and numeric calculations are much more precise than JS number type * * @category value-objects */ export declare class Amount { private readonly _value; private constructor(); /** * @return The Signa Currency Symbol * @deprecated Due to Multiverse feature it's not recommended to use this hard coded stuff. * */ static CurrencySymbol(): string; /** * Same as `Amount.fromPlanck(0)` or `Amount.fromSigna(0)` */ static Zero(): Amount; /** * Creates a Burst Value object from Planck * @param planck The value in Planck */ static fromPlanck(planck: number | string): Amount; /** * Creates a Value object from SIGNA * @param signa The value in SIGNA */ static fromSigna(signa: number | string): Amount; /** * Leaky value getter * @return the underlying value in its big number representation (immutable) */ getRaw(): BigNumber; /** * @return Gets Planck representation */ getPlanck(): string; /** * Sets value as Planck, i.e. overwrites current hold value * @param p The planck value * @return This value object */ setPlanck(p: string): Amount; /** * Gets SIGNA representation * @return value in SIGNA */ getSigna(): string; /** * Sets value as SIGNA, i.e. overwrites current hold value * @param b value in SIGNA * @return This value object */ setSigna(b: string): Amount; /** * Checks for equality * @param amount The other value to be compared * @return true if equal, otherwise false */ equals(amount: Amount): boolean; /** * Checks for lesser or equality * @param amount The other value to be compared * @return true if less or equal, otherwise false */ lessOrEqual(amount: Amount): boolean; /** * Checks for lesser value * @param amount The other value to be compared * @return true if less, otherwise false */ less(amount: Amount): boolean; /** * Checks for greater or equality value * @param amount The other value to be compared * @return true if greater or equal, otherwise false */ greaterOrEqual(amount: Amount): boolean; /** * Checks for greater value * @param amount The other value to be compared * @return true if greater, otherwise false */ greater(amount: Amount): boolean; /** * Adds two values * @param amount The other value to be added * @return the _mutated_ Amount object */ add(amount: Amount): Amount; /** * Subtracts from value * @param amount The other value to be subtracted * @return the _mutated_ Amount object */ subtract(amount: Amount): Amount; /** * Multiplies with a _numeric_ value (not Amount) * @param value A numeric value to be multiplied with * @return the _mutated_ Amount object */ multiply(value: number): Amount; /** * Divides by a _numeric_ value (not Amount) * @param value A numeric value to be divided by * @return the _mutated_ Amount object */ divide(value: number): Amount; /** * Gets a string representation in form `Ꞩ 100` * @param format The format object, Default: {@link AmountFormats.DotDecimal} * @return The formatted string */ toString(format?: AmountFormat): string; /** * Clones/Copies the current Amount to a new object * @return new Amount instance */ clone(): Amount; }