Skip to content

Commit 896f6e3

Browse files
committed
generate documentation
1 parent 8869e3c commit 896f6e3

File tree

12 files changed

+847
-9821
lines changed

12 files changed

+847
-9821
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"es6": true
77
},
88
"parserOptions": {
9-
"sourceType": "module"
9+
"sourceType": "module",
10+
"ecmaVersion": 8
1011
}
1112
}

docs/00-index.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<header>
2+
<img src="https://user-images.githubusercontent.com/1062039/31397824-9dfa15f0-adac-11e7-9869-fb20746e90c1.png" alt="currency logo">
3+
<h1>currency.js</h1>
4+
<p>{{description}}</p>
5+
<a class="btn" href="https://unpkg.com/currency.js/{{dist}}">Download currency.js</a>
6+
<p><strong>v{{version}}</strong> <em>({{size}})</em></p>
7+
<aside class="center">
8+
<a class="github-button" href="https://github.com/scurker/currency.js" data-icon="octicon-star" data-size="large" data-show-count="true" aria-label="Star scurker/currency.js on GitHub">Star</a>
9+
</aside>
10+
</header>

docs/01-usage.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
## Usage
2+
3+
*currency.js* was built to work around common floating point issues in javascript with a flexible api.
4+
5+
When working with currencies, decimals only need to be precise up to the smallest cent value while avoiding common floating point errors when performing basic arithmetic. *currency.js* resolves this issue by working with integers behind the scenes, so you don't have to be concerned about decimal precision.
6+
7+
For more details on why Javascript has issues with floating point numbers, there's an excellent talk by Bartek Szopka on [everything you never wanted to know about Javascript numbers](http://www.youtube.com/watch?v=MqHDDtVYJRI) explaining why Javascript and other IEEE 754 implementations have floating point issues.
8+
9+
*currency.js* will work with a range of inputs, including `strings`, `numbers`, `decimals`, or another `currency` object.
10+
11+
```js
12+
// Numbers
13+
currency(1); // => "1.00"
14+
currency(123); // => "123.00"
15+
16+
// Decimals
17+
currency(1.00); // => "1.00"
18+
currency(1.23); // => "1.23"
19+
20+
// Strings
21+
currency("1.23"); // => "1.23"
22+
currency("$12.30"); // => "12.23"
23+
currency("£1,234,567.89"); // => "1,234,567.89"
24+
25+
// Currency
26+
let c1 = currency(1.23);
27+
let c2 = currency(4.56);
28+
currency(7.89).add(c1).add(c2); // => "13.68"
29+
```
30+
31+
Currency values are handled transparently behind the scenes, so you don't have to worry about those pesky floating point issues!
32+
33+
```js
34+
2.51 + .01; // => 2.5199999999999996
35+
currency(2.51).add(.01); // => 2.52
36+
37+
2.52 - .01; // 2.5100000000000002
38+
currency(2.52).subtract(.01); // 2.51
39+
```
40+
41+
Since *currency.js* handles values internally as integers, there is a limit to the precision that can be stored before encountering precision errors. This should be okay for most reasonable values of currencies. As long as your currencies are less than 2<sup>52</sup>sup> (in cents) or `90,071,992,547,409.91`, you should not see any problems.
42+
43+
*currency.js* also works with a variety of strings. This makes it easy to work into your UI without having do do string to number conversion or vice versa.
44+
45+
```js
46+
var c = currency("$1,234.56").add("890.12"); // 2124.68
47+
c.format(); // 2,124.68
48+
49+
// Negative values
50+
currency("-$5,000").add(1234.56); // -3765.44
51+
currency("($5,000)").add(1234.56); // -3765.44
52+
```
53+
54+
By default, currency resolves to a `string` value.
55+
56+
```js
57+
// Sets the first input to the resolved currency value
58+
document.getElementsByTagName("input")[0].value = currency(1234.56).add(6.44); // 1241.00
59+
```
60+
61+
If you need access to the raw numbers, the value is stored as both an `integer` and a `string`, which you can access with `.intValue` or `.value`;
62+
63+
```js
64+
// Get the internal values
65+
currency(123.45).add(.1).value; // => 123.46
66+
currency(123.45).add(.1).intValue; // => 12346
67+
```

docs/02-options.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
## Options
2+
3+
You can customize the formatting and parsing of `currency.js` with an optional options object. These values default to US centric currency values, but they can be overridden based on your locale.
4+
5+
### symbol *default*: `"$"`
6+
7+
When `formatWithSymbol` is set to `true`, this currency symbol will be used when calling `currency.format()`.
8+
9+
```js
10+
currency(1.23, { formatWithSymbol: true }).format(); // => "$1.23"
11+
```
12+
13+
### separator *default*: `","`
14+
15+
Separator between the number groupings when calling `currency.format()`.
16+
17+
```js
18+
currency(1234.56, { symbol: ',' }).format(); // => "1,234.56"
19+
currency(1234.56, { symbol: ' ' }).format(); // => "1 234.56"
20+
```
21+
22+
### decimal *default*: `"."`
23+
24+
```js
25+
currency(1.23, { decimal: '.' }).format(); // => "1.23"
26+
currency(1.23, { decimal: ',' }).format(); // => "1,23"
27+
```
28+
29+
### precision *default*: `2`
30+
31+
Number of decimal places to store as cents.
32+
33+
```js
34+
currency(1.234, { precision: 2 }); // => "1.23"
35+
currency(1.234, { precision: 3 }); // => "1.234"
36+
```
37+
38+
### formatWithSymbol *default*: `false`
39+
40+
Includes the `symbol` option when calling `currency.format()`.
41+
42+
```js
43+
currency(1.23, { formatWithSymbol: true }).format(); // => "$1.23"
44+
currency(1.23, { formatWithSymbol: false }).format(); // => "1.23"
45+
```
46+
47+
### errorOnInvalid *default*: `false`
48+
49+
If an invalid value such as `null` or `undefined` is passed in, `currency` will throw an error.
50+
51+
```js
52+
currency(undefined, { errorOnInvalid: true }); // throws an error
53+
```
54+
55+
### increment *default*: `null`
56+
57+
When implementing a currency that implements rounding, setting the `increment` value will allow you to set the closest increment to round the display value to.
58+
59+
```js
60+
var currencyRounding = value => currency(value, { increment: .05 });
61+
currencyRounding(1.09); // => { intValue: 109, value: 1.09 }
62+
currencyRounding(1.09).format(); // => "1.10"
63+
currencyRounding(1.06); // => { intValue: 106, value: 1.06 }
64+
currencyRounding(1.06).format(); // => "1.05"
65+
```
66+
67+
### useVedic *default*: `false`
68+
69+
When using a currency that implements the [Indian Numbering System](https://en.wikipedia.org/wiki/Indian_numbering_system), setting `useVedic` will format values with the correct groupings, i.e. `10,00,000.00`.
70+
71+
```js
72+
currency(1234567.89, { useVedic: true }).format(); // => "12,34,567.89"
73+
```

docs/03-api.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
---
2+
toc: true
3+
---
4+
5+
## Methods
6+
7+
{{{ toc }}}
8+
9+
### add
10+
11+
`currency.add( value )`
12+
13+
Adds a `string`, `number`, or `currency` value to the current currency instance.
14+
15+
```js
16+
currency(123.45).add(.01); // => "123.46"
17+
```
18+
19+
### subtract
20+
21+
`currency.subtract( value )`
22+
23+
Subtracts a `string`, `number`, or `currency` value to the current currency instance.
24+
25+
```js
26+
currency(123.45).subtract(.01); // => "123.44"
27+
```
28+
29+
### multiply
30+
31+
`currency.multiply( number )`
32+
33+
Multiplies the current currency instance by `number`.
34+
35+
```js
36+
currency(123.45).multiply(2); // => "246.90"
37+
```
38+
39+
### divide
40+
41+
`currency.divide( number )`
42+
43+
Divides the current currency instance by `number`.
44+
45+
```js
46+
currency(123.45).divide(2); // => "61.73"
47+
```
48+
49+
### distribute
50+
51+
`currency.distribute( number )`
52+
53+
Distribute takes the currency value, and tries to distribute the amount evenly. Any extra cents left over from the distribution will be stacked onto the first sets of entries.
54+
55+
```js
56+
currency(12.35).distribute(3); // => [4.12, 4.12, 4.11]
57+
currency(12.00).distribute(3); // => [4.00, 4.00, 4.00]
58+
```
59+
60+
### format
61+
62+
`currency.format([ boolean ])`
63+
64+
A simple formatter that returns a human friendly currency format.
65+
66+
```js
67+
currency(1000.00).format(); // => "1,000.00"
68+
currency("1,234,567/90").add("200,000").format(); // => "1,434,567.89"
69+
```
70+
71+
The default formatter can be overridden by passing in options as a second parameter.
72+
73+
```js
74+
var euro = value => currency(value, { separator: ' ', decimal: ',' });
75+
76+
// ...
77+
78+
euro(1000.00).format(); // => "1 000,00"
79+
euro(1234567.89).add("200 000").format(); // => "1 434 567,89"
80+
```
81+
82+
You can also include the currently set `symbol` option in a couple of different ways. By default it's always turned off, but you can turn it on for all instances of the object via options, or by passing in a boolean operator to the `format()` function.
83+
84+
```js
85+
var money = value => currency(value, { formatWithSymbol: true });
86+
87+
money(1000.00).format(); // => "$1,000.00"
88+
money(1000.00).format(false); // => "1,000.00"
89+
```
90+
91+
### dollars
92+
93+
`currency.dollars`
94+
95+
Returns the dollar value of the currency.
96+
97+
```js
98+
currency(123.45).dollars(); // => 123
99+
currency("0.99").dollars(); // => 0
100+
```
101+
102+
### cents
103+
104+
`currency.cents`
105+
106+
Returns the cent value of the currency.
107+
108+
```js
109+
currency(123.45).cents(); // => 45
110+
currency("0.99").cents(); // => 99
111+
```

docs/04-i18n.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## Internationalization (i18n) and Formatting
2+
3+
*currency.js* defaults to a US locale, but is flexible enough to work with any type of format. Since each currency instance is immutable, you can create multiple instances to work with any number of different international formats.
4+
5+
```js
6+
const USD = value => currency(value);
7+
const JPY = value => currency(value, { precision: 0, symbol: '¥' });
8+
const EURO = value => currency(value, { symbol: '', decimal: ',', separator: '.' });
9+
10+
USD(1234.567).format(true); // => "$1,234.57"
11+
JPY(1234.567).format(true); // => "¥1,235"
12+
EURO(1234.567).format(true); // => "€1.234,57"
13+
```

docs/05-addons.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Addons
2+
3+
### [babel-plugin-transform-currency-operators](https://github.com/scurker/babel-plugin-transform-currency-operators)
4+
5+
An experimental babel plugin for transforming currency operators: `currency(1.23) + 4.56` to `currency(1.23).add(4.56)`.

0 commit comments

Comments
 (0)