Skip to content

Commit

Permalink
v3.8.1: fixes cache keys
Browse files Browse the repository at this point in the history
  • Loading branch information
0xNe0x1 committed Sep 9, 2024
1 parent 5adcbea commit fef271f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 28 deletions.
22 changes: 13 additions & 9 deletions dist/esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,11 +437,7 @@ class Currency {
}

static getCacheKey(currency) {
const now = new Date();
const year = now.getUTCFullYear();
const month = now.getUTCMonth();
const day = now.getUTCDate();
return `@depay/local-currency/v3.6.1/rates/${currency}/${year}-${month}-${day}`
return `@depay/local-currency/v3.8.1/rates/${currency}`
}

static async rate({ from, to }) {
Expand All @@ -454,11 +450,19 @@ class Currency {
return fromToUsd.amount / toToUsd.amount
}

static getCachedValue(cacheKey) {
let cachedValue = localStorage.getItem(cacheKey);
if(cachedValue) {
cachedValue = JSON.parse(cachedValue);
cachedValue = cachedValue.expiresAt < Date.now() ? null : cachedValue.value;
}
return cachedValue
}

static fromUSDSync({ amount, code, timeZone }) {
let currency = new Currency({ amount, code, timeZone });
const cacheKey = Currency.getCacheKey(currency.code);
let cachedValue = localStorage.getItem(cacheKey);
let rate;
let cachedValue = Currency.getCachedValue(cacheKey);
if(cachedValue) {
rate = cachedValue;
} else {
Expand All @@ -473,7 +477,7 @@ class Currency {
static async fromUSD({ amount, code, timeZone }) {
let currency = new Currency({ amount, code, timeZone });
const cacheKey = Currency.getCacheKey(currency.code);
let cachedValue = localStorage.getItem(cacheKey);
let cachedValue = Currency.getCachedValue(cacheKey);
let rate;
if(cachedValue) {
rate = cachedValue;
Expand All @@ -482,7 +486,7 @@ class Currency {
.then((response) => response.json())
.then((data) => {
let value = parseFloat(data);
localStorage.setItem(cacheKey, value);
localStorage.setItem(cacheKey, JSON.stringify({ value, expiresAt: Date.now() + (24 * 60 * 60 * 1000) })); // 24 hours
return value
})
.catch((e)=>{
Expand Down
22 changes: 13 additions & 9 deletions dist/umd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,11 +443,7 @@
}

static getCacheKey(currency) {
const now = new Date();
const year = now.getUTCFullYear();
const month = now.getUTCMonth();
const day = now.getUTCDate();
return `@depay/local-currency/v3.6.1/rates/${currency}/${year}-${month}-${day}`
return `@depay/local-currency/v3.8.1/rates/${currency}`
}

static async rate({ from, to }) {
Expand All @@ -460,11 +456,19 @@
return fromToUsd.amount / toToUsd.amount
}

static getCachedValue(cacheKey) {
let cachedValue = localStorage.getItem(cacheKey);
if(cachedValue) {
cachedValue = JSON.parse(cachedValue);
cachedValue = cachedValue.expiresAt < Date.now() ? null : cachedValue.value;
}
return cachedValue
}

static fromUSDSync({ amount, code, timeZone }) {
let currency = new Currency({ amount, code, timeZone });
const cacheKey = Currency.getCacheKey(currency.code);
let cachedValue = localStorage.getItem(cacheKey);
let rate;
let cachedValue = Currency.getCachedValue(cacheKey);
if(cachedValue) {
rate = cachedValue;
} else {
Expand All @@ -479,7 +483,7 @@
static async fromUSD({ amount, code, timeZone }) {
let currency = new Currency({ amount, code, timeZone });
const cacheKey = Currency.getCacheKey(currency.code);
let cachedValue = localStorage.getItem(cacheKey);
let cachedValue = Currency.getCachedValue(cacheKey);
let rate;
if(cachedValue) {
rate = cachedValue;
Expand All @@ -488,7 +492,7 @@
.then((response) => response.json())
.then((data) => {
let value = parseFloat(data);
localStorage.setItem(cacheKey, value);
localStorage.setItem(cacheKey, JSON.stringify({ value, expiresAt: Date.now() + (24 * 60 * 60 * 1000) })); // 24 hours
return value
})
.catch((e)=>{
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@depay/local-currency",
"moduleName": "LocalCurrency",
"version": "3.8.0",
"version": "3.8.1",
"description": "JavaScript library that detects user's local currency and provides functionalities to convert between multiple currencies.",
"main": "dist/umd/index.js",
"module": "dist/esm/index.js",
Expand Down
22 changes: 13 additions & 9 deletions src/Currency.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ class Currency {
}

static getCacheKey(currency) {
const now = new Date()
const year = now.getUTCFullYear()
const month = now.getUTCMonth()
const day = now.getUTCDate()
return `@depay/local-currency/v3.6.1/rates/${currency}/${year}-${month}-${day}`
return `@depay/local-currency/v3.8.1/rates/${currency}`
}

static async rate({ from, to }) {
Expand All @@ -29,11 +25,19 @@ class Currency {
return fromToUsd.amount / toToUsd.amount
}

static getCachedValue(cacheKey) {
let cachedValue = localStorage.getItem(cacheKey)
if(cachedValue) {
cachedValue = JSON.parse(cachedValue)
cachedValue = cachedValue.expiresAt < Date.now() ? null : cachedValue.value
}
return cachedValue
}

static fromUSDSync({ amount, code, timeZone }) {
let currency = new Currency({ amount, code, timeZone })
const cacheKey = Currency.getCacheKey(currency.code)
let cachedValue = localStorage.getItem(cacheKey)
let rate
let cachedValue = Currency.getCachedValue(cacheKey)
if(cachedValue) {
rate = cachedValue
} else {
Expand All @@ -48,7 +52,7 @@ class Currency {
static async fromUSD({ amount, code, timeZone }) {
let currency = new Currency({ amount, code, timeZone })
const cacheKey = Currency.getCacheKey(currency.code)
let cachedValue = localStorage.getItem(cacheKey)
let cachedValue = Currency.getCachedValue(cacheKey)
let rate
if(cachedValue) {
rate = cachedValue
Expand All @@ -57,7 +61,7 @@ class Currency {
.then((response) => response.json())
.then((data) => {
let value = parseFloat(data)
localStorage.setItem(cacheKey, value)
localStorage.setItem(cacheKey, JSON.stringify({ value, expiresAt: Date.now() + (24 * 60 * 60 * 1000) })) // 24 hours
return value
})
.catch((e)=>{
Expand Down

0 comments on commit fef271f

Please sign in to comment.