Skip to content

Commit

Permalink
Improved rates caching
Browse files Browse the repository at this point in the history
  • Loading branch information
olarotseyi committed Jun 28, 2022
1 parent 1265b16 commit c0e287c
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 7 deletions.
42 changes: 37 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ class CurrencyConverter {
this.currencyTo = ""
this.currencyAmount = 1
this.convertedValue = 0
this.isDecimalComma = false
this.isDecimalComma = false;
this.isRatesCaching = false;
this.ratesCacheDuration = 0;
this.ratesCache = {};

if(params != undefined){
Expand Down Expand Up @@ -228,13 +230,41 @@ class CurrencyConverter {
return text_
}

setupRatesCache(ratesCacheOptions) {
if (typeof ratesCacheOptions != "object")
throw new TypeError("ratesCacheOptions should be an object")

if (ratesCacheOptions.isRatesCaching === undefined)
throw new Error(`ratesCacheOptions should have a property called isRatesCaching`)

if (typeof ratesCacheOptions.isRatesCaching != "boolean")
throw new TypeError("ratesCacheOptions.isRatesCaching should be a boolean")

if (typeof ratesCacheOptions.ratesCacheDuration != "number")
throw new TypeError("ratesCacheOptions.ratesCacheDuration should be a number")

if(ratesCacheOptions.ratesCacheDuration <= 0)
throw new Error("ratesCacheOptions.ratesCacheDuration should be a positive number of seconds")

this.isRatesCaching = ratesCacheOptions.isRatesCaching;

if (ratesCacheOptions.ratesCacheDuration === undefined)
this.ratesCacheDuration = 3600000; // Defaults to 3600 seconds (1 hour)
else
this.ratesCacheDuration = ratesCacheOptions.ratesCacheDuration * 1000;

return this
}

rates(){
if(this.currencyFrom === this.currencyTo)
return new Promise((resolve, _) => {resolve(this.currencyAmount) })
else
let currencyPair = this.currencyFrom.toUpperCase() + this.currencyTo.toUpperCase();
if (currencyPair in this.ratesCache)
return this.ratesCache[currencyPair];
return new Promise((resolve, _) => {
resolve(this.ratesCache[currencyPair]);
});
else
return got(`https://www.google.co.in/search?q=${this.currencyFrom}+to+${this.currencyTo}+&hl=en`)
.then((html) => {
Expand All @@ -251,8 +281,10 @@ class CurrencyConverter {
if(rates.includes(","))
rates = this.replaceAll(rates, ",", "")
}
this.ratesCache[currencyPair] = parseFloat(rates);
this.removeCurrencyPairFromRatesCache(currencyPair);
if (this.isRatesCaching) {
this.ratesCache[currencyPair] = parseFloat(rates);
this.removeCurrencyPairFromRatesCache(currencyPair);
}
return parseFloat(rates)
})
}
Expand Down Expand Up @@ -296,7 +328,7 @@ class CurrencyConverter {
// Deletes cached currencyPair rate an hour later
setTimeout(function() {
delete this.ratesCache[currencyPair];
}, 3600000);
}, this.ratesCacheDuration);
}
}

Expand Down
17 changes: 17 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,23 @@ currencyConverter.rates().then((response) => {
})
```

Rates can be cached. To implement rate caching, instantiate an object of CurrencyConverter only once in your project, in a CurrencyConverter file, and setup rates caching then import the instance of CurrencyConverter from the CurrencyConverter file in your project across the rest of your project. Use chaining to convert currencies when caching is implemented. Below is an example of a CurrencyConverter file.

```javascript
const CC = require('currency-converter-lt')

let currencyConverter = new CC()

let ratesCacheOptions = {
isRatesCaching: true, // Set this boolean to true to implement rate caching
ratesCacheDuration: 3600 // Set this to a positive number to set the number of seconds you want your rates to be cached. Defaults to 3600 seconds (1 hour)
}

currencyConverter = currencyConverter.setupRatesCache(ratesCacheOptions)

module.exports = currencyConverter
```

Chaining is also supported.

```javascript
Expand Down
40 changes: 38 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe("currencyConverter", () => {
let CC_ = new CC()
assert.equal(CC_.currencyFrom, "")
})

it("should instantiate an object with json object as a parameter", () => {
let CC_ = new CC({from:"GBP", to:"CAD", amount: 100})
assert.equal(CC_.currencyFrom, "GBP")
Expand Down Expand Up @@ -178,9 +178,45 @@ describe("currencyConverter", () => {
expect(() => currencyConverter.currencyName("DDD")).to.throw(Error);
})
})

describe("setupRatesCache", () => {
it("should throw a TypeError", () => {
expect(() => currencyConverter.setupRatesCache(5)).to.throw(TypeError);
})

it("should throw a TypeError", () => {
let ratesCacheOptions = {
isRatesCaching: 3
}
expect(() => currencyConverter.setupRatesCache(ratesCacheOptions)).to.throw(TypeError);
})

it("should throw a TypeError", () => {
let ratesCacheOptions = {
isRatesCaching: true,
ratesCacheDuration: "10"
}
expect(() => currencyConverter.setupRatesCache(ratesCacheOptions)).to.throw(TypeError);
})

it("should throw an Error", () => {
let ratesCacheOptions = {
ratesCacheDuration: 50
}
expect(() => currencyConverter.setupRatesCache(ratesCacheOptions)).to.throw(Error);
})

it("should throw an Error", () => {
let ratesCacheOptions = {
isRatesCaching: true,
ratesCacheDuration: -50
}
expect(() => currencyConverter.setupRatesCache(ratesCacheOptions)).to.throw(Error);
})
})
})

// console.log(cf)


// console.log(c.currencies)
// console.log(c.currencies)

0 comments on commit c0e287c

Please sign in to comment.