xchanger
Advanced tools
Comparing version 0.1.1 to 0.1.2
20
index.js
@@ -111,17 +111,17 @@ const fs = require('fs') | ||
XCHANGE.prototype.convert = async function(amount, to, from) { | ||
XCHANGE.prototype.convert = async function(amount, from, to) { | ||
try { | ||
if(!amount) { | ||
throw new Error('missing argument 1: amount') | ||
throw new Error('missing argument 1: `amount`') | ||
} else if(isNaN(amount)) { | ||
throw new Error('invalid argument 1: expected amount to be a number') | ||
throw new Error('invalid argument 1: expected `amount` to be a number') | ||
} | ||
if(!this.isValidCC(to)) { | ||
throw new Error('invalid/missing argument 2: to is either not a valid country code or is not provided') | ||
if(!this.isValidCC(from)) { | ||
throw new Error('invalid/missing argument 2: `from` is either not a valid country code or is not provided') | ||
} | ||
if(!from) { | ||
from = this.base | ||
if(!to) { | ||
to = this.base | ||
} | ||
if(!this.isValidCC(from)) { | ||
throw new Error('invalid/missing argument 3: from is not a valid country code') | ||
if(!this.isValidCC(to)) { | ||
throw new Error('invalid/missing argument 3: `to` is not a valid country code') | ||
} | ||
@@ -139,3 +139,3 @@ to = to.toString().toUpperCase() | ||
} | ||
let cf = this.getCF(to, from) | ||
let cf = this.getCF(from, to) | ||
return amount * cf | ||
@@ -142,0 +142,0 @@ } catch(error) { |
{ | ||
"name": "xchanger", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Currency exhange api with auto caching of api calls for better performance and bandwidth utilization", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -21,7 +21,12 @@ ~~~~ | ||
~~~~ | ||
const xchange = (require('xchanger'))() | ||
const XCHANGER = require('xchanger') | ||
const xchange = new XCHANGER() // or new XCHANGER(<currency code>) | ||
xchange.setBase('inr') //Set the base exchange currency to use as a default | ||
// Convert to currency specified as base currency | ||
xchange.convert(100, 'usd').then(result => { | ||
// Since we specified INR as base | ||
// the converted result will be in INR | ||
console.log(result) | ||
@@ -31,7 +36,74 @@ }).catch(error => { | ||
}) | ||
// Convert from usd to aud | ||
xchange.convert(100, 'usd', 'aud').then(result => { | ||
console.log(result) | ||
}).catch(error => { | ||
console.log(error) | ||
}) | ||
// Get a list of supported currency codes | ||
xchange.getValidCodes() | ||
~~~~ | ||
# Functions | ||
The module itself exports a constructor from which and exchange object can | ||
be created. | ||
Each exchange object has the following methods available - | ||
- sync() : Fetches the latest rates from the api. Returns a promise | ||
- saveRates() : Stores the rates to a local .json file. Returns a promise | ||
- loadRates() : Reads the rates from the local .json file. Returns a promise | ||
- setBase() : Sets the base exchange rate. (Available as the | ||
'base' attribute of the object) | ||
- getBase() : Gets the current base exchange rate set on the xchange | ||
- getValidCodes() : Returns a list of accepted Currency Codes | ||
- isValidCC() : Checks if a given Currency Code is valid for use | ||
- convert() : Converts a given amount from one currency to another | ||
# Supported Currencies | ||
Currently the api provides support for the 33 currencies listed below - | ||
- 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) | ||
Thats it! | ||
#Author | ||
Created and maintained by Hanut Singh Gusain <hanutsingh@gmail.com> [http://www.hanutsingh.in] | ||
# Author | ||
Created and maintained by Hanut Singh Gusain | ||
### My Links | ||
- [Github](https://github.com/hanut/) | ||
- [Website](http://www.hanutsingh.in) | ||
- [Twitter](http://www.twitter.com/hanutsingh) |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
108
70171
5
131
1