Skip to content

Commit

Permalink
v3.3.0: adds .rate({ from, to })
Browse files Browse the repository at this point in the history
  • Loading branch information
10xSebastian committed Jun 26, 2022
1 parent ad9177e commit d5eae16
Show file tree
Hide file tree
Showing 7 changed files with 615 additions and 9 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ currency.toString()
// €16.88
```

### rate

Gets rate for given `from` and `to`:

```javascript
let rate = await Currency.rate({ from: 'EUR', to: 'GBP' })
// 1.1585365853658536
```

### getCode

Gives you the local currency code:
Expand Down
7 changes: 7 additions & 0 deletions dist/esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,13 @@ class Currency {
return timezoneToCurrency[timeZone || Currency.timeZone()] || 'USD'
}

static async rate({ from, to }) {
if(to == undefined) { to = Currency.getCode(); }
let fromToUsd = await Currency.fromUSD({ amount: 1, code: from });
let toToUsd = await Currency.fromUSD({ amount: 1, code: to });
return fromToUsd.amount / toToUsd.amount
}

static async fromUSD({ amount, code, timeZone }) {
let currency = new Currency({ amount, code, timeZone });
let rate = await fetch('https://public.depay.fi/currencies/' + currency.code)
Expand Down
7 changes: 7 additions & 0 deletions dist/umd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,13 @@
return timezoneToCurrency[timeZone || Currency.timeZone()] || 'USD'
}

static async rate({ from, to }) {
if(to == undefined) { to = Currency.getCode(); }
let fromToUsd = await Currency.fromUSD({ amount: 1, code: from });
let toToUsd = await Currency.fromUSD({ amount: 1, code: to });
return fromToUsd.amount / toToUsd.amount
}

static async fromUSD({ amount, code, timeZone }) {
let currency = new Currency({ amount, code, timeZone });
let rate = await fetch('https://public.depay.fi/currencies/' + currency.code)
Expand Down
3 changes: 2 additions & 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.2.2",
"version": "3.3.0",
"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 Expand Up @@ -54,6 +54,7 @@
"eslint-plugin-react": "^7.21.5",
"eslint-plugin-react-hooks": "^4.2.0",
"fetch-mock": "^9.11.0",
"jest-environment-jsdom": "^28.1.1",
"node-fetch": "^2.6.1",
"regenerator-runtime": "^0.13.7",
"rollup": "^2.34.2",
Expand Down
7 changes: 7 additions & 0 deletions src/Currency.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ class Currency {
return timezoneToCurrency[timeZone || Currency.timeZone()] || 'USD'
}

static async rate({ from, to }) {
if(to == undefined) { to = Currency.getCode() }
let fromToUsd = await Currency.fromUSD({ amount: 1, code: from })
let toToUsd = await Currency.fromUSD({ amount: 1, code: to })
return fromToUsd.amount / toToUsd.amount
}

static async fromUSD({ amount, code, timeZone }) {
let currency = new Currency({ amount, code, timeZone })
let rate = await fetch('https://public.depay.fi/currencies/' + currency.code)
Expand Down
21 changes: 21 additions & 0 deletions tests/units/Currency.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,25 @@ describe('Currency', () => {
Currency.getCode('Europe/Berlin')
).toEqual('EUR')
})

describe('fetch and provide rates for given from/to', ()=>{

beforeEach(()=>{
fetchMock.get({
url: 'https://public.depay.fi/currencies/EUR',
overwriteRoutes: true
}, "0.95"
)
fetchMock.get({
url: 'https://public.depay.fi/currencies/GBP',
overwriteRoutes: true
}, "0.82"
)
})

it.only('provides rate for given from to', async ()=> {
let rate = await Currency.rate({ from: 'EUR', to: 'GBP' })
expect(rate).toEqual(1.1585365853658536)
})
})
});
Loading

0 comments on commit d5eae16

Please sign in to comment.