npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

currency-converter-global

v1.0.0

Published

A simple JavaScript library for currency conversion using real-time exchange rates. This library is compatible with both **Node.js** and **browser** environments, making it suitable for use in **Angular**, **React**, and **Vue** applications.

Downloads

4

Readme

Currency Converter

A simple JavaScript library for currency conversion using real-time exchange rates. This library is compatible with both Node.js and browser environments, making it suitable for use in Angular, React, and Vue applications.

Features

  • Fetches real-time exchange rates from Open Exchange Rates API.
  • Converts between any two supported currencies.
  • Easy integration with frontend frameworks like React, Angular, and Vue.
  • Works both server-side (Node.js) and client-side (browser environments).

Installation

You can install the package using npm:

npm install currency-converter

Usage

Node.js

const CurrencyConverter = require('currency-converter');

const converter = new CurrencyConverter('YOUR_API_KEY'); // Replace with your Open Exchange Rates API key

(async () => {
    try {
        const result = await converter.convert('USD', 'EUR', 100);
        console.log(`100 USD is equal to ${result.toFixed(2)} EUR`);
    } catch (error) {
        console.error(error.message);
    }
})();

Browser (React, Angular, Vue)

React Example

import React, { useState, useEffect } from 'react';
import CurrencyConverter from 'currency-converter';

const converter = new CurrencyConverter('YOUR_API_KEY');

const CurrencyConverterComponent = () => {
    const [convertedAmount, setConvertedAmount] = useState(0);

    useEffect(() => {
        async function convertCurrency() {
            try {
                const result = await converter.convert('USD', 'EUR', 100);
                setConvertedAmount(result);
            } catch (error) {
                console.error(error.message);
            }
        }

        convertCurrency();
    }, []);

    return <div>100 USD is equal to {convertedAmount.toFixed(2)} EUR</div>;
};

export default CurrencyConverterComponent;

Vue Example

<template>
  <div>
    100 USD is equal to {{ convertedAmount }} EUR
  </div>
</template>

<script>
import CurrencyConverter from 'currency-converter';

export default {
  data() {
    return {
      convertedAmount: 0
    };
  },
  mounted() {
    const converter = new CurrencyConverter('YOUR_API_KEY');
    converter.convert('USD', 'EUR', 100)
      .then(result => {
        this.convertedAmount = result.toFixed(2);
      })
      .catch(error => console.error(error.message));
  }
};
</script>

Angular Example

import { Component, OnInit } from '@angular/core';
import CurrencyConverter from 'currency-converter';

@Component({
  selector: 'app-currency-converter',
  template: `<p>100 USD is equal to {{ convertedAmount }} EUR</p>`,
})
export class CurrencyConverterComponent implements OnInit {
  convertedAmount: number = 0;
  converter = new CurrencyConverter('YOUR_API_KEY');

  ngOnInit(): void {
    this.converter.convert('USD', 'EUR', 100)
      .then(result => {
        this.convertedAmount = result.toFixed(2);
      })
      .catch(error => console.error(error.message));
  }
}

API

CurrencyConverter(apiKey)

Creates a new instance of the CurrencyConverter class. Requires an API key from the Open Exchange Rates service.

Parameters:

  • apiKey (string): Your Open Exchange Rates API key.

convert(fromCurrency, toCurrency, amount)

Converts an amount from one currency to another.

Parameters:

  • fromCurrency (string): The three-letter currency code of the base currency (e.g., USD).
  • toCurrency (string): The three-letter currency code of the target currency (e.g., EUR).
  • amount (number): The amount to be converted.

Returns:

  • A Promise that resolves to the converted amount.

Example:

const converter = new CurrencyConverter('YOUR_API_KEY');

converter.convert('USD', 'EUR', 100)
  .then(result => console.log(result))
  .catch(error => console.error(error));

Setup

Before using the library, ensure you have a valid API key from the Open Exchange Rates website. Sign up for an account, and get your API key for real-time exchange rates.

Build

If you're modifying the source code, you can bundle it for client-side usage by running the following command:

npm run build

This will create the bundled file currency-converter.js in the dist directory.

License

This project is licensed under the MIT License.

Contributing

Feel free to submit pull requests or open issues for improvements or bug fixes!