diff --git a/README.md b/README.md index 8ba5243..4e48f71 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # Currency Master [![npm package][npm-img]][npm-url] [![Build Status][build-img]][build-url] +[![Downloads][download-img]][npm-url] [![Issues][issues-img]][issues-url] [![Code Coverage][codecov-img]][codecov-url] [![Semantic Release][semantic-release-img]][semantic-release-url] @@ -93,6 +94,7 @@ This project is licensed under the MIT License. [build-url]:https://github.com/dev-ahmadbilal/currency-master/actions/workflows/release.yml [npm-img]:https://img.shields.io/npm/v/currency-master [npm-url]:https://www.npmjs.com/package/currency-master +[download-img]: https://badgen.net/npm/dt/currency-master [issues-img]:https://img.shields.io/github/issues/dev-ahmadbilal/currency-master [issues-url]:https://github.com/dev-ahmadbilal/currency-master/issues [codecov-img]:https://codecov.io/gh/dev-ahmadbilal/currency-master/branch/main/graph/badge.svg diff --git a/src/conversion-master.ts b/src/conversion-master.ts index 21c0ecd..1e72d7e 100644 --- a/src/conversion-master.ts +++ b/src/conversion-master.ts @@ -37,7 +37,7 @@ export class ConversionMaster { static async getCurrencyRates(currency: CurrencyEnum, date?: string): Promise { try { const formattedDate = date || ConversionMaster.formatDate(new Date()); - const url = `${API_BASE_URL}@${formattedDate}/v1/currencies/${currency}.json`; + const url = `${API_BASE_URL}@${formattedDate}/v1/currencies/${currency.toLowerCase()}.json`; const response = await fetch(url); const data: CurrencyApiResponse = await response.json(); return data[currency] as CurrencyRates; diff --git a/src/country-currency-code.ts b/src/country-currency-code.ts new file mode 100644 index 0000000..31611a8 --- /dev/null +++ b/src/country-currency-code.ts @@ -0,0 +1,80 @@ +import { CountryCurrencyMap } from './types/country-currency.map'; + +// Define a type for valid country codes (keys of the object) +type CountryCode = keyof typeof CountryCurrencyMap; + +export class CountryCurrencyCode { + // Object containing country codes as keys and currency codes as values + private static countryCurrencyMap = CountryCurrencyMap; + + /** + * Returns the currency code for a given country code. + * + * @param {string} countryCode - The ISO 3166-1 alpha-2 country code. + * @returns {string | null} - The ISO 4217 currency code or null if the country code is invalid. + * + * @example + * ```ts + * const currency = CountryCurrencyCode.getCurrencyByCountryCode('US'); + * console.log(currency); // Output: 'USD' + * + * const invalidCurrency = CountryCurrencyCode.getCurrencyByCountryCode('XYZ'); + * console.log(invalidCurrency); // Output: null + * ``` + */ + public static getCurrencyByCountryCode(countryCode: string): string | null { + const upperCaseCountryCode = countryCode.toUpperCase() as CountryCode; + return CountryCurrencyCode.countryCurrencyMap[upperCaseCountryCode] || null; + } + + /** + * Returns the list of country codes associated with a given currency code. + * + * @param {string} currencyCode - The ISO 4217 currency code. + * @returns {string[] | null} - An array of country codes that use this currency or null if none are found. + * + * @example + * ```ts + * const countries = CountryCurrencyCode.getCountriesByCurrencyCode('USD'); + * console.log(countries); // Output: ['US', 'UM'] + * ``` + */ + public static getCountriesByCurrencyCode(currencyCode: string): string[] | null { + const countries = Object.keys(CountryCurrencyCode.countryCurrencyMap).filter( + (country) => CountryCurrencyCode.countryCurrencyMap[country as CountryCode] === currencyCode.toUpperCase(), + ); + + return countries.length ? countries : null; + } + + /** + * Returns a list of all available country codes. + * + * @returns {string[]} - An array of all available country codes. + * + * @example + * ```ts + * const countries = CountryCurrencyCode.getAllCountryCodes(); + * console.log(countries); // Output: ['AF', 'AL', 'DZ', ...] + * ``` + */ + public static getAllCountryCodes(): string[] { + return Object.keys(CountryCurrencyCode.countryCurrencyMap); + } + + /** + * Returns a list of all unique currency codes. + * + * @returns {string[]} - An array of unique currency codes. + * + * @example + * ```ts + * const currencies = CountryCurrencyCode.getAllCurrencyCodes(); + * console.log(currencies); // Output: ['AFN', 'ALL', 'USD', ...] + * ``` + */ + public static getAllCurrencyCodes(): string[] { + const currencySet = new Set(Object.values(CountryCurrencyCode.countryCurrencyMap)); + return Array.from(currencySet); + } +} diff --git a/src/currency.ts b/src/currency.ts new file mode 100644 index 0000000..dec6de3 --- /dev/null +++ b/src/currency.ts @@ -0,0 +1,214 @@ +import { CurrencySettings } from './types/currency-settings'; + +const defaultSettings: CurrencySettings = { + symbol: '$', + separator: ',', + decimal: '.', + errorOnInvalid: false, + precision: 2, + pattern: '!#', + negativePattern: '-!#', + fromCents: false, + groups: /(\d)(?=(\d{3})+\b)/g, + format: formatCurrency, +}; + +class Currency { + public intValue: number; + public value: number; + private settings: CurrencySettings; + private precisionFactor: number; + + constructor(value: number | string | Currency, options?: Partial) { + this.settings = { ...defaultSettings, ...options }; + this.precisionFactor = Math.pow(10, this.settings.precision); + this.intValue = this.parseValue(value); + this.value = this.intValue / this.precisionFactor; + this.settings.increment ||= 1 / this.precisionFactor; + + if (this.settings.useVedic) { + this.settings.groups = /(\d)(?=(\d\d)+\d\b)/g; + } + } + + /** + * Parses the input value into an integer representation suitable for currency calculations. + * @param value - The value to parse (number, string, or Currency instance). + * @returns Parsed integer value. + */ + private parseValue(value: number | string | Currency): number { + if (value instanceof Currency) { + return value.intValue; + } + + if (typeof value === 'number') { + return Math.round(value * (this.settings.fromCents ? 1 : this.precisionFactor)); + } + + const regex = new RegExp('[^-\\d' + this.settings.decimal + ']', 'g'); + const cleanedValue = parseFloat( + value + .replace(/\((.*)\)/, '-$1') + .replace(regex, '') + .replace(this.settings.decimal, '.'), + ); + + if (isNaN(cleanedValue)) { + if (this.settings.errorOnInvalid) { + throw new Error('Invalid input'); + } + return 0; + } + + return Math.round(cleanedValue * (this.settings.fromCents ? 1 : this.precisionFactor)); + } + + /** + * Adds a specified amount to the currency value. + * @param amount - The amount to add (number, string, or Currency instance). + * @returns New Currency instance with the summed value. + * @example + * const currency = new Currency(10); + * const result = currency.add(5); // Result is a new Currency instance with value 15 + */ + add(amount: number | string | Currency): Currency { + const result = this.intValue + this.parseValue(amount); + return new Currency(result / this.precisionFactor, this.settings); + } + + /** + * Subtracts a specified amount from the currency value. + * @param amount - The amount to subtract (number, string, or Currency instance). + * @returns New Currency instance with the difference. + * @example + * const currency = new Currency(10); + * const result = currency.subtract(5); // Result is a new Currency instance with value 5 + */ + subtract(amount: number | string | Currency): Currency { + const result = this.intValue - this.parseValue(amount); + return new Currency(result / this.precisionFactor, this.settings); + } + + /** + * Multiplies the currency value by a specified factor. + * @param factor - The factor to multiply by. + * @returns New Currency instance with the multiplied value. + * @example + * const currency = new Currency(10); + * const result = currency.multiply(2); // Result is a new Currency instance with value 20 + */ + multiply(factor: number): Currency { + const result = this.intValue * factor; + return new Currency(result / this.precisionFactor, this.settings); + } + + /** + * Divides the currency value by a specified divisor. + * @param divisor - The divisor to divide by. + * @returns New Currency instance with the divided value. + * @example + * const currency = new Currency(10); + * const result = currency.divide(2); // Result is a new Currency instance with value 5 + */ + divide(divisor: number): Currency { + const result = this.intValue / divisor; + return new Currency(result / this.precisionFactor, this.settings); + } + + /** + * Distributes the currency value into an array of Currency instances evenly. + * @param parts - The number of parts to distribute into. + * @returns Array of Currency instances with the distributed values. + * @example + * const currency = new Currency(100); + * const parts = currency.distribute(3); // Returns an array of 3 Currency instances with values [33.34, 33.33, 33.33] + */ + distribute(parts: number): Currency[] { + const splitValue = Math.floor(this.intValue / parts); + const remainder = Math.abs(this.intValue - splitValue * parts); + const distributed = []; + + for (let i = 0; i < parts; i++) { + const amount = splitValue + (i < remainder ? Math.sign(this.intValue) : 0); + distributed.push(new Currency(amount / this.precisionFactor, this.settings)); + } + + return distributed; + } + + /** + * Returns the dollar portion of the currency value. + * @returns The dollar value as an integer. + * @example + * const currency = new Currency(123.45); + * const dollars = currency.dollars(); // Returns 123 + */ + dollars(): number { + return Math.floor(this.value); + } + + /** + * Returns the cent portion of the currency value. + * @returns The cent value as an integer. + * @example + * const currency = new Currency(123.45); + * const cents = currency.cents(); // Returns 45 + */ + cents(): number { + return Math.floor(this.intValue % this.precisionFactor); + } + + /** + * Formats the currency value as a string according to the formatting settings. + * @param options - Optional formatting options. + * @returns The formatted currency string. + * @example + * const currency = new Currency(1234.56); + * const formatted = currency.format(); // Returns '$1,234.56' + */ + format(options?: Partial): string { + return this.settings.format(this, { ...this.settings, ...options }); + } + + /** + * Converts the currency value to a string. + * @returns String representation of the currency value. + * @example + * const currency = new Currency(1234.56); + * const value = currency.toString(); // Returns '1234.56' + */ + toString(): string { + return (this.intValue / this.precisionFactor).toFixed(this.settings.precision); + } + + /** + * Returns the currency value for JSON serialization. + * @returns The numeric value for serialization. + * @example + * const currency = new Currency(1234.56); + * JSON.stringify(currency); // Returns '1234.56' + */ + toJSON(): number { + return this.value; + } +} + +/** + * Formats a currency object. + * @param currency - The Currency object to format. + * @param settings - Settings for formatting. + * @returns The formatted currency string. + * @example + * const currency = new Currency(1234.56); + * const formatted = formatCurrency(currency, currency.settings); // Returns '$1,234.56' + */ +function formatCurrency(currency: Currency, settings: CurrencySettings): string { + const { pattern, negativePattern, symbol, separator, decimal, groups } = settings; + const [dollars, cents] = currency.value.toFixed(settings.precision).split('.'); + + return (currency.value >= 0 ? pattern : negativePattern) + .replace('!', symbol) + .replace('#', dollars.replace(groups, '$1' + separator) + (cents ? decimal + cents : '')); +} + +export default Currency; diff --git a/src/index.ts b/src/index.ts index 7269088..933c365 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,3 @@ export { ConversionMaster } from './conversion-master'; +export { CountryCurrencyCode } from './country-currency-code'; export * from './types'; diff --git a/src/types/country-currency.enum.ts b/src/types/country-currency.enum.ts new file mode 100644 index 0000000..81e1f54 --- /dev/null +++ b/src/types/country-currency.enum.ts @@ -0,0 +1,201 @@ +/* eslint-disable @typescript-eslint/no-duplicate-enum-values */ +/** + * This enum maps country codes (ISO 3166-1 alpha-2) to their respective currency codes (ISO 4217). + */ +export enum CountryCurrencyEnum { + AE = 'AED', // United Arab Emirates + AF = 'AFN', // Afghanistan + AL = 'ALL', // Albania + AM = 'AMD', // Armenia + AO = 'AOA', // Angola + AR = 'ARS', // Argentina + AT = 'ATS', // Austria + AU = 'AUD', // Australia + AW = 'AWG', // Aruba + AZ = 'AZN', // Azerbaijan + BA = 'BAM', // Bosnia and Herzegovina + BB = 'BBD', // Barbados + BD = 'BDT', // Bangladesh + BE = 'BEF', // Belgium + BF = 'XOF', // Burkina Faso (West African CFA franc) + BG = 'BGN', // Bulgaria + BH = 'BHD', // Bahrain + BI = 'BIF', // Burundi + BJ = 'XOF', // Benin (West African CFA franc) + BM = 'BMD', // Bermuda + BN = 'BND', // Brunei + BO = 'BOB', // Bolivia + BR = 'BRL', // Brazil + BS = 'BSD', // Bahamas + BT = 'BTN', // Bhutan + BW = 'BWP', // Botswana + BY = 'BYN', // Belarus + BZ = 'BZD', // Belize + CA = 'CAD', // Canada + CD = 'CDF', // Congo (Democratic Republic) + CF = 'XAF', // Central African Republic (CFA franc BEAC) + CG = 'XAF', // Congo (CFA franc BEAC) + CH = 'CHF', // Switzerland + CI = 'XOF', // Ivory Coast (West African CFA franc) + CL = 'CLP', // Chile + CM = 'XAF', // Cameroon (CFA franc BEAC) + CN = 'CNY', // China + CO = 'COP', // Colombia + CR = 'CRC', // Costa Rica + CU = 'CUP', // Cuba + CV = 'CVE', // Cape Verde + CY = 'CYP', // Cyprus (Old currency) + CZ = 'CZK', // Czech Republic + DE = 'DEM', // Germany (Old currency) + DJ = 'DJF', // Djibouti + DK = 'DKK', // Denmark + DM = 'XCD', // Dominica (Eastern Caribbean dollar) + DO = 'DOP', // Dominican Republic + DZ = 'DZD', // Algeria + EC = 'USD', // Ecuador + EE = 'EEK', // Estonia (Old currency) + EG = 'EGP', // Egypt + ER = 'ERN', // Eritrea + ES = 'ESP', // Spain (Old currency) + ET = 'ETB', // Ethiopia + FI = 'FIM', // Finland (Old currency) + FJ = 'FJD', // Fiji + FM = 'USD', // Micronesia + FR = 'FRF', // France (Old currency) + GA = 'XAF', // Gabon (CFA franc BEAC) + GB = 'GBP', // United Kingdom + GE = 'GEL', // Georgia + GH = 'GHS', // Ghana + GM = 'GMD', // Gambia + GN = 'GNF', // Guinea + GQ = 'XAF', // Equatorial Guinea (CFA franc BEAC) + GR = 'GRD', // Greece (Old currency) + GT = 'GTQ', // Guatemala + GY = 'GYD', // Guyana + HK = 'HKD', // Hong Kong + HN = 'HNL', // Honduras + HR = 'HRK', // Croatia + HT = 'HTG', // Haiti + HU = 'HUF', // Hungary + ID = 'IDR', // Indonesia + IE = 'IEP', // Ireland (Old currency) + IL = 'ILS', // Israel + IN = 'INR', // India + IQ = 'IQD', // Iraq + IR = 'IRR', // Iran + IS = 'ISK', // Iceland + IT = 'ITL', // Italy (Old currency) + JM = 'JMD', // Jamaica + JO = 'JOD', // Jordan + JP = 'JPY', // Japan + KE = 'KES', // Kenya + KG = 'KGS', // Kyrgyzstan + KH = 'KHR', // Cambodia + KI = 'AUD', // Kiribati + KM = 'KMF', // Comoros + KN = 'XCD', // Saint Kitts and Nevis (Eastern Caribbean dollar) + KP = 'KPW', // North Korea + KR = 'KRW', // South Korea + KW = 'KWD', // Kuwait + KY = 'KYD', // Cayman Islands + KZ = 'KZT', // Kazakhstan + LA = 'LAK', // Laos + LB = 'LBP', // Lebanon + LC = 'XCD', // Saint Lucia (Eastern Caribbean dollar) + LI = 'CHF', // Liechtenstein + LK = 'LKR', // Sri Lanka + LR = 'LRD', // Liberia + LS = 'LSL', // Lesotho + LT = 'LTL', // Lithuania (Old currency) + LU = 'LUF', // Luxembourg (Old currency) + LV = 'LVL', // Latvia (Old currency) + LY = 'LYD', // Libya + MA = 'MAD', // Morocco + MC = 'EUR', // Monaco + MD = 'MDL', // Moldova + ME = 'EUR', // Montenegro + MG = 'MGA', // Madagascar + MH = 'USD', // Marshall Islands + MK = 'MKD', // North Macedonia + ML = 'XOF', // Mali (West African CFA franc) + MM = 'MMK', // Myanmar + MN = 'MNT', // Mongolia + MO = 'MOP', // Macau + MR = 'MRU', // Mauritania + MT = 'MTL', // Malta (Old currency) + MU = 'MUR', // Mauritius + MV = 'MVR', // Maldives + MW = 'MWK', // Malawi + MX = 'MXN', // Mexico + MY = 'MYR', // Malaysia + MZ = 'MZN', // Mozambique + NA = 'NAD', // Namibia + NE = 'XOF', // Niger (West African CFA franc) + NG = 'NGN', // Nigeria + NI = 'NIO', // Nicaragua + NL = 'NLG', // Netherlands (Old currency) + NO = 'NOK', // Norway + NP = 'NPR', // Nepal + NR = 'AUD', // Nauru + NZ = 'NZD', // New Zealand + OM = 'OMR', // Oman + PA = 'PAB', // Panama + PE = 'PEN', // Peru + PG = 'PGK', // Papua New Guinea + PH = 'PHP', // Philippines + PK = 'PKR', // Pakistan + PL = 'PLN', // Poland + PT = 'PTE', // Portugal (Old currency) + PY = 'PYG', // Paraguay + QA = 'QAR', // Qatar + RO = 'RON', // Romania + RU = 'RUB', // Russia + RW = 'RWF', // Rwanda + SA = 'SAR', // Saudi Arabia + SB = 'SBD', // Solomon Islands + SC = 'SCR', // Seychelles + SD = 'SDG', // Sudan + SE = 'SEK', // Sweden + SG = 'SGD', // Singapore + SH = 'SHP', // Saint Helena + SI = 'SIT', // Slovenia (Old currency) + SK = 'SKK', // Slovakia (Old currency) + SL = 'SLL', // Sierra Leone + SM = 'EUR', // San Marino + SN = 'XOF', // Senegal (West African CFA franc) + SO = 'SOS', // Somalia + SR = 'SRD', // Suriname + SS = 'SSP', // South Sudan + ST = 'STN', // São Tomé and Príncipe + SV = 'USD', // El Salvador + SY = 'SYP', // Syria + SZ = 'SZL', // Eswatini + TC = 'USD', // Turks and Caicos Islands + TD = 'XAF', // Chad (CFA franc BEAC) + TG = 'XOF', // Togo (West African CFA franc) + TH = 'THB', // Thailand + TJ = 'TJS', // Tajikistan + TL = 'USD', // Timor-Leste + TM = 'TMT', // Turkmenistan + TN = 'TND', // Tunisia + TO = 'TOP', // Tonga + TR = 'TRY', // Turkey + TT = 'TTD', // Trinidad and Tobago + TV = 'AUD', // Tuvalu + TZ = 'TZS', // Tanzania + UA = 'UAH', // Ukraine + UG = 'UGX', // Uganda + US = 'USD', // United States + UY = 'UYU', // Uruguay + UZ = 'UZS', // Uzbekistan + VA = 'EUR', // Vatican City + VC = 'XCD', // Saint Vincent and the Grenadines (Eastern Caribbean dollar) + VE = 'VES', // Venezuela + VN = 'VND', // Vietnam + VU = 'VUV', // Vanuatu + WS = 'WST', // Samoa + YE = 'YER', // Yemen + ZA = 'ZAR', // South Africa + ZM = 'ZMK', // Zambia (Old currency) + ZW = 'ZWL', // Zimbabwe +} diff --git a/src/types/country-currency.map.ts b/src/types/country-currency.map.ts new file mode 100644 index 0000000..2e2ff99 --- /dev/null +++ b/src/types/country-currency.map.ts @@ -0,0 +1,200 @@ +/** + * This const maps country codes (ISO 3166-1 alpha-2) to their respective currency codes (ISO 4217). + */ +export const CountryCurrencyMap = { + AE: 'AED', // United Arab Emirates + AF: 'AFN', // Afghanistan + AL: 'ALL', // Albania + AM: 'AMD', // Armenia + AO: 'AOA', // Angola + AR: 'ARS', // Argentina + AT: 'ATS', // Austria + AU: 'AUD', // Australia + AW: 'AWG', // Aruba + AZ: 'AZN', // Azerbaijan + BA: 'BAM', // Bosnia and Herzegovina + BB: 'BBD', // Barbados + BD: 'BDT', // Bangladesh + BE: 'BEF', // Belgium + BF: 'XOF', // Burkina Faso (West African CFA franc) + BG: 'BGN', // Bulgaria + BH: 'BHD', // Bahrain + BI: 'BIF', // Burundi + BJ: 'XOF', // Benin (West African CFA franc) + BM: 'BMD', // Bermuda + BN: 'BND', // Brunei + BO: 'BOB', // Bolivia + BR: 'BRL', // Brazil + BS: 'BSD', // Bahamas + BT: 'BTN', // Bhutan + BW: 'BWP', // Botswana + BY: 'BYN', // Belarus + BZ: 'BZD', // Belize + CA: 'CAD', // Canada + CD: 'CDF', // Congo (Democratic Republic) + CF: 'XAF', // Central African Republic (CFA franc BEAC) + CG: 'XAF', // Congo (CFA franc BEAC) + CH: 'CHF', // Switzerland + CI: 'XOF', // Ivory Coast (West African CFA franc) + CL: 'CLP', // Chile + CM: 'XAF', // Cameroon (CFA franc BEAC) + CN: 'CNY', // China + CO: 'COP', // Colombia + CR: 'CRC', // Costa Rica + CU: 'CUP', // Cuba + CV: 'CVE', // Cape Verde + CY: 'CYP', // Cyprus (Old currency) + CZ: 'CZK', // Czech Republic + DE: 'DEM', // Germany (Old currency) + DJ: 'DJF', // Djibouti + DK: 'DKK', // Denmark + DM: 'XCD', // Dominica (Eastern Caribbean dollar) + DO: 'DOP', // Dominican Republic + DZ: 'DZD', // Algeria + EC: 'USD', // Ecuador + EE: 'EEK', // Estonia (Old currency) + EG: 'EGP', // Egypt + ER: 'ERN', // Eritrea + ES: 'ESP', // Spain (Old currency) + ET: 'ETB', // Ethiopia + FI: 'FIM', // Finland (Old currency) + FJ: 'FJD', // Fiji + FM: 'USD', // Micronesia + FR: 'FRF', // France (Old currency) + GA: 'XAF', // Gabon (CFA franc BEAC) + GB: 'GBP', // United Kingdom + GE: 'GEL', // Georgia + GH: 'GHS', // Ghana + GM: 'GMD', // Gambia + GN: 'GNF', // Guinea + GQ: 'XAF', // Equatorial Guinea (CFA franc BEAC) + GR: 'GRD', // Greece (Old currency) + GT: 'GTQ', // Guatemala + GY: 'GYD', // Guyana + HK: 'HKD', // Hong Kong + HN: 'HNL', // Honduras + HR: 'HRK', // Croatia + HT: 'HTG', // Haiti + HU: 'HUF', // Hungary + ID: 'IDR', // Indonesia + IE: 'IEP', // Ireland (Old currency) + IL: 'ILS', // Israel + IN: 'INR', // India + IQ: 'IQD', // Iraq + IR: 'IRR', // Iran + IS: 'ISK', // Iceland + IT: 'ITL', // Italy (Old currency) + JM: 'JMD', // Jamaica + JO: 'JOD', // Jordan + JP: 'JPY', // Japan + KE: 'KES', // Kenya + KG: 'KGS', // Kyrgyzstan + KH: 'KHR', // Cambodia + KI: 'AUD', // Kiribati + KM: 'KMF', // Comoros + KN: 'XCD', // Saint Kitts and Nevis (Eastern Caribbean dollar) + KP: 'KPW', // North Korea + KR: 'KRW', // South Korea + KW: 'KWD', // Kuwait + KY: 'KYD', // Cayman Islands + KZ: 'KZT', // Kazakhstan + LA: 'LAK', // Laos + LB: 'LBP', // Lebanon + LC: 'XCD', // Saint Lucia (Eastern Caribbean dollar) + LI: 'CHF', // Liechtenstein + LK: 'LKR', // Sri Lanka + LR: 'LRD', // Liberia + LS: 'LSL', // Lesotho + LT: 'LTL', // Lithuania (Old currency) + LU: 'LUF', // Luxembourg (Old currency) + LV: 'LVL', // Latvia (Old currency) + LY: 'LYD', // Libya + MA: 'MAD', // Morocco + MC: 'EUR', // Monaco + MD: 'MDL', // Moldova + ME: 'EUR', // Montenegro + MG: 'MGA', // Madagascar + MH: 'USD', // Marshall Islands + MK: 'MKD', // North Macedonia + ML: 'XOF', // Mali (West African CFA franc) + MM: 'MMK', // Myanmar + MN: 'MNT', // Mongolia + MO: 'MOP', // Macau + MR: 'MRU', // Mauritania + MT: 'MTL', // Malta (Old currency) + MU: 'MUR', // Mauritius + MV: 'MVR', // Maldives + MW: 'MWK', // Malawi + MX: 'MXN', // Mexico + MY: 'MYR', // Malaysia + MZ: 'MZN', // Mozambique + NA: 'NAD', // Namibia + NE: 'XOF', // Niger (West African CFA franc) + NG: 'NGN', // Nigeria + NI: 'NIO', // Nicaragua + NL: 'NLG', // Netherlands (Old currency) + NO: 'NOK', // Norway + NP: 'NPR', // Nepal + NR: 'AUD', // Nauru + NZ: 'NZD', // New Zealand + OM: 'OMR', // Oman + PA: 'PAB', // Panama + PE: 'PEN', // Peru + PG: 'PGK', // Papua New Guinea + PH: 'PHP', // Philippines + PK: 'PKR', // Pakistan + PL: 'PLN', // Poland + PT: 'PTE', // Portugal (Old currency) + PY: 'PYG', // Paraguay + QA: 'QAR', // Qatar + RO: 'RON', // Romania + RU: 'RUB', // Russia + RW: 'RWF', // Rwanda + SA: 'SAR', // Saudi Arabia + SB: 'SBD', // Solomon Islands + SC: 'SCR', // Seychelles + SD: 'SDG', // Sudan + SE: 'SEK', // Sweden + SG: 'SGD', // Singapore + SH: 'SHP', // Saint Helena + SI: 'SIT', // Slovenia (Old currency) + SK: 'SKK', // Slovakia (Old currency) + SL: 'SLL', // Sierra Leone + SM: 'EUR', // San Marino + SN: 'XOF', // Senegal (West African CFA franc) + SO: 'SOS', // Somalia + SR: 'SRD', // Suriname + SS: 'SSP', // South Sudan + ST: 'STN', // São Tomé and Príncipe + SV: 'USD', // El Salvador + SY: 'SYP', // Syria + SZ: 'SZL', // Eswatini + TC: 'USD', // Turks and Caicos Islands + TD: 'XAF', // Chad (CFA franc BEAC) + TG: 'XOF', // Togo (West African CFA franc) + TH: 'THB', // Thailand + TJ: 'TJS', // Tajikistan + TL: 'USD', // Timor-Leste + TM: 'TMT', // Turkmenistan + TN: 'TND', // Tunisia + TO: 'TOP', // Tonga + TR: 'TRY', // Turkey + TT: 'TTD', // Trinidad and Tobago + TV: 'AUD', // Tuvalu + TZ: 'TZS', // Tanzania + UA: 'UAH', // Ukraine + UG: 'UGX', // Uganda + US: 'USD', // United States + UY: 'UYU', // Uruguay + UZ: 'UZS', // Uzbekistan + VA: 'EUR', // Vatican City + VC: 'XCD', // Saint Vincent and the Grenadines (Eastern Caribbean dollar) + VE: 'VES', // Venezuela + VN: 'VND', // Vietnam + VU: 'VUV', // Vanuatu + WS: 'WST', // Samoa + YE: 'YER', // Yemen + ZA: 'ZAR', // South Africa + ZM: 'ZMK', // Zambia (Old currency) + ZW: 'ZWL', // Zimbabwe +} as const; diff --git a/src/types/currency-settings.ts b/src/types/currency-settings.ts new file mode 100644 index 0000000..f5bc896 --- /dev/null +++ b/src/types/currency-settings.ts @@ -0,0 +1,16 @@ +import Currency from '../currency'; + +export type CurrencySettings = { + symbol: string; + separator: string; + decimal: string; + errorOnInvalid: boolean; + precision: number; + pattern: string; + negativePattern: string; + fromCents: boolean; + increment?: number; + useVedic?: boolean; + groups: RegExp; + format: (currency: Currency, settings: CurrencySettings) => string; +}; diff --git a/src/types/currency.enum.ts b/src/types/currency.enum.ts index 3915d24..a9d8ab5 100644 --- a/src/types/currency.enum.ts +++ b/src/types/currency.enum.ts @@ -1,254 +1,254 @@ export enum CurrencyEnum { - Satoshi = '1000sats', - OneInch = '1inch', - Aave = 'aave', - Cardano = 'ada', - EmiratiDirham = 'aed', - AfghanAfghani = 'afn', - SingularityNET = 'agix', - AkashNetwork = 'akt', - Algorand = 'algo', - AlbanianLek = 'all', - ArmenianDram = 'amd', - AmpToken = 'amp', - DutchGuilder = 'ang', - AngolanKwanza = 'aoa', - ApeCoin = 'ape', - Aptos = 'apt', - Arweave = 'ar', - Arbitrum = 'arb', - ArgentinePeso = 'ars', - Cosmos = 'atom', - AustrianSchilling = 'ats', - AustralianDollar = 'aud', - Avalanche = 'avax', - ArubanGuilder = 'awg', - AxieInfinity = 'axs', - AzerbaijaniOldManat = 'azm', - AzerbaijaniManat = 'azn', - BakeryToken = 'bake', - BosnianConvertibleMark = 'bam', - BasicAttentionToken = 'bat', - BarbadianDollar = 'bbd', - BitcoinCash = 'bch', - BangladeshiTaka = 'bdt', - BelgianFranc = 'bef', - BulgarianLev = 'bgn', - BahrainiDinar = 'bhd', - BurundianFranc = 'bif', - BermudianDollar = 'bmd', - BinanceCoin = 'bnb', - BruneianDollar = 'bnd', - BolivianBoliviano = 'bob', - BrazilianReal = 'brl', - BahamianDollar = 'bsd', - BitcoinSV = 'bsv', - Biswap = 'bsw', - Bitcoin = 'btc', - BitcoinBEP2 = 'btcb', - BitcoinGold = 'btg', - BhutaneseNgultrum = 'btn', - BitTorrent = 'btt', - BinanceUSD = 'busd', - BotswanaPula = 'bwp', - BelarusianRuble = 'byn', - BelarusianOldRuble = 'byr', - BelizeanDollar = 'bzd', - CanadianDollar = 'cad', - PancakeSwap = 'cake', - CongoleseFranc = 'cdf', - Celo = 'celo', - Conflux = 'cfx', - SwissFranc = 'chf', - Chiliz = 'chz', - ChileanPeso = 'clp', - ChineseYuanOffshore = 'cnh', - ChineseYuan = 'cny', - Compound = 'comp', - ColombianPeso = 'cop', - CostaRicanColon = 'crc', - CryptoComChain = 'cro', - CurveDAO = 'crv', - Casper = 'cspr', - CubanConvertiblePeso = 'cuc', - CubanPeso = 'cup', - CapeVerdeanEscudo = 'cve', - ConvexFinance = 'cvx', - CypriotPound = 'cyp', - CzechKoruna = 'czk', - DAI = 'dai', - Dash = 'dash', - Decred = 'dcr', - GermanDeutscheMark = 'dem', - DfiStarter = 'dfi', - DjiboutianFranc = 'djf', - DanishKrone = 'dkk', - Dogecoin = 'doge', - DominicanPeso = 'dop', - Polkadot = 'dot', - DYdX = 'dydx', - AlgerianDinar = 'dzd', - EstonianKroon = 'eek', - Elrond = 'egld', - EgyptianPound = 'egp', - EnjinCoin = 'enj', - EOS = 'eos', - EritreanNakfa = 'ern', - SpanishPeseta = 'esp', - EthiopianBirr = 'etb', - EthereumClassic = 'etc', - Ethereum = 'eth', - Euro = 'eur', - FeiUSD = 'fei', - Filecoin = 'fil', - FinnishMarkka = 'fim', - FijianDollar = 'fjd', - FalklandIslandsPound = 'fkp', - Flow = 'flow', - Flare = 'flr', - Frax = 'frax', - FrenchFranc = 'frf', - Fantom = 'ftm', - FTXToken = 'ftt', - FraxShare = 'fxs', - Gala = 'gala', - BritishPound = 'gbp', - GeorgianLari = 'gel', - GuernseyPound = 'ggp', - GhanaianOldCedi = 'ghc', - GhanaianCedi = 'ghs', - GibraltarPound = 'gip', - GambianDalasi = 'gmd', - GoldmaxCoin = 'gmx', - GuineanFranc = 'gnf', - Gnosis = 'gno', - GreekDrachma = 'grd', - TheGraph = 'grt', - GateToken = 'gt', - GuatemalanQuetzal = 'gtq', - GeminiUSD = 'gusd', - GuyaneseDollar = 'gyd', - Hedera = 'hbar', - HongKongDollar = 'hkd', - HonduranLempira = 'hnl', - Helium = 'hnt', - HydroProtocol = 'hot', - CroatianKuna = 'hrk', - HuobiToken = 'ht', - HaitianGourde = 'htg', - HungarianForint = 'huf', - InternetComputer = 'icp', - IndonesianRupiah = 'idr', - IrishPound = 'iep', - IsraeliShekel = 'ils', - IsleOfManPound = 'imp', - ImmutableX = 'imx', - Injective = 'inj', - IndianRupee = 'inr', - IraqiDinar = 'iqd', - IranianRial = 'irr', - IcelandicKrona = 'isk', - ItalianLira = 'itl', - JerseyPound = 'jep', - JamaicanDollar = 'jmd', - JordanianDinar = 'jod', - JapaneseYen = 'jpy', - Kava = 'kava', - Kucoin = 'kcs', - Kadena = 'kda', - KenyanShilling = 'kes', - KyrgyzstaniSom = 'kgs', - CambodianRiel = 'khr', - Klaytn = 'klay', - ComorianFranc = 'kmf', - KyberNetworkCrystals = 'knc', - NorthKoreanWon = 'kpw', - SouthKoreanWon = 'krw', - Kusama = 'ksm', - KuwaitiDinar = 'kwd', - CaymanIslandsDollar = 'kyd', - KazakhstaniTenge = 'kzt', - LaoKip = 'lak', - LebanesePound = 'lbp', - LidoDAO = 'ldo', - LEOCoin = 'leo', - Chainlink = 'link', - SriLankanRupee = 'lkr', - Loopring = 'lrc', - LiberianDollar = 'lrd', - BasothoLoti = 'lsl', - Litecoin = 'ltc', - LithuanianLitas = 'ltl', - LuxembourgFranc = 'luf', - Terra = 'luna', - LatvianLat = 'lvl', - LibyanDinar = 'lyd', - MoroccanDirham = 'mad', - Decentraland = 'mana', - Polygon = 'matic', - MobieCoin = 'mbx', - MoldovanLeu = 'mdl', - MalagasyAriary = 'mga', - MalagasyOldFranc = 'mgf', - Mina = 'mina', - MacedonianDenar = 'mkd', - Maker = 'mkr', - BurmeseKyat = 'mmk', - MongolianTughrik = 'mnt', - MacauPataca = 'mop', - MauritanianOldOuguiya = 'mro', - MauritanianOuguiya = 'mru', - MalteseLira = 'mtl', - MauritianRupee = 'mur', - MaldivianRufiyaa = 'mvr', - MalawianKwacha = 'mwk', - MexicanPeso = 'mxn', - MalaysianRinggit = 'myr', - MozambicanMetical = 'mzn', - NamibianDollar = 'nad', - NearProtocol = 'near', - NEO = 'neo', - NEXO = 'nexo', - NFT = 'nft', - NigerianNaira = 'ngn', - NicaraguanCordoba = 'nio', - DutchGuilderAlternative = 'nlg', - NorwegianKrone = 'nok', - NepaleseRupee = 'npr', - OmaniRial = 'omr', - PakistaniRupee = 'pkr', - PanamanianBalboa = 'pab', - ParaguayanGuarani = 'pyg', - PapuaNewGuineanKina = 'pgk', - PeruvianSol = 'pen', - PhilippinePeso = 'php', - PolishZloty = 'pln', - QatariRiyal = 'qar', - RomanianLeu = 'ron', - RussianRuble = 'rub', - RwandanFranc = 'rwf', - SaudiRiyal = 'sar', - WestAfricanCFAFranc = 'xof', - SerbianDinar = 'rsd', - SingaporeDollar = 'sgd', - SolomonIslandsDollar = 'sbd', - SomaliShilling = 'sos', - SouthAfricanRand = 'zar', - EswatiniLilangeni = 'szl', - SyrianPound = 'syp', - TrinidadAndTobagoDollar = 'ttd', - TurkishLira = 'try', - TurkmenistaniManat = 'tmt', - UgandanShilling = 'ugx', - UkrainianHryvnia = 'uah', - UnitedStatesDollar = 'usd', - UruguayanPeso = 'uyu', - UzbekistaniSom = 'uzs', - VanuatuVatu = 'vuv', - VenezuelanBolivar = 'ves', - VietnameseDong = 'vnd', - SamoanTala = 'wst', - YemeniRial = 'yer', - ZambianOldKwacha = 'zmk', - ZimbabweanDollar = 'zwl', + Satoshi = '1000SATS', + OneInch = '1INCH', + Aave = 'AAVE', + Cardano = 'ADA', + EmiratiDirham = 'AED', + AfghanAfghani = 'AFN', + SingularityNET = 'AGIX', + AkashNetwork = 'AKT', + Algorand = 'ALGO', + AlbanianLek = 'ALL', + ArmenianDram = 'AMD', + AmpToken = 'AMP', + DutchGuilder = 'ANG', + AngolanKwanza = 'AOA', + ApeCoin = 'APE', + Aptos = 'APT', + Arweave = 'AR', + Arbitrum = 'ARB', + ArgentinePeso = 'ARS', + Cosmos = 'ATOM', + AustrianSchilling = 'ATS', + AustralianDollar = 'AUD', + Avalanche = 'AVAX', + ArubanGuilder = 'AWG', + AxieInfinity = 'AXS', + AzerbaijaniOldManat = 'AZM', + AzerbaijaniManat = 'AZN', + BakeryToken = 'BAKE', + BosnianConvertibleMark = 'BAM', + BasicAttentionToken = 'BAT', + BarbadianDollar = 'BBD', + BitcoinCash = 'BCH', + BangladeshiTaka = 'BDT', + BelgianFranc = 'BEF', + BulgarianLev = 'BGN', + BahrainiDinar = 'BHD', + BurundianFranc = 'BIF', + BermudianDollar = 'BMD', + BinanceCoin = 'BNB', + BruneianDollar = 'BND', + BolivianBoliviano = 'BOB', + BrazilianReal = 'BRL', + BahamianDollar = 'BSD', + BitcoinSV = 'BSV', + Biswap = 'BSW', + Bitcoin = 'BTC', + BitcoinBEP2 = 'BTCB', + BitcoinGold = 'BTG', + BhutaneseNgultrum = 'BTN', + BitTorrent = 'BTT', + BinanceUSD = 'BUSD', + BotswanaPula = 'BWP', + BelarusianRuble = 'BYN', + BelarusianOldRuble = 'BYR', + BelizeanDollar = 'BZD', + CanadianDollar = 'CAD', + PancakeSwap = 'CAKE', + CongoleseFranc = 'CDF', + Celo = 'CELO', + Conflux = 'CFX', + SwissFranc = 'CHF', + Chiliz = 'CHZ', + ChileanPeso = 'CLP', + ChineseYuanOffshore = 'CNH', + ChineseYuan = 'CNY', + Compound = 'COMP', + ColombianPeso = 'COP', + CostaRicanColon = 'CRC', + CryptoComChain = 'CRO', + CurveDAO = 'CRV', + Casper = 'CSPR', + CubanConvertiblePeso = 'CUC', + CubanPeso = 'CUP', + CapeVerdeanEscudo = 'CVE', + ConvexFinance = 'CVX', + CypriotPound = 'CYP', + CzechKoruna = 'CZK', + DAI = 'DAI', + Dash = 'DASH', + Decred = 'DCR', + GermanDeutscheMark = 'DEM', + DfiStarter = 'DFI', + DjiboutianFranc = 'DJF', + DanishKrone = 'DKK', + Dogecoin = 'DOGE', + DominicanPeso = 'DOP', + Polkadot = 'DOT', + DYdX = 'DYDX', + AlgerianDinar = 'DZD', + EstonianKroon = 'EEK', + Elrond = 'EGLD', + EgyptianPound = 'EGP', + EnjinCoin = 'ENJ', + EOS = 'EOS', + EritreanNakfa = 'ERN', + SpanishPeseta = 'ESP', + EthiopianBirr = 'ETB', + EthereumClassic = 'ETC', + Ethereum = 'ETH', + Euro = 'EUR', + FeiUSD = 'FEI', + Filecoin = 'FIL', + FinnishMarkka = 'FIM', + FijianDollar = 'FJD', + FalklandIslandsPound = 'FKP', + Flow = 'FLOW', + Flare = 'FLR', + Frax = 'FRAX', + FrenchFranc = 'FRF', + Fantom = 'FTM', + FTXToken = 'FTT', + FraxShare = 'FXS', + Gala = 'GALA', + BritishPound = 'GBP', + GeorgianLari = 'GEL', + GuernseyPound = 'GGP', + GhanaianOldCedi = 'GHC', + GhanaianCedi = 'GHS', + GibraltarPound = 'GIP', + GambianDalasi = 'GMD', + GoldmaxCoin = 'GMX', + GuineanFranc = 'GNF', + Gnosis = 'GNO', + GreekDrachma = 'GRD', + TheGraph = 'GRT', + GateToken = 'GT', + GuatemalanQuetzal = 'GTQ', + GeminiUSD = 'GUSD', + GuyaneseDollar = 'GYD', + Hedera = 'HBAR', + HongKongDollar = 'HKD', + HonduranLempira = 'HNL', + Helium = 'HNT', + HydroProtocol = 'HOT', + CroatianKuna = 'HRK', + HuobiToken = 'HT', + HaitianGourde = 'HTG', + HungarianForint = 'HUF', + InternetComputer = 'ICP', + IndonesianRupiah = 'IDR', + IrishPound = 'IEP', + IsraeliShekel = 'ILS', + IsleOfManPound = 'IMP', + ImmutableX = 'IMX', + Injective = 'INJ', + IndianRupee = 'INR', + IraqiDinar = 'IQD', + IranianRial = 'IRR', + IcelandicKrona = 'ISK', + ItalianLira = 'ITL', + JerseyPound = 'JEP', + JamaicanDollar = 'JMD', + JordanianDinar = 'JOD', + JapaneseYen = 'JPY', + Kava = 'KAVA', + Kucoin = 'KCS', + Kadena = 'KDA', + KenyanShilling = 'KES', + KyrgyzstaniSom = 'KGS', + CambodianRiel = 'KHR', + Klaytn = 'KLAY', + ComorianFranc = 'KMF', + KyberNetworkCrystals = 'KNC', + NorthKoreanWon = 'KPW', + SouthKoreanWon = 'KRW', + Kusama = 'KSM', + KuwaitiDinar = 'KWD', + CaymanIslandsDollar = 'KYD', + KazakhstaniTenge = 'KZT', + LaoKip = 'LAK', + LebanesePound = 'LBP', + LidoDAO = 'LDO', + LEOCoin = 'LEO', + Chainlink = 'LINK', + SriLankanRupee = 'LKR', + Loopring = 'LRC', + LiberianDollar = 'LRD', + BasothoLoti = 'LSL', + Litecoin = 'LTC', + LithuanianLitas = 'LTL', + LuxembourgFranc = 'LUF', + Terra = 'LUNA', + LatvianLat = 'LVL', + LibyanDinar = 'LYD', + MoroccanDirham = 'MAD', + Decentraland = 'MANA', + Polygon = 'MATIC', + MobieCoin = 'MBX', + MoldovanLeu = 'MDL', + MalagasyAriary = 'MGA', + MalagasyOldFranc = 'MGF', + Mina = 'MINA', + MacedonianDenar = 'MKD', + Maker = 'MKR', + BurmeseKyat = 'MMK', + MongolianTughrik = 'MNT', + MacauPataca = 'MOP', + MauritanianOldOuguiya = 'MRO', + MauritanianOuguiya = 'MRU', + MalteseLira = 'MTL', + MauritianRupee = 'MUR', + MaldivianRufiyaa = 'MVR', + MalawianKwacha = 'MWK', + MexicanPeso = 'MXN', + MalaysianRinggit = 'MYR', + MozambicanMetical = 'MZN', + NamibianDollar = 'NAD', + NearProtocol = 'NEAR', + NEO = 'NEO', + NEXO = 'NEXO', + NFT = 'NFT', + NigerianNaira = 'NGN', + NicaraguanCordoba = 'NIO', + DutchGuilderAlternative = 'NLG', + NorwegianKrone = 'NOK', + NepaleseRupee = 'NPR', + OmaniRial = 'OMR', + PakistaniRupee = 'PKR', + PanamanianBalboa = 'PAB', + ParaguayanGuarani = 'PYG', + PapuaNewGuineanKina = 'PGK', + PeruvianSol = 'PEN', + PhilippinePeso = 'PHP', + PolishZloty = 'PLN', + QatariRiyal = 'QAR', + RomanianLeu = 'RON', + RussianRuble = 'RUB', + RwandanFranc = 'RWF', + SaudiRiyal = 'SAR', + WestAfricanCFAFranc = 'XOF', + SerbianDinar = 'RSD', + SingaporeDollar = 'SGD', + SolomonIslandsDollar = 'SBD', + SomaliShilling = 'SOS', + SouthAfricanRand = 'ZAR', + EswatiniLilangeni = 'SZL', + SyrianPound = 'SYP', + TrinidadAndTobagoDollar = 'TTD', + TurkishLira = 'TRY', + TurkmenistaniManat = 'TMT', + UgandanShilling = 'UGX', + UkrainianHryvnia = 'UAH', + UnitedStatesDollar = 'USD', + UruguayanPeso = 'UYU', + UzbekistaniSom = 'UZS', + VanuatuVatu = 'VUV', + VenezuelanBolivar = 'VES', + VietnameseDong = 'VND', + SamoanTala = 'WST', + YemeniRial = 'YER', + ZambianOldKwacha = 'ZMK', + ZimbabweanDollar = 'ZWL', } diff --git a/src/types/index.ts b/src/types/index.ts index 8a613b5..711c1eb 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,2 +1,3 @@ export { CurrencyEnum } from './currency.enum'; export { CurrencyApiResponse, CurrencyRates } from './currency-converter-api-response'; +export { CountryCurrencyEnum } from './country-currency.enum';