Skip to content

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
ruigomeseu committed May 24, 2018
1 parent 5cfe0cd commit 7399d0b
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 3 deletions.
108 changes: 107 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![](https://ruigomes.me/bitcoin-units.png)
![](https://ruigomes.me/bitcoin-units.png?v=2)

# bitcoin-units
[![Build Status](https://img.shields.io/travis/ruigomeseu/bitcoin-units.svg)](https://travis-ci.org/ruigomeseu/bitcoin-units) [![codecov](https://img.shields.io/codecov/c/github/ruigomeseu/bitcoin-units.svg)](https://codecov.io/gh/ruigomeseu/bitcoin-units) [![npm version](https://badge.fury.io/js/bitcoin-units.svg)](https://badge.fury.io/js/bitcoin-units)
Expand All @@ -22,3 +22,109 @@ Or NPM:
```bash
npm install bitcoin-units --save
```

## Usage

### Importing
Import using ES6:

```js
import bitcoin from 'bitcoin-units';
```

Or AMD:

```js
var bitcoin = require('bitcoin-units');
```

### Converting bitcoin

Basic unit conversions are done using the `to(unit)` function:
```js
bitcoin(1, 'mBTC').to('BTC').value()
0.001
```

#### Available units

There are 4 available units by default:
`btc`, `mbtc`, `bit` and `satoshi`.

You can also use any of the following aliases:

`btc`: `bitcoin` or `bitcoins`

`mbtc`: `millibtc`

`bit`: `μbtc` or `microbtc`

`satoshi`: `sat`, `sats`, `satoshi`, `satoshis`



#### Available getters

`value()` returns a `Number`:
```js
bitcoin(1, 'mBTC').to('BTC').value()
0.001
```

`toString()` returns a `string`:
```js
bitcoin(100000, 'satoshi').to('BTC').format()
'0.001'
```

`format()` returns a string with the value and unit:
```js
bitcoin(100000, 'satoshi').to('BTC').format()
'0.001 BTC'
```

### Customization

#### Format
You can set your own custom units using the `bitcoin.setDisplay(unit, options)`:

```js
bitcoin.setDisplay('satoshi', {
unit: 'custom'
});

bitcoin(1, 'BTC').to('satoshi').format();

'100000000 custom'
```

#### Custom Unit
If you want to create your own custom units, you can use the `bitcoin.setUnit(unit, value)`:
```js
bitcoin.setUnit('custom', 1 / 1E2);

bitcoin(1, 'btc').to('custom').value();

100
```

You can use this to create your own conversion to fiat currencies:

```js
// Let's suppose a rate of 1 BTC = $8000.00

bitcoin.setUnit('usd', 1 / 8000);

bitcoin(1, 'satoshi').to('usd').value();

0.00008
```

There's a shorthand to this function named `bitcoin.setFiat(unit, rate)`:
```js
bitcoin.setFiat('usd', 8000);

bitcoin(1, 'satoshi').to('usd').value();

0.00008
```
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "bitcoin-units",
"version": "0.1.0",
"version": "0.1.1",
"description": "Easily convert between BTC, mBTC and Satoshis.",
"main": "index.js",
"main": "./lib/index.js",
"scripts": {
"test": "mocha --require babel-core/register --require babel-polyfill --recursive",
"build": "cross-env BABEL_ENV=production babel src --out-dir lib",
Expand Down

0 comments on commit 7399d0b

Please sign in to comment.