-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
convert.ts
30 lines (27 loc) · 888 Bytes
/
convert.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import getRate from './lib/get-rate';
import {Options} from './lib/options';
import parse from './utils/parser';
/**
* Function, which converts currencies based on provided rates.
*
* @param {number | string} amount - Amount of money you want to convert.
* @param {Object} options - Conversion options.
* @return {number} Conversion result.
*
* @example
* const rates = {
* GBP: 0.92,
* EUR: 1.00,
* USD: 1.12
* };
*
* convert(10, {from: 'EUR', to: 'GBP', base: 'EUR', rates}); //=> 9.2
*/
export default function convert(amount: number | string, {from, to, base, rates}: Options): number {
// If provided `amount` is a string, use parsing
if (typeof amount === 'string') {
const data = parse(amount);
return (data.amount * 100) * getRate(base, rates, data.from ?? from, data.to ?? to) / 100;
}
return (amount * 100) * getRate(base, rates, from, to) / 100;
}