Socket
Socket
Sign inDemoInstall

currencyxchange

Package Overview
Dependencies
4
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    currencyxchange

A light weight library for currency locale and exchange rates. Currency x-change module use auto caching of exchange rates.


Version published
Weekly downloads
65
increased by150%
Maintainers
1
Install size
539 kB
Created
Weekly downloads
 

Readme

Source

Currency X-change

A light weight library for currency locale and exchange rates. Currency x-change module use auto caching of exchange rates.

It is based on exchangeratesapi.io API.

"Light-Weight, Easy to use"

Getting started

Installation

Use the package manager npm or yarn to install foobar.

npm i currencyxchange

or

 yarn add currencyxchange

Import

import { formatCurrency, getCurrencyRate, getCurrencyRateList, getCurrencyCode, getCurrencySymbol } from 'currencyxchange';

or

import currencyxchange from 'currencyxchange';

Functions

This package exports following functions to use

  • getCurrencyRate(): Takes base currency as parameter and return exchange rates to all supported currencies, Returns a promise.

  • getCurrencyRateList(): Fetch latest exchange rates for all supported currencies, Returns a promise.

  • formatCurrency(): convert price to reuested currency based on latest exchange rates, Returns a formatted price in string format.

  • getCurrencyCode(): Fetch currency code by country code, Returns a string.

  • getCurrencySymbol(): Fetch currency symbol by currency code, Returns a string.

Usage

1. getCurrencyRate(base: string)

By default it will return exchange rates with base currency 'USD'. It will return cached exchange rates. If no cache found it will fetch exchange rates, cache it and return.

Example

await getCurrencyRate('USD')

output:

{
  "rates": {
    "CAD": 1.3079191238,
    "HKD": 7.7531592249,
    "ISK": 137.7422072452,
    "PHP": 48.1819713564,
    "DKK": 6.2757371525,
    "HUF": 302.4599831508,
    "CZK": 22.4658803707,
    "GBP": 0.7618365628,
    .
    .
    .
    .
  },
  "base": "USD",
  "date": "2020-11-06"
}

2. getCurrencyRateList()

It will return an array of cached exchange rates for all supported currencies. If no cache found it will fetch exchange rates, cache it and return.

Example

await getCurrencyRateList()

output:

[
  {
    "rates": {
      "CAD": 0.0043242716,
      "HKD": 0.0256336694,
      "ISK": 0.455406384,
      "PHP": 0.1593003175,
      "DKK": 0.0207489833,
      "HUF": 1,
      "CZK": 0.074277199,
      "GBP": 0.0025188012,
      "RON": 0.0135563478,
      .
      .
      .
      .
    },
    "base": "HUF",
    "date": "2020-11-06"
  },
  {
    "rates": {
      "CAD": 0.2084088438,
      "HKD": 1.235418093,
      "ISK": 21.9483709879,
      "PHP": 7.6774998993,
      "DKK": 1,
      "HUF": 48.1951324286,
      .
      .
      .
      .
    },
    "base": "DKK",
    "date": "2020-11-06"
  },
  {
    "rates": {
      "CAD": 0.2053843101,
      "HKD": 1.2174890859,
      "ISK": 21.6298452176,
      "PHP": 7.5660801693,
      "DKK": 0.9854874983,
      "HUF": 47.4957004895,
      .
      .
      .
      .
    },
    "base": "HRK",
    "date": "2020-11-06"
  }
  .
  .
  .
  .
  .
]

3. formatCurrency(countryCode: string)

Developer can use their own conversion rates, or use above two methods. In the example I am using my own. Example

const conversionRates = {
  "rates": {
    "CAD": 1.3079191238,
    "INR": 74.25262772,
    "HKD": 7.7531592249,
    "ISK": 137.7422072452,
    "PHP": 48.1819713564,
    "DKK": 6.2757371525,
    "HUF": 302.4599831508,
    "CZK": 22.4658803707,
    "GBP": 0.7618365628,
    .
    .
    .
    .
  },
  "base": "USD",
  "date": "2020-11-06"
}
formatCurrency({
  rates: conversionRates.rates,
  amount: 100,
  from: 'USD',
  to: 'INR'
})
// output: '₹74'

or

formatCurrency({
  rates: conversionRates.rates,
  amount: 100,
  from: 'USD',
  country: 'IN'
})
// output: '₹74'

4. getCurrencyCode(countryCode: string)

Example

getCurrencyCode('US')

// output: 'USD'

5. getCurrencySymbol({ country?: string, currency?: string})

Example

getCurrencySymbol({ country: 'US' })
// output: '$'

or

getCurrencySymbol({ currency: 'USD' })
// output: '$'

Supported Currencies

  • Australian Dollar (AUD)
  • Bulgarian Lev (BGN)
  • Brazilian Real (BRL)
  • Canadian Dollar (CAD)
  • Swiss Franc (CHF)
  • Chinese Yuan Renminbi (CNY)
  • Czech Koruna (CZK)
  • Danish Krone (DKK)
  • European Union Euro (EUR)
  • Great Britain Pound (GBP)
  • Hong Kong Dollar (HKD)
  • Croatian Kuna (HRK)
  • Hungarian Forint (HUF)
  • Indonesian Rupiah (IDR)
  • New Israeli Sheqel (ILS)
  • Indian Rupee (INR)
  • Iceland Krona (ISK)
  • Japanese Yen (JPY)
  • Korean Won (KRW)
  • Mexican Peso (MXN)
  • Malaysian Ringgit (MYR)
  • Norwegian Krone (NOK)
  • New Zealand Dollar (NZD)
  • Philippine Peso (PHP)
  • Polis Zloty (PLN)
  • Romanian Leu (RON)
  • Russian Ruble (RUB)
  • Swedish Krona (SEK)
  • Singapore Dollar (SGD)
  • Thailand Baht (THB)
  • Turkish Lira (TRY)
  • United States Dollar (USD)
  • Rand (ZAR)

For now thats it!.

Working on to support more currencies...

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

Keywords

FAQs

Last updated on 16 Jun 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc