UNPKG

decimal.js-i18n

Version:
955 lines (932 loc) 26.4 kB
/*! * Copyright (c) 2022 Pedro José Batista, licensed under the MIT License. * See the LICENSE.md file in the project root for more information. */ import Decimal from "decimal.js"; export { default as Decimal, default } from "decimal.js"; /** Formats of compact display available. Takes either "`short`" (default) or "`long`" */ declare type CompactDisplay = "short" | "long"; /** * The currency to use in currency formatting. Possible values are the ISO 4217 currency codes, such as "`USD`" * for the US dollar, "`EUR`" for the euro, or "`CNY`" for the Chinese RMB — see the [Current currency & funds * code list](https://iso.org/iso-4217-currency-codes.html). */ declare type Currency = | "AED" | "AFN" | "ALL" | "AMD" | "ANG" | "AOA" | "ARS" | "AUD" | "AWG" | "AZN" | "BAM" | "BBD" | "BDT" | "BGN" | "BHD" | "BIF" | "BMD" | "BND" | "BOB" | "BRL" | "BSD" | "BTN" | "BWP" | "BYR" | "BZD" | "CAD" | "CDF" | "CHF" | "CLP" | "CNY" | "COP" | "CRC" | "CUP" | "CVE" | "CZK" | "DJF" | "DKK" | "DOP" | "DZD" | "EEK" | "EGP" | "ERN" | "ETB" | "EUR" | "FJD" | "FKP" | "GBP" | "GEL" | "GHS" | "GIP" | "GMD" | "GNF" | "GTQ" | "GYD" | "HKD" | "HNL" | "HRK" | "HTG" | "HUF" | "IDR" | "ILS" | "INR" | "IQD" | "IRR" | "ISK" | "JMD" | "JOD" | "JPY" | "KES" | "KGS" | "KHR" | "KMF" | "KPW" | "KRW" | "KWD" | "KYD" | "KZT" | "LAK" | "LBP" | "LKR" | "LRD" | "LSL" | "LTL" | "LVL" | "LYD" | "MAD" | "MDL" | "MGA" | "MKD" | "MMK" | "MNT" | "MOP" | "MRO" | "MUR" | "MVR" | "MWK" | "MXN" | "MYR" | "MZN" | "NAD" | "NGN" | "NIO" | "NOK" | "NPR" | "NZD" | "OMR" | "PAB" | "PEN" | "PGK" | "PHP" | "PKR" | "PLN" | "PYG" | "QAR" | "RON" | "RSD" | "RUB" | "RWF" | "SAR" | "SBD" | "SCR" | "SDG" | "SEK" | "SGD" | "SHP" | "SLL" | "SOS" | "SRD" | "STD" | "SVC" | "SYP" | "SZL" | "THB" | "TJS" | "TND" | "TOP" | "TRY" | "TTD" | "TWD" | "TZS" | "UAH" | "UGX" | "USD" | "UYU" | "UZS" | "VEF" | "VES" | "VND" | "VUV" | "WST" | "XAF" | "XCD" | "XOF" | "XPF" | "YER" | "ZAR" | "ZMK" | "ZWD" | CurrencyString; declare type CurrencyString = string & {}; /** * How to display the currency in currency formatting. Possible values are: * * - "`symbol`" to use a localized currency symbol such as €; * - "`narrowSymbol`" to use a narrow format symbol ("$100" rather than "US$100"); * - "`code`" to use the ISO currency code; * - "`name`" to use a localized currency name such as "dollar". */ declare type CurrencyDisplay = "symbol" | "narrowSymbol" | "code" | "name"; /** * In many locales, accounting format means to wrap the number with parentheses instead of appending a minus * sign. You can enable this formatting by setting the currencySign option to "`accounting`". The default value * is "`standard`". */ declare type CurrencySign = "standard" | "accounting"; /** * The formatting that should be displayed for the number, the defaults is "`standard`". * * - "`standard`" plain number formatting; * - "`scientific`" return the order-of-magnitude for formatted number; * - "`engineering`" return the exponent of ten when divisible by three; * - "`compact`" string representing exponent; defaults to using the "short" form. */ declare type Notation = "standard" | "scientific" | "engineering" | "compact"; /** * A numeral system is a system for expressing numbers. The `NumberingSystem` type helps to represent the * different numeral systems used by various countries, regions, and cultures around the world. * * See the [numberingSystem](https://mdn.io/Intl.Locale.prototype.numberingSystem) page for more information. */ declare type NumberingSystem = | "adlm" | "ahom" | "arab" | "arabext" | "armn" | "armnlow" | "bali" | "beng" | "bhks" | "brah" | "cakm" | "cham" | "cyrl" | "deva" | "ethi" | "finance" | "fullwide" | "geor" | "gong" | "gonm" | "grek" | "greklow" | "gujr" | "guru" | "hanidays" | "hanidec" | "hans" | "hansfin" | "hant" | "hantfin" | "hebr" | "hmng" | "hmnp" | "java" | "jpan" | "jpanfin" | "jpanyear" | "kali" | "khmr" | "knda" | "lana" | "lanatham" | "laoo" | "latn" | "lepc" | "limb" | "mathbold" | "mathdbl" | "mathmono" | "mathsanb" | "mathsans" | "mlym" | "modi" | "mong" | "mroo" | "mtei" | "mymr" | "mymrshan" | "mymrtlng" | "native" | "newa" | "nkoo" | "olck" | "orya" | "osma" | "rohg" | "roman" | "romanlow" | "saur" | "shrd" | "sind" | "sinh" | "sora" | "sund" | "takr" | "talu" | "taml" | "tamldec" | "telu" | "thai" | "tirh" | "tibt" | "traditio" | "vaii" | "wara" | "wcho" | NumberingSystemString; declare type NumberingSystemString = string & {}; /** * When to display the sign for the number: * * - "`always`" always display sign; * - "`auto`" sign display for negative numbers only; * - "`exceptZero`" sign display for positive and negative numbers, but not zero; * - "`negative`" sign display for negative numbers only, excluding negative zero; * - "`never`" never display sign. */ declare type SignDisplay = "auto" | "never" | "always" | "exceptZero" | "negative"; /** * The formatting style to use. * * - "`decimal`" for plain number formatting; * - "`currency`" for currency formatting; * - "`percent`" for percent formatting; * - "`unit`" for unit formatting. */ declare type Style = "decimal" | "currency" | "percent" | "unit"; /** * A string expressing the strategy for displaying trailing zeros on whole numbers. * * - "`auto`": keep trailing zeros according to `minimumFractionDigits` and `minimumSignificantDigits`; * - "`stripIfInteger`": the result with more precision wins a conflict; * - "`lessPrecision`": same as "auto", but remove the fraction digits if they are all zero. */ declare type TrailingZeroDisplay = "auto" | "stripIfInteger" | "lessPrecision"; /** * The unit to use in unit formatting, possible values are core unit identifiers. Only a [subset][1] of units * from the full list was selected for use in ECMAScript. Pairs of simple units can be concatenated with * "`-per-`" to make a compound unit. * * [1]: https://tc39.es/proposal-unified-intl-numberformat/section6/locales-currencies-tz_proposed_out.html#sec-issanctionedsimpleunitidentifier */ declare type Unit = | "acre" | "bit" | "byte" | "celsius" | "centimeter" | "day" | "degree" | "fahrenheit" | "fluid-ounce" | "foot" | "gallon" | "gigabit" | "gigabyte" | "gram" | "hectare" | "hour" | "inch" | "kilobit" | "kilobyte" | "kilogram" | "kilometer" | "liter" | "megabit" | "megabyte" | "meter" | "mile" | "mile-scandinavian" | "milliliter" | "millimeter" | "millisecond" | "minute" | "month" | "ounce" | "percent" | "petabyte" | "pound" | "second" | "stone" | "terabit" | "terabyte" | "week" | "yard" | "year" | UnitString; declare type UnitString = string & {}; /** * The unit formatting style to use in {@link unit} formatting, the defaults is "`short`". Can be "`long`", * "`narrow`" or "`short`". */ declare type UnitDisplay = "long" | "narrow" | "short"; /** * Whether to use grouping separators, such as thousands separators or thousand/lakh/crore separators. * * - "`always`": display grouping separators even if the locale prefers otherwise; * - "`auto`": display grouping separators based on the locale preference, which may also depend on the `currency`; * - "`false`"/`false`: do not display grouping separators; * - "`min2`": display grouping separators when there are at least 2 digits in a group; * - "`true`"/`true`: alias for always. */ declare type UseGrouping = boolean | "always" | "auto" | "false" | "min2" | "true"; /** * Base type of object used to configure {@link Decimal.Format} formatters. * * @template N Numeric notation of formatting. * @template S Numeric style of formatting. */ interface BaseFormatOptions<N extends Notation = "standard", S extends Style = "decimal"> { /** Only used when {@link notation `notation`} is "`compact`". */ compactDisplay: CompactDisplay; /** * The currency to use in currency formatting. Possible values are the ISO 4217 currency codes, such as * "`USD`" for the US dollar, "`EUR`" for the euro, or "`CNY`" for the Chinese RMB — see the [Current * currency & funds code list](https://iso.org/iso-4217-currency-codes.html). There is no default value; if * the {@link style} is "`currency`", the `currency` property must be provided. */ currency: S extends "currency" ? Currency : undefined; /** How to display the {@link currency `currency`} in currency formatting. The default is "`symbol`". */ currencyDisplay: CurrencyDisplay; /** * In many locales, accounting format means to wrap the number with parentheses instead of appending a * minus sign. You can enable this formatting by setting the currencySign option to "`accounting`". The * default value is "`standard`". */ currencySign: CurrencySign; /** * The maximum number of fraction digits to use. This allows any positive integer value up to `999999999`, * including; the default for plain number formatting is the larger of * {@link minimumFractionDigits `minimumFractionDigits`} and `3`; the default for currency formatting is the * larger of `minimumFractionDigits` and the number of minor unit digits provided by the [ISO 4217 currency * code list](https://www.currency-iso.org/en/home/tables/table-a1.html) (`2` if the list doesn't provide * that information); the default for percent formatting is the larger of `minimumFractionDigits` and `0`. */ maximumFractionDigits: number; /** * The maximum number of significant digits to use. This allows any positive natural value up to * `1000000000`, including; the default for plain number formatting is the larger of * {@link minimumSignificantDigits `minimumSignificantDigits`} and `21`. */ maximumSignificantDigits: number; /** * The minimum number of fraction digits to use. This allows any positive integer value up to `999999999`, * including; the default for plain number and percent formatting is `0`; the default for * {@link currency `currency`} formatting is the number of minor unit digits provided by the [ISO 4217 * currency code list](https://www.currency-iso.org/en/home/tables/table-a1.html) (`2` if the list doesn't * provide that information). */ minimumFractionDigits: number; /** * The minimum number of integer digits to use. This allows any positive natural value up to `1000000000`, * including; the default is `1`. */ minimumIntegerDigits: number; /** * The minimum number of significant digits to use. This allows any positive natural value up to * `1000000000`, including; the default is `1`. */ minimumSignificantDigits: number; /** The formatting that should be displayed for the number, the defaults is "`standard`". */ notation: N; /** * A numeral system is a system for expressing numbers. The numberingSystem property helps to represent the * different numeral systems used by various countries, regions, and cultures around the world. * * See [the MDN page](https://mdn.io/Intl.Locale.prototype.numberingSystem) for more information. */ numberingSystem: NumberingSystem; /** * Options for rounding modes reflecting the [ICU user guide][1]. Used in this plugin as in * [decimal.js](https://mikemcl.github.io/decimal.js/#modes). * * [1]: https://unicode-org.github.io/icu/userguide/format_parse/numbers/rounding-modes.html */ rounding: Decimal.Rounding; /** When to display the sign for the number; defaults to "`auto`". */ signDisplay: SignDisplay; /** The formatting style to use. The default is "`decimal`". */ style: S; /** * The unit to use in unit formatting, possible values are core unit identifiers. There is no default * value; if the {@link style} is "`unit`", the `unit` property must be provided. */ unit: S extends "unit" ? Unit : undefined; /** * The unit formatting style to use in {@link unit} formatting, the defaults is "`short`". Can be "`long`", * "`narrow`" or "`short`". */ unitDisplay: UnitDisplay; /** * Whether to use grouping separators, such as thousands separators or thousand/lakh/crore separators. The * default is "`auto`". */ useGrouping: UseGrouping; /** A string expressing the strategy for displaying trailing zeros on whole numbers. The default is "`auto`". */ trailingZeroDisplay: TrailingZeroDisplay; } /** * A string with a BCP 47 language tag, or an array of such strings. For the general form and interpretation of * the locales argument, see the [Intl page on * MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl). */ declare type Locale = | "af-ZA" | "am-ET" | "ar-AE" | "ar-BH" | "ar-DZ" | "ar-EG" | "ar-IQ" | "ar-JO" | "ar-KW" | "ar-LB" | "ar-LY" | "ar-MA" | "arn-CL" | "ar-OM" | "ar-QA" | "ar-SA" | "ar-SD" | "ar-SY" | "ar-TN" | "ar-YE" | "as-IN" | "az-az" | "az-Cyrl-AZ" | "az-Latn-AZ" | "ba-RU" | "be-BY" | "bg-BG" | "bn-BD" | "bn-IN" | "bo-CN" | "br-FR" | "bs-Cyrl-BA" | "bs-Latn-BA" | "ca-ES" | "co-FR" | "cs-CZ" | "cy-GB" | "da-DK" | "de-AT" | "de-CH" | "de-DE" | "de-LI" | "de-LU" | "dsb-DE" | "dv-MV" | "el-CY" | "el-GR" | "en-029" | "en-AU" | "en-BZ" | "en-CA" | "en-cb" | "en-GB" | "en-IE" | "en-IN" | "en-JM" | "en-MT" | "en-MY" | "en-NZ" | "en-PH" | "en-SG" | "en-TT" | "en-US" | "en-ZA" | "en-ZW" | "es-AR" | "es-BO" | "es-CL" | "es-CO" | "es-CR" | "es-DO" | "es-EC" | "es-ES" | "es-GT" | "es-HN" | "es-MX" | "es-NI" | "es-PA" | "es-PE" | "es-PR" | "es-PY" | "es-SV" | "es-US" | "es-UY" | "es-VE" | "et-EE" | "eu-ES" | "fa-IR" | "fi-FI" | "fil-PH" | "fo-FO" | "fr-BE" | "fr-CA" | "fr-CH" | "fr-FR" | "fr-LU" | "fr-MC" | "fy-NL" | "ga-IE" | "gd-GB" | "gd-ie" | "gl-ES" | "gsw-FR" | "gu-IN" | "ha-Latn-NG" | "he-IL" | "hi-IN" | "hr-BA" | "hr-HR" | "hsb-DE" | "hu-HU" | "hy-AM" | "id-ID" | "ig-NG" | "ii-CN" | "in-ID" | "is-IS" | "it-CH" | "it-IT" | "iu-Cans-CA" | "iu-Latn-CA" | "iw-IL" | "ja-JP" | "ka-GE" | "kk-KZ" | "kl-GL" | "km-KH" | "kn-IN" | "kok-IN" | "ko-KR" | "ky-KG" | "lb-LU" | "lo-LA" | "lt-LT" | "lv-LV" | "mi-NZ" | "mk-MK" | "ml-IN" | "mn-MN" | "mn-Mong-CN" | "moh-CA" | "mr-IN" | "ms-BN" | "ms-MY" | "mt-MT" | "nb-NO" | "ne-NP" | "nl-BE" | "nl-NL" | "nn-NO" | "no-no" | "nso-ZA" | "oc-FR" | "or-IN" | "pa-IN" | "pl-PL" | "prs-AF" | "ps-AF" | "pt-BR" | "pt-PT" | "qut-GT" | "quz-BO" | "quz-EC" | "quz-PE" | "rm-CH" | "ro-mo" | "ro-RO" | "ru-mo" | "ru-RU" | "rw-RW" | "sah-RU" | "sa-IN" | "se-FI" | "se-NO" | "se-SE" | "si-LK" | "sk-SK" | "sl-SI" | "sma-NO" | "sma-SE" | "smj-NO" | "smj-SE" | "smn-FI" | "sms-FI" | "sq-AL" | "sr-BA" | "sr-CS" | "sr-Cyrl-BA" | "sr-Cyrl-CS" | "sr-Cyrl-ME" | "sr-Cyrl-RS" | "sr-Latn-BA" | "sr-Latn-CS" | "sr-Latn-ME" | "sr-Latn-RS" | "sr-ME" | "sr-RS" | "sr-sp" | "sv-FI" | "sv-SE" | "sw-KE" | "syr-SY" | "ta-IN" | "te-IN" | "tg-Cyrl-TJ" | "th-TH" | "tk-TM" | "tlh-QS" | "tn-ZA" | "tr-TR" | "tt-RU" | "tzm-Latn-DZ" | "ug-CN" | "uk-UA" | "ur-PK" | "uz-Cyrl-UZ" | "uz-Latn-UZ" | "uz-uz" | "vi-VN" | "wo-SN" | "xh-ZA" | "yo-NG" | "zh-CN" | "zh-HK" | "zh-MO" | "zh-SG" | "zh-TW" | "zu-ZA" | LocaleString; declare type LocaleString = string & {}; /** String that describes the approach taken by the formatters in order to achieve a target locale. */ declare type LocaleMatcher = "best fit" | "lookup"; /** * Describes the resolved configuration of a formatter instance, obtained via `Decimal.Format.resolvedOptions`. * * @template N Numeric notation of formatting. * @template S Numeric style of formatting. */ interface ResolvedFormatOptions<N extends Notation = "standard", S extends Style = "decimal"> extends Partial<BaseFormatOptions<N, S>> { minimumIntegerDigits: number; notation: N; numberingSystem: NumberingSystem; rounding: Decimal.Rounding; signDisplay: SignDisplay; style: S; useGrouping: UseGrouping; trailingZeroDisplay: TrailingZeroDisplay; compactDisplay: N extends "compact" ? CompactDisplay : undefined; currency: S extends "currency" ? Currency : undefined; currencyDisplay: S extends "currency" ? CurrencyDisplay : undefined; currencySign: S extends "currency" ? CurrencySign : undefined; unit: S extends "unit" ? Unit : undefined; unitDisplay?: S extends "unit" ? UnitDisplay : undefined; /** * A string with a [BCP 47](https://www.rfc-editor.org/info/bcp47) language tag. * * For the general form and interpretation of this property, see the [Intl page on * MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl). */ locale: string; } /** * Object used to configure a `Decimal.Format` object; the following properties fall into two groups: * * - {@link minimumIntegerDigits}, {@link minimumFractionDigits}, and {@link maximumFractionDigits} in one group; * - {@link minimumSignificantDigits} and {@link maximumSignificantDigits} in the other. * * If at least one property from the second group is defined, then the first group is ignored. * * @template N Numeric notation of formatting. * @template S Numeric style of formatting. */ interface FormatOptions<N extends Notation = "standard", S extends Style = "decimal"> extends Partial<BaseFormatOptions<N, S>> { /** * The locale matching algorithm to use. Possible values are "`lookup`" and "`best fit`"; the default is * "`best fit`". For information about this option, see the [Intl page on * MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl). */ localeMatcher?: LocaleMatcher | undefined; } /** Strings describing the type of parts available as result of `Format.formatToParts`. */ declare type FormatPartTypes = | "literal" | "nan" | "infinity" | "percent" | "integer" | "group" | "decimal" | "fraction" | "plusSign" | "minusSign" | "percentSign" | "currency" | "code" | "symbol" | "name" | "unit" | "exponentInteger" | "exponentMinusSign" | "exponentSeparator" | "unknown"; /** Object used to describe a single transliteration part of `Decimal.Format.formatToParts`. */ declare type FormatPart = PartType & PartValue; /** Fragment of a part containing the type. */ interface PartType { /** A string describing the formatting part. */ type: Intl.NumberFormatPartTypes | FormatPartTypes; } /** Fragment of a part containing the value. */ interface PartValue { /** Localized string with a fragment of the result. */ value: string; } /** * The `Decimal.Format` object enables language-sensitive decimal number formatting. It is entirely based on * `Intl.NumberFormat`, with the options of the latter being 100% compatible with it. * * This class, however, extend the numeric digits constraints of `Intl.NumberFormat` from 21 to 1000000000 in * order to fully take advantage of the arbitrary-precision of `decimal.js`. * * @template N Numeric notation of formatting. * @template S Numeric style of formatting. */ declare class DecimalFormat<N extends Notation, S extends Style> { static readonly [Symbol.toPrimitive]: typeof Decimal.Format; readonly [Symbol.toStringTag] = "Decimal.Format"; /** * Formats a number according to the locale and formatting options of this {@link DecimalFormat} object. * * @param value A valid [Decimal.Value](https://mikemcl.github.io/decimal.js/#decimal) to format. * @returns Formatted localized string. */ readonly format: (value: Decimal.Value) => string; /** * Allows locale-aware formatting of strings produced by `Decimal.Format` formatters. * * @param value A valid [Decimal.Value](https://mikemcl.github.io/decimal.js/#decimal) to format. * @returns An array of objects containing the formatted number in parts. */ readonly formatToParts: (value: Decimal.Value) => FormatPart[]; /** * Returns a new object with properties reflecting the locale and number formatting options computed during * initialization of this {@link Decimal.Format} object. * * @returns A new object with properties reflecting the locale and number formatting options computed * during the initialization of this object. */ readonly resolvedOptions: () => ResolvedFormatOptions<N, S>; /** * Creates a new instance of the `Decimal.Format` object. * * @param locales A string with a [BCP 47](https://www.rfc-editor.org/info/bcp47) language tag, or an array * of such strings. * * For the general form and interpretation of this parameter, see the [Intl page on * MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl). * @param options Object used to configure the behavior of the string localization. * @throws `RangeError` when an invalid option is given. */ constructor(locales?: Locale | Locale[], options?: FormatOptions<N, S>); /** * Returns an array containing the default locales available to the environment, based on a default * dictionary of locales and regions. * * This method is non-standard method that is not available on `Intl` formatters. * * @returns Array of strings with the available locales. */ static supportedLocales(): Decimal.Format.Locale[]; /** * Returns an array containing those of the provided locales that are supported without having to fall back * to the runtime's default locale. * * @template TNotation Numeric notation of formatting. * @template TStyle Numeric style of formatting. * @param locales A string with a BCP 47 language tag, or an array of such strings. For the general form * and interpretation of the locales argument, see the [Intl page on * MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl). * @param options Object used to configure the behavior of the string localization. * @returns Array of strings with the available locales. */ static supportedLocalesOf<TNotation extends Notation = "standard", TStyle extends Style = "decimal">( locales: string | string[], options?: FormatOptions<TNotation, TStyle>, ): Decimal.Format.Locale[]; } declare namespace DecimalFormat { export type { BaseFormatOptions, CompactDisplay, Currency, CurrencyDisplay, CurrencySign, Locale, LocaleMatcher, Notation, NumberingSystem, FormatOptions, FormatPart, FormatPartTypes, ResolvedFormatOptions, SignDisplay, Style, TrailingZeroDisplay, Unit, UnitDisplay, UseGrouping, }; } declare module "decimal.js" { interface Decimal { /** * Returns a string with a language-sensitive representation of this decimal number. * * @template N Numeric notation of formatting. * @template S Numeric style of formatting. * @param locales A string with a [BCP 47](https://www.rfc-editor.org/info/bcp47) language tag, or an * array of such strings. * * For the general form and interpretation of this parameter, see the [Intl page on * MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl). * @param options Object used to configure the behavior of the string localization. * @returns A localized and formatted string. */ toLocaleString: <N extends Notation = "standard", S extends Style = "decimal">( locales?: Locale | Locale[], options?: FormatOptions<N, S>, ) => string; } namespace Decimal { /** * The `Decimal.Format` object enables language-sensitive decimal number formatting. It is entirely * based on `Intl.NumberFormat`, with the options of the latter being 100% compatible with it. * * This class, however, extend the numeric digits constraints of `Intl.NumberFormat` from 21 to * 1000000000 in order to fully take advantage of the arbitrary-precision of `decimal.js`. */ export { DecimalFormat }; } }