country-currency-utils-john-edwin-r
v1.0.0
Published
A collection of utilities for working with country and currency data
Downloads
8
Maintainers
Readme
country-currency-utils
A lightweight TypeScript utility library for working with country and currency metadata.
Features
- 250 country and territory records
- 154 currency records
- Country lookup by ISO-2 code
- Currency symbol and rate lookup
- Basic currency conversion helper
- TypeScript declarations included (
dist/*.d.ts)
Installation
npm install country-currency-utilsLocal Development
npm install
npm run typecheck
npm run buildBuild output is generated in dist/.
Usage
import { CountryService, CurrencyService } from "country-currency-utils";
const countries = CountryService.getAll();
const usCurrency = CountryService.getCurrency("US");
const usdSymbol = CurrencyService.getSymbol("USD");
const converted = CurrencyService.convert(100, "USD", "JPY");
console.log(countries.length);
console.log(usCurrency);
console.log(usdSymbol);
console.log(converted);API
CountryService
getAll(): Country[]Returns all country entries.getByCode(code: string): Country | undefinedReturns a country by ISO-2 code (for example,"US","LK","JP").getCurrency(countryCode: string): string | undefinedReturns the currency code for a country.
CurrencyService
getSymbol(code: string): string | undefinedReturns a currency symbol by currency code.getRate(code: string): number | undefinedReturns the configured rate for a currency code.convert(amount: number, from: string, to: string): numberConverts using the formula:
$$ ext{result} = \left(\frac{\text{amount}}{\text{fromRate}}\right) \times \text{toRate} $$
Throws an error when from or to currency is invalid.
Data Shape
export interface Country {
name: string;
code: string;
currency: string;
symbol: string;
locale: string;
}
export interface Currency {
symbol: string;
rate: number;
}Notes
- Current
ratevalues are placeholders (1) in the bundled dataset. - For real-world conversion, plug in live exchange rates before calling
convert.
License
MIT
