Skip to content

Commit d20d9ed

Browse files
committed
Improve docs, add section about integrating with big.js
1 parent 25877cd commit d20d9ed

File tree

1 file changed

+73
-30
lines changed

1 file changed

+73
-30
lines changed

readme.md

+73-30
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
> Lightweight currency conversion library, successor of money.js
44
5-
[![Build Status](https://travis-ci.org/xxczaki/cashify.svg?branch=master)](https://travis-ci.org/xxczaki/cashify)
5+
[![Build Status](https://github.com/xxczaki/cashify/workflows/CI/badge.svg)](https://github.com/xxczaki/cashify/actions?query=workflow%3ACI)
66
[![Coverage Status](https://coveralls.io/repos/github/xxczaki/cashify/badge.svg?branch=master)](https://coveralls.io/github/xxczaki/cashify?branch=master)
77
[![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/xojs/xo)
88
[![install size](https://packagephobia.now.sh/badge?p=cashify)](https://packagephobia.now.sh/result?p=cashify)
@@ -16,17 +16,20 @@
1616
- [With constructor](#with-constructor)
1717
- [Without constructor](#without-constructor)
1818
- [Parsing](#parsing)
19-
- [Integration with currency.js](#integration)
19+
- [Integration with big.js](#integration-bigjs)
20+
- [Integration with currency.js](#integration-currencyjs)
2021
- [API](#api)
2122
- [Cashify({base, rates})](#cashifybase-rates)
2223
- [base](#base)
2324
- [rates](#rates)
25+
- [BigJs](#bigjs)
2426
- [convert(amount, {from, to, base, rates})](#convertamount-from-to-base-rates-with-and-without-constructor)
2527
- [amount](#amount)
2628
- [from](#from)
2729
- [to](#to)
2830
- [base](#base-1)
2931
- [rates](#rates-1)
32+
- [BigJs](#bigjs-1)
3033
- [parse(expression)](#parseexpression)
3134
- [expression](#expression)
3235
- [Migrating from money.js](#migrating-from-moneyjs)
@@ -51,22 +54,25 @@ This package was created, because the popular [money.js](http://openexchangerate
5154
- Simple API
5255
- 0 dependencies
5356
- Actively maintained
54-
- Well tested
57+
- Well tested and documented
5558
- [Easy migration from money.js](#migrating-from-moneyjs)
5659
- Written in TypeScript
60+
- ESM-only
5761

5862
## Install
5963

6064
```
6165
$ npm install cashify
6266
```
6367

68+
**Please note that starting with version `3.0.0` this package is ESM-only and thus requires Node.js v14 or higher.**
69+
6470
## Usage
6571

6672
### With constructor
6773

6874
```js
69-
const {Cashify} = require('cashify');
75+
import {Cashify} from 'cashify';
7076

7177
const rates = {
7278
GBP: 0.92,
@@ -86,7 +92,7 @@ console.log(result); //=> 9.2
8692
Using the `Cashify` constructor is not required. Instead, you can just use the `convert` function:
8793

8894
```js
89-
const {convert} = require('cashify');
95+
import {convert} from 'cashify';
9096

9197
const rates = {
9298
GBP: 0.92,
@@ -104,7 +110,7 @@ console.log(result); //=> 9.2
104110
Cashify supports parsing, so you can pass a `string` to the `amount` argument and the `from` and/or `to` currency will be automatically detected:
105111

106112
```js
107-
const {Cashify} = require('cashify');
113+
import {Cashify} from 'cashify';
108114

109115
const rates = {
110116
GBP: 0.92,
@@ -121,15 +127,15 @@ cashify.convert('€10 EUR', {to: 'GBP'});
121127
cashify.convert('10 EUR to GBP');
122128
```
123129

124-
Alternatively, if you just want to parse a `string` without conversion, you can use the [`parse`](#parseexpression) function, which returns an `object` with parsing results:
130+
Alternatively, if you just want to parse a `string` without conversion you can use the [`parse`](#parseexpression) function which returns an `object` with parsing results:
125131

126132
```js
127-
const {parse} = require('cashify');
133+
import {parse} from 'cashify';
128134

129135
parse('10 EUR to GBP'); //=> {amount: 10, from: 'EUR', to: 'GBP'}
130136
```
131137

132-
**Note:** If you want to use full parsing, you need to pass a `string` with specific format:
138+
**Note:** If you want to use full parsing, you need to pass a `string` in a specific format:
133139

134140
```
135141
10 usd to pln
@@ -139,15 +145,41 @@ parse('10 EUR to GBP'); //=> {amount: 10, from: 'EUR', to: 'GBP'}
139145

140146
You can use `to`, `in` or `as` to separate the expression (case insensitive). Used currencies name case doesn't matter, as cashify will automatically convert them to upper case.
141147

142-
<a id="integration"></a>
148+
<a id="integration-bigjs"></a>
149+
150+
### Integration with [big.js](https://github.com/MikeMcl/big.js/)
151+
152+
[big.js](https://github.com/scurker/currency.js/) is a small JavaScript library for arbitrary-precision decimal arithmetic. You can use it with cashify to make sure you won't run into floating point issues:
153+
154+
```js
155+
import {Cashify} from 'cashify';
156+
import Big from 'big.js';
157+
158+
const rates = {
159+
EUR: 0.8235,
160+
USD: 1
161+
};
162+
163+
const cashify = new Cashify({base: 'USD', rates});
164+
165+
const result = cashify.convert(1, {
166+
from: 'USD',
167+
to: 'EUR',
168+
BigJs: Big
169+
});
170+
171+
console.log(result); //=> 8.235 (without big.js you would get something like 0.8234999999999999)
172+
```
173+
174+
<a id="integration-currencyjs"></a>
143175

144176
### Integration with [currency.js](https://github.com/scurker/currency.js/)
145177

146-
[currency.js](https://github.com/scurker/currency.js/) is a small and lightweight library for working with currency values. It works great with cashify. In the following example we are using it to format the conversion result:
178+
[currency.js](https://github.com/scurker/currency.js/) is a small and lightweight library for working with currency values. It integrates well with cashify. In the following example we are using it to format the conversion result:
147179

148180
```js
149-
const {Cashify} = require('cashify');
150-
const currency = require('currency.js');
181+
import {Cashify} from 'cashify';
182+
import currency from 'currency.js';
151183

152184
const rates = {
153185
GBP: 0.92,
@@ -165,25 +197,31 @@ currency(converted, {symbol: '€', formatWithSymbol: true}).format(); // => €
165197

166198
## API
167199

168-
### Cashify({base, rates})
200+
### Cashify({base, rates, BigJs})
169201

170-
Constructor
202+
Constructor.
171203

172204
##### base
173205

174206
Type: `string`
175207

176-
Base currency
208+
The base currency.
177209

178210
##### rates
179211

180212
Type: `object`
181213

182-
Object containing currency rates (for example from an API, such as Open Exchange Rates)
214+
An object containing currency rates (for example from an API, such as Open Exchange Rates).
215+
216+
##### BigJs
217+
218+
Type: [big.js](https://github.com/MikeMcl/big.js/) constructor
219+
220+
See [integration with big.js](#integration-bigjs).
183221

184222
### convert(amount, {from, to, base, rates}) *`with and without constructor`*
185223

186-
Returns conversion result (`number`)
224+
Returns conversion result (`number`).
187225

188226
##### amount
189227

@@ -207,13 +245,19 @@ Currency to which you want to convert. You might not need to specify it if you a
207245

208246
Type: `string`
209247

210-
Base currency
248+
The base currency.
211249

212250
##### rates
213251

214252
Type: `object`
215253

216-
Object containing currency rates (for example from an API, such as Open Exchange Rates)
254+
An object containing currency rates (for example from an API, such as Open Exchange Rates).
255+
256+
##### BigJs
257+
258+
Type: [big.js](https://github.com/MikeMcl/big.js/) constructor
259+
260+
See [integration with big.js](#integration-bigjs).
217261

218262
### parse(expression)
219263

@@ -238,8 +282,8 @@ Expression you want to parse, ex. `10 usd to pln` or `€1.23 eur`
238282
With `Cashify` constructor:
239283

240284
```diff
241-
- const fx = require('money');
242-
+ const {Cashify} = require('cashify');
285+
- import fx from 'money';
286+
+ import {Cashify} from 'cashify';
243287

244288
- fx.base = 'EUR';
245289
- fx.rates = {
@@ -263,8 +307,8 @@ With `Cashify` constructor:
263307
With `convert` function:
264308

265309
```diff
266-
- const fx = require('money');
267-
+ const {convert} = require('cashify');
310+
- import fx from 'money';
311+
+ import {convert} from 'cashify';
268312

269313
- fx.base = 'EUR';
270314
- fx.rates = {
@@ -290,8 +334,8 @@ When working with currencies, decimals only need to be precise up to the smalles
290334
Let's take a look at the following example:
291335

292336
```js
293-
const fx = require('money');
294-
const {Cashify} = require('cashify');
337+
import fx from 'money';
338+
import {Cashify} from 'cashify';
295339

296340
const rates = {
297341
GBP: 0.92,
@@ -309,14 +353,13 @@ cashify.convert(10, {from: 'EUR', to: 'GBP'}); //=> 9.2
309353

310354
As you can see, money.js doesn't handle currencies correctly and therefore a floating point issues are occuring. Even though there's just a minor discrepancy between the results, if you're converting large amounts, that can add up.
311355

312-
Cashify solves this problem the same way as [currency.js](https://github.com/scurker/currency.js/) - by working with integers behind the scenes. This should be okay for most reasonable values of currencies.
356+
Cashify solves this problem the same way as [currency.js](https://github.com/scurker/currency.js/) - by working with integers behind the scenes. **This should be okay for most reasonable values of currencies**; if you want to avoid all floating point issues, see [integration with big.js]().
313357

314358
## Related projects
315359

316-
* [nestjs-cashify](https://github.com/vahidvdn/nestjs-cashify) - nodejs cashify module for nestjs
317-
* [currency.js](https://github.com/scurker/currency.js/) - Lightweight javascript library for working with currency values.
360+
* [nestjs-cashify](https://github.com/vahidvdn/nestjs-cashify) - Node.js Cashify module for Nest.js.
318361
* [cashify-rs](https://github.com/xxczaki/cashify-rs) - Cashify port for Rust.
319362

320363
## License
321364

322-
MIT © [Antoni Kepinski](https://kepinski.me)
365+
MIT © [Antoni Kępiński](https://www.kepinski.ch)

0 commit comments

Comments
 (0)