@osskit/currencies
v2.2.1
Published
A TypeScript library for handling currency codes, symbols, and conversions.
Readme
@osskit/currencies
A TypeScript library for handling currency codes, symbols, and conversions.
Installation
yarn add @osskit/currenciesor
npm install @osskit/currenciesUsage
Importing the library
import {
getByCode,
getByNumber,
isCurrencyCode,
toMajorUnit,
toMinorUnit,
getSymbolByCode,
isAmount,
calculateRate,
applyRate,
} from '@osskit/currencies';Examples
Get currency by code
const currency = getByCode('EUR');
console.log(currency);Get currency by number
const currency = getByNumber('978');
console.log(currency);Check if a string is a valid currency code
const isValid = isCurrencyCode('USD');
console.log(isValid); // trueConvert to major unit
const majorUnit = toMajorUnit({ value: 100_000, currency: 'USD' });
console.log(majorUnit); // 1000Convert to minor unit
const minorUnit = toMinorUnit(1000, 'USD');
console.log(minorUnit); // 100_000Get symbol by currency code
const symbol = getSymbolByCode('USD');
console.log(symbol); // $Check if an object is a valid amount
const validAmount = isAmount({ value: 1000, currency: 'USD' });
console.log(validAmount); // trueCalculate exchange rate
const rate = calculateRate({ value: 1000, currency: 'USD' }, { value: 800, currency: 'EUR' });
console.log(rate); // 0.8Apply exchange rate to amount
const convertedAmount = applyRate({ value: 100_000, currency: 'USD' }, 'EUR', 0.8);
console.log(convertedAmount); // { value: 80_000, currency: 'EUR' }Running Tests
To run the tests, use the following command:
yarn testContributing
Contributions are welcome! Please open an issue or submit a pull request.
License
This project is licensed under the MIT License.
