UNPKG

@type-ddd/money

Version:

This package provides TypeScript type definitions for handling Money in Domain-Driven Design contexts

297 lines (296 loc) 13 kB
import { Result, ValueObject } from 'rich-domain'; export declare enum Currencies { USD = "USD",// Dólar Americano EUR = "EUR",// Euro JPY = "JPY",// Iene Japonês GBP = "GBP",// Libra Esterlina AUD = "AUD",// Dólar Australiano CAD = "CAD",// Dólar Canadense CHF = "CHF",// Franco Suíço CNY = "CNY",// Renminbi Chinês SEK = "SEK",// Coroa Sueca NZD = "NZD",// Dólar da Nova Zelândia KRW = "KRW",// Won Sul-Coreano SGD = "SGD",// Dólar de Singapura NOK = "NOK",// Coroa Norueguesa MXN = "MXN",// Peso Mexicano INR = "INR",// Rúpia Indiana BRL = "BRL",// Real Brasileiro RUB = "RUB",// Rublo Russo ZAR = "ZAR",// Rand Sul-Africano TRY = "TRY",// Lira Turca HKD = "HKD" } export declare enum Locales { 'en-US' = "en-US",// Inglês (Estados Unidos) 'en-GB' = "en-GB",// Inglês (Reino Unido) 'en-CA' = "en-CA",// Inglês (Canadá) 'en-AU' = "en-AU",// Inglês (Austrália) 'en-NZ' = "en-NZ",// Inglês (Nova Zelândia) 'fr-FR' = "fr-FR",// Francês (França) 'fr-CA' = "fr-CA",// Francês (Canadá) 'es-ES' = "es-ES",// Espanhol (Espanha) 'es-MX' = "es-MX",// Espanhol (México) 'pt-BR' = "pt-BR",// Português (Brasil) 'pt-PT' = "pt-PT",// Português (Portugal) 'de-DE' = "de-DE",// Alemão (Alemanha) 'it-IT' = "it-IT",// Italiano (Itália) 'ja-JP' = "ja-JP",// Japonês (Japão) 'ko-KR' = "ko-KR",// Coreano (Coreia do Sul) 'zh-CN' = "zh-CN",// Chinês (China) 'zh-TW' = "zh-TW",// Chinês (Taiwan) 'ru-RU' = "ru-RU" } type Locale = keyof typeof Locales; type Currency = keyof typeof Currencies; declare class Money extends ValueObject<number> { protected static readonly MESSAGE: string; private constructor(); value(): number; /** * @description Validate if provided value is a valid money value. * @param value The value to validate. * @returns A boolean indicating whether the provided value is a valid money value. */ static isValid(value: number): boolean; /** * @description Validate if provided value is a valid money value. * @param value The value to validate. * @returns A boolean indicating whether the provided value is a valid money value. */ static isValidProps(value: number): boolean; /** * @description Formats the money value as currency according to the specified currency and locale. * @param currency The currency code or enum representing the currency. Defaults to Brazilian Real (BRL). * @param locale The locale code or enum representing the locale. Defaults to Brazilian Portuguese (pt-BR). * @returns A string representing the money value formatted as currency. */ coin(currency?: Currency | Currencies, locale?: Locale | Locales): string; /** * @description Performs addition with another Money object or a numeric value. * @param money The Money object or numeric value to add. * @returns A new Money object representing the result of the addition. */ sum(money: Money | number): Money; /** * @description Performs subtraction with another Money object or a numeric value. * @param money The Money object or numeric value to subtract. * @returns A new Money object representing the result of the subtraction. */ subtract(money: Money | number): Money; /** * @description Performs multiplication with another Money object or a numeric value. * @param money The Money object or numeric value to multiply by. * @returns A new Money object representing the result of the multiplication. */ multiply(money: Money | number): Money; /** * @description Performs division with another Money object or a numeric value. * @param money The Money object or numeric value to divide by. * @returns A new Money object representing the result of the division. */ divide(money: Money | number): Money; /** * @description Adds a percentage to the money value. * @param percent The percentage to add. * @returns A new Money object representing the result of adding the percentage. */ addPercent(percent: number): Money; /** * @description Calculates the percentage of the money value. * @param percent The percentage to calculate. * @returns A new Money object representing the calculated percentage. */ percent(percent: number): Money; /** * @description Checks if the money value is greater than the provided value. * @param value The value to compare. * @returns A boolean indicating whether the money value is greater than the provided value. */ isGt(value: Money | number): boolean; /** * @description Checks if the money value is greater than or equal to the provided value. * @param value The value to compare. * @returns A boolean indicating whether the money value is greater than or equal to the provided value. */ isGte(value: Money | number): boolean; /** * @description Checks if the money value is less than the provided value. * @param value The value to compare. * @returns A boolean indicating whether the money value is less than the provided value. */ isLt(value: Money | number): boolean; /** * @description Checks if the money value is less than or equal to the provided value. * @param value The value to compare. * @returns A boolean indicating whether the money value is less than or equal to the provided value. */ isLte(value: Money | number): boolean; /** * @description Checks if the money value is equal to the provided value. * @param value The value to compare. * @returns A boolean indicating whether the money value is equal to the provided value. */ isEq(value: Money | number): boolean; /** * @description Checks if the money value is positive. * @returns A boolean indicating whether the money value is positive. */ isPos(): boolean; /** * @description Checks if the money value is negative. * @returns A boolean indicating whether the money value is negative. */ isNeg(): boolean; /** * @description Checks if the money value is zero. * @returns A boolean indicating whether the money value is zero. */ isZero(): boolean; /** * @description Makes the money value positive. * @returns A new Money object with a positive value. */ makePos(): Money; /** * @description Makes the money value negative. * @returns A new Money object with a negative value. */ makeNeg(): Money; /** * @description Creates a new Money object with a value of zero. * @returns A new Money object initialized with a value of zero. */ static zero(): Money; /** * @description Creates a new Money object with a value of one. * @returns A new Money object initialized with a value of one. */ static one(): Money; /** * @description Creates a new Money object with a value of ten. * @returns A new Money object initialized with a value of ten. */ static ten(): Money; /** * @description Creates a new Money object with a value of one hundred. * @returns A new Money object initialized with a value of one hundred. */ static one_hundred(): Money; /** * @description Creates a new Money object with a value of one thousand. * @returns A new Money object initialized with a value of one thousand. */ static one_thousand(): Money; /** * @description Performs summation of two values. * @param a The first value to sum, which can be a number or a Money object. * @param b The second value to sum, which can be a number or a Money object. * @returns The result of the summation as a number. */ static sum(a: number | Money, b: number | Money): number; /** * @description Performs division of two values. * @param a The numerator value, which can be a number or a Money object. * @param b The denominator value, which can be a number or a Money object. * @returns The result of the division as a number. */ static divide(a: number | Money, b: number | Money): number; /** * @description Performs multiplication of two values. * @param a The first value to multiply, which can be a number or a Money object. * @param b The second value to multiply, which can be a number or a Money object. * @returns The result of the multiplication as a number. */ static multiply(a: number | Money, b: number | Money): number; /** * @description Performs subtraction of two values. * @param a The minuend value, which can be a number or a Money object. * @param b The subtrahend value, which can be a number or a Money object. * @returns The result of the subtraction as a number. */ static subtract(a: number | Money, b: number | Money): number; /** * @description Rounds down the money value to the nearest integer. * @returns A new Money object representing the rounded-down value. */ floor(): Money; /** * @description Rounds up the money value to the nearest integer. * @returns A new Money object representing the rounded-up value. */ ceil(): Money; /** * @description Generates a closure that performs arithmetic operations with a predefined initial value. * @param initial The initial value for arithmetic operations. * @returns An object containing functions for performing `summation`, `division`, `multiplication`, and `subtraction` with the initial value. */ static closure(initial: number): { sum: (value: number) => number; divide: (value: number) => number; multiply: (value: number) => number; subtract: (value: number) => number; }; /** * @description Calculates the simple interest based on the provided rate and periods. * @param rate The interest rate as a percentage. * @param periods The number of periods over which the interest is applied. * @returns A new Money object representing the calculated interest. * @throws {Error} If either the rate or periods is negative. */ interest(rate: number, periods: number): Money; /** * @description Finds the maximum value among the provided Money objects. * @param values An array of Money objects. * @returns A new Money object representing the maximum value found. */ static max(values: Money[]): Money; /** * @description Finds the minimum value among the provided Money objects. * @param values An array of Money objects. * @returns A new Money object representing the minimum value found. */ static min(values: Money[]): Money; /** * @description Calculates the compound interest based on the provided rate and periods. * @param rate The interest rate as a percentage. * @param periods The number of periods over which the interest is applied. * @returns A new Money object representing the calculated compound interest. * @throws {Error} If either the rate or periods is negative. */ compoundInterest(rate: number, periods: number): Money; /** * @description Generates a random Money value within the specified range. * @param min The minimum value of the range. * @param max The maximum value of the range. * @returns A new Money object representing a random value within the specified range. */ static random(min: number, max: number): Money; /** * @description Calculates the average value among the provided Money objects. * @param values An array of Money objects. * @returns A new Money object representing the average value of the Money objects. */ static average(values: Money[]): Money; /** * @description Converts the current Money value to another currency using the provided exchange rate. * @param exchangeRate The exchange rate, which can be a Money object representing the rate or a number. * @returns A new Money object representing the converted value. */ convertTo(exchangeRate: Money | number): Money; /** * @description Initializes a Money object with the provided value. * @param value The initial value for the Money object. * @returns A new Money object initialized with the provided value. * @throws {Error} If the provided value is invalid. */ static init(value: number): Money; /** * @description Creates a Result object containing a Money object initialized with the provided value. * @param value The initial value for the Money object. * @returns A Result object containing either a Money object or an error message. */ static create(value: number): Result<Money | null>; } export { Money }; export default Money;