A lightweight TypeScript package for conversion/exchanges fiat or cryptocurrencies with formatted value.
- Installation
- Api
- Abstract
ConversionBase
ConversionCore
- Class
- Abstract
- Contributing
- Support
- Code of Conduct
- Git
- License
- Related packages
npm install
@typedly/currency
@typescript-package/range --save-peer
npm install @typescript-package/currency --save-peer
import {
// Abstract.
ConversionBase,
ConversionCore,
// Class.
Conversion,
Currencies,
CurrencyValue,
Exchange
} from '@typescript-package/currency';
The concrete class implementation for currency conversion.
import { Conversion } from '@typescript-package/currency';
const conversion = new Conversion(15 as number, 'PLN', { 'USD': 0.275, 'EUR': 0.2347 });
conversion.amount; // 15
conversion.currency; // PLN
conversion.currencies; // ['USD', 'EUR']
conversion.getConversionRates(); // { 'USD': 0.275, 'EUR': 0.2347 }
conversion.conversionRates; // Map([["USD", 0.275], ["EUR", 0.2347 ]]);
// Convert PLN to USD.
conversion.to('USD'); // 4.125
// Convert 15 PLN to EUR.
conversion.to('EUR', 15); // 3.5204999999999997
// Convert USD and EUR to PLN.
conversion.toMany(['USD', 'EUR']); // { USD: 4.125, EUR: 3.5204999999999997 }
// Convert 10 USD and 10 EUR to PLN.
conversion.toMany(['USD', 'EUR'], 10); // { USD: 2.75, EUR: 2.347 }
// Convert and sum USD and EUR.
conversion.sumTo(['USD', 'EUR'], 10); // 5.0969999999999995
// Convert USD to PLN.
conversion.from('USD'); // 54.54545454545454
// Convert custom 15 EUR to PLN.
conversion.from('EUR', 15); // 63.911376224968045
// Convert 10 USD and 10 EUR to PLN.
conversion.fromMany(['USD', 'EUR']); // { USD: 54.54545454545454, EUR: 63.911376224968045 }
conversion.fromMany(['USD', 'EUR'], 10); // { "USD": 36.36363636363636, "EUR": 42.6075841499787 }
conversion.sumFrom(['USD', 'EUR'], 10); // 78.97122051361507
// Set amount to 5 PLN.
conversion.setAmount(5);
// Convert 5 PLN to EUR.
conversion.to('EUR'); // 1.1735
The class to store codes of the currencies.
import { Currencies } from '@typescript-package/currency';
The currency value class for representing and manipulating monetary values.
import { CurrencyValue } from '@typescript-package/currency';
const value = new CurrencyValue(42345255.356, 'USD');
value.valueOf(); // 42345255.356
value.currency; // USD
value.of; // 42345255.356
value.currencySymbol; // $
value.formatted; // 42,345,255.36
value.getValueAsCurrency('USD'); // $42,345,255.36
value.isBetweenEvery([43345255, 52345255.356], [3500, 127000]); // false
value.lessThan(52345255.356); // true
The Exchange
class provides functionality to fetch and manage currency exchange rates, and perform conversions using the Conversion
class.
import { Exchange } from '@typescript-package/currency';
const exchange = new Exchange(
15 as number, // number
'PLN', // base currency code
['USD', 'EUR'], // currencies for exchanges
{ 'USD': 0.275, 'EUR': 0.2347 } // custom exchanges
// link for exchange api
);
exchange.amount; // 15
exchange.currency; // PLN
exchange.currencies; // ['USD', 'EUR']
exchange.exchangeRates; // { 'USD': 0.275, 'EUR': 0.2347 }
exchange.to('USD').then(result => console.debug(`exchange.to('USD')`, result)); // 4.125
exchange.to('EUR', 15).then(result => console.debug(`exchange.to('EUR', 15)`, result)); // 3.5204999999999997
exchange.from('USD').then(result => console.debug(`exchange.from('USD')`, result)); // 54.54545454545454
exchange.from('EUR', 15).then(result => console.debug(`exchange.from('EUR', 15)`, result)); // 63.911376224968045
Your contributions are valued! If you'd like to contribute, please feel free to submit a pull request. Help is always appreciated.
If you find this package useful and would like to support its and general development, you can contribute through one of the following payment methods. Your support helps maintain the packages and continue adding new.
Support via:
or via Trust Wallet
By participating in this project, you agree to follow Code of Conduct.
Given a version number MAJOR.MINOR.PATCH, increment the:
- MAJOR version when you make incompatible API changes,
- MINOR version when you add functionality in a backwards-compatible manner, and
- PATCH version when you make backwards-compatible bug fixes.
Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.
FAQ How should I deal with revisions in the 0.y.z initial development phase?
The simplest thing to do is start your initial development release at 0.1.0 and then increment the minor version for each subsequent release.
How do I know when to release 1.0.0?
If your software is being used in production, it should probably already be 1.0.0. If you have a stable API on which users have come to depend, you should be 1.0.0. If you’re worrying a lot about backwards compatibility, you should probably already be 1.0.0.
MIT © typescript-package (license)
- @typescript-package/chain-descriptor: A TypeScript library for chain property descriptor.
- @typescript-package/controller: A TypeScript package with for various kind of controllers.
- @typescript-package/descriptor: A TypeScript library for property descriptor.
- @typescript-package/descriptor-chain: A TypeScript library for property descriptor chain.
- @typescript-package/descriptors: A TypeScript library for property descriptors.
- @typescript-package/property: A TypeScript package with features to handle object properties.
- @typescript-package/wrap-descriptor: A TypeScript package for wrapping object descriptors.
- @typescript-package/wrap-property: A TypeScript package for wrapping object properties.
- @xtypescript/property - A comprehensive, reactive TypeScript library for precise and extensible object property control.