Skip to content

Commit cb6ae41

Browse files
authored
1.2.0
2 parents 1146d36 + a3d1fa9 commit cb6ae41

File tree

6 files changed

+114
-84
lines changed

6 files changed

+114
-84
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cashify",
3-
"version": "1.1.3",
3+
"version": "1.2.0",
44
"description": "Lightweight currency conversion library, successor of money.js",
55
"main": "dist/index.js",
66
"module": "dist/index.esm.js",
@@ -51,7 +51,7 @@
5151
"@akepinski/tsconfig": "0.0.2",
5252
"@typescript-eslint/eslint-plugin": "^1.13.0",
5353
"@typescript-eslint/parser": "^1.13.0",
54-
"ava": "^2.2.0",
54+
"ava": "^2.3.0",
5555
"browser-env": "^3.2.6",
5656
"coveralls": "^3.0.6",
5757
"cpy-cli": "^2.0.0",

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
[![Build Status](https://travis-ci.org/xxczaki/cashify.svg?branch=master)](https://travis-ci.org/xxczaki/cashify) [![Coverage Status](https://coveralls.io/repos/github/xxczaki/cashify/badge.svg?branch=master)](https://coveralls.io/github/xxczaki/cashify?branch=master) [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/xojs/xo) [![install size](https://packagephobia.now.sh/badge?p=cashify)](https://packagephobia.now.sh/result?p=cashify)
66

7-
This package was created, because the popular [money.js]() library is:
7+
This package was created, because the popular [money.js](http://openexchangerates.github.io/money.js/) library is:
88
* not maintained (last commit was ~5 years ago)
99
* has over 20 issues open
1010
* does not support TypeScript

src/index.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,31 @@ interface Options {
77
rates: object;
88
}
99

10+
// Small helper for TypeScript
11+
function hasKey<T>(obj: T, key: string | number | symbol): key is keyof T {
12+
return key in obj;
13+
}
14+
1015
const getRate = ({base, rates, from, to}: Options): number => {
1116
// If `from` equals `base`, return the basic exchange rate for the `to` currency
12-
if (from === base) {
13-
// @ts-ignore
17+
if (from === base && hasKey(rates, to)) {
1418
return rates[to];
1519
}
1620

1721
// If `to` equals `base`, return the basic inverse rate of the `from` currency
18-
if (to === base) {
19-
// @ts-ignore
22+
if (to === base && hasKey(rates, from)) {
2023
return 1 / rates[from];
2124
}
2225

2326
/**
2427
Otherwise, return the `to` rate multipled by the inverse of the `from` rate to get the
2528
relative exchange rate between the two currencies
26-
*/
27-
// @ts-ignore
28-
return rates[to] * (1 / rates[from]);
29+
*/
30+
if (hasKey(rates, from) && hasKey(rates, to)) {
31+
return rates[to] * (1 / rates[from]);
32+
}
33+
34+
throw new Error('`rates` object does not contain either `from` or `to` currency!');
2935
};
3036

3137
class Cashify {
@@ -44,7 +50,7 @@ class Cashify {
4450
}
4551

4652
/**
47-
* @param {number} amount Amount of money you want to convert (in decimal, e.g., $1 = 1.00)
53+
* @param {number} amount Amount of money you want to convert
4854
* @param {object} options Conversion options
4955
* @param {string} options.from Currency from which you want to convert
5056
* @param {string} options.to Currency to which you want to convert
@@ -63,7 +69,7 @@ class Cashify {
6369
}
6470

6571
/**
66-
* @param {number} amount Amount of money you want to convert (in decimal, e.g., $1 = 1.00)
72+
* @param {number} amount Amount of money you want to convert
6773
* @param {object} options Conversion options
6874
* @param {string} options.from Currency from which you want to convert
6975
* @param {string} options.to Currency to which you want to convert

test.ts

Lines changed: 0 additions & 72 deletions
This file was deleted.

test/constructor.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import test from 'ava';
2+
import {Cashify} from '../dist';
3+
4+
const rates = {
5+
GBP: 0.92,
6+
EUR: 1.00,
7+
USD: 1.12
8+
};
9+
10+
const cashify = new Cashify({base: 'EUR', rates});
11+
12+
test('basic conversion', t => {
13+
t.is(cashify.convert(12, {from: 'USD', to: 'GBP'}), 9.857142857142856);
14+
});
15+
16+
test('`from` equals `base`', t => {
17+
t.is(cashify.convert(10, {from: 'EUR', to: 'GBP'}), 9.2);
18+
});
19+
20+
test('`to` equals `base`', t => {
21+
t.is(cashify.convert(10, {from: 'GBP', to: 'EUR'}), 10.869565217391303);
22+
});
23+
24+
test('`from` equals `to`', t => {
25+
t.is(cashify.convert(10, {from: 'USD', to: 'USD'}), 10);
26+
});
27+
28+
test('`from` equals `to`, but `base` is different', t => {
29+
const cashify = new Cashify({base: 'USD', rates});
30+
31+
t.is(cashify.convert(10, {from: 'EUR', to: 'EUR'}), 10);
32+
});
33+
34+
test('`rates` without `base` currency', t => {
35+
const rates = {
36+
GBP: 0.92,
37+
USD: 1.12
38+
};
39+
40+
const cashify = new Cashify({base: 'EUR', rates});
41+
42+
t.is(cashify.convert(10, {from: 'EUR', to: 'GBP'}), 9.2);
43+
});
44+
45+
test('`rates` object does not contain either `from` or `to` currency', t => {
46+
const error = t.throws(() => {
47+
cashify.convert(10, {from: 'CHF', to: 'EUR'});
48+
}, Error);
49+
50+
t.is(error.message, '`rates` object does not contain either `from` or `to` currency!');
51+
});

test/function.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import test from 'ava';
2+
import {convert} from '../dist';
3+
4+
const rates = {
5+
GBP: 0.92,
6+
EUR: 1.00,
7+
USD: 1.12
8+
};
9+
10+
test('basic conversion', t => {
11+
t.is(convert(12, {from: 'USD', to: 'GBP', base: 'EUR', rates}), 9.857142857142856);
12+
});
13+
14+
test('`from` equals `base`', t => {
15+
t.is(convert(10, {from: 'EUR', to: 'GBP', base: 'EUR', rates}), 9.2);
16+
});
17+
18+
test('`to` equals `base`', t => {
19+
t.is(convert(10, {from: 'GBP', to: 'EUR', base: 'EUR', rates}), 10.869565217391303);
20+
});
21+
22+
test('`from` equals `to`', t => {
23+
t.is(convert(10, {from: 'USD', to: 'USD', base: 'EUR', rates}), 10);
24+
});
25+
26+
test('`from` equals `to`, but `base` is different', t => {
27+
t.is(convert(10, {from: 'EUR', to: 'EUR', base: 'USD', rates}), 10);
28+
});
29+
30+
test('`rates` without `base` currency', t => {
31+
const rates = {
32+
GBP: 0.92,
33+
USD: 1.12
34+
};
35+
36+
t.is(convert(10, {from: 'EUR', to: 'GBP', base: 'EUR', rates}), 9.2);
37+
});
38+
39+
test('`rates` object does not contain either `from` or `to` currency', t => {
40+
const error = t.throws(() => {
41+
convert(10, {from: 'CHF', to: 'EUR', base: 'EUR', rates});
42+
}, Error);
43+
44+
t.is(error.message, '`rates` object does not contain either `from` or `to` currency!');
45+
});

0 commit comments

Comments
 (0)