Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge next into master #249

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 16 additions & 18 deletions docs/02-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ You can customize the formatting and parsing of `currency.js` with an optional o

### symbol *default*: `"$"`

When `formatWithSymbol` is set to `true`, this currency symbol will be used when calling `currency.format()`.

```js
currency(1.23, { formatWithSymbol: true }).format(); // => "$1.23"
currency(1.23).format(); // => "$1.23"
```

### separator *default*: `","`
Expand All @@ -22,8 +20,8 @@ currency(1234.56, { separator: ' ' }).format(); // => "1 234.56"
### decimal *default*: `"."`

```js
currency(1.23, { decimal: '.' }).format(); // => "1.23"
currency(1.23, { decimal: ',' }).format(); // => "1,23"
currency(1.23, { decimal: '.' }).format(); // => "$1.23"
currency(1.23, { decimal: ',' }).format(); // => "$1,23"
```

### precision *default*: `2`
Expand All @@ -35,23 +33,13 @@ currency(1.234, { precision: 2 }); // => "1.23"
currency(1.234, { precision: 3 }); // => "1.234"
```

### formatWithSymbol *default*: `false`

Includes the `symbol` option when calling `currency.format()`.

```js
currency(1.23, { formatWithSymbol: true }).format(); // => "$1.23"
currency(1.23, { formatWithSymbol: false }).format(); // => "1.23"
```

### pattern *default*: `!#`

Allows you to customize the format pattern using `!` as replacement for the currency symbol and `#` as replacement for the currency amount.

```js
currency(1.23, {
pattern: `# !`,
formatWithSymbol: true
pattern: `# !`
}).format(); // => "1.23 $"
```

Expand All @@ -61,11 +49,21 @@ Allows you to customize the negative format pattern using `!` as replacement for

```js
currency(-1.23, {
negativePattern: `(!#)`,
formatWithSymbol: true
negativePattern: `(!#)`
}).format(); // => "($1.23)"
```

### format *default*: `null`

Allows you to customize the format of the currency when calling `currency.format()`. `format` passes in the `currency` object as well as the `options` object to the function and expects a string to be returned. Use this when the provided formatting options do not meet your needs.

```js
function format(currency, options) {
return `${currency.dollars}.${currency.cents}`;
}
currency(1234.56, { format }).format(); // => "1234.56"
```

### errorOnInvalid *default*: `false`

If an invalid value such as `null` or `undefined` is passed in, `currency` will throw an error.
Expand Down
17 changes: 4 additions & 13 deletions docs/03-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,35 +83,26 @@ currency(12.00).distribute(3); // => [4.00, 4.00, 4.00]

### format

`currency.format([ boolean ])`
`currency.format([ function | options ])`

A simple formatter that returns a human friendly currency format.

```js
currency(1000.00).format(); // => "1,000.00"
currency("1,234,567/90").add("200,000").format(); // => "1,434,567.89"
currency(1000.00).format(); // => "$1,000.00"
currency("1,234,567/90").add("200,000").format(); // => "$1,434,567.89"
```

The default formatter can be overridden by passing in options as a second parameter.

```js
var euro = value => currency(value, { separator: ' ', decimal: ',' });
var euro = value => currency(value, { separator: ' ', decimal: ',', format: ... });

// ...

euro(1000.00).format(); // => "1 000,00"
euro(1234567.89).add("200 000").format(); // => "1 434 567,89"
```

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.

```js
var money = value => currency(value, { formatWithSymbol: true });

money(1000.00).format(); // => "$1,000.00"
money(1000.00).format(false); // => "1,000.00"
```

### dollars

`currency.dollars`
Expand Down
122 changes: 122 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
"minify:docs": "html-minifier --input-dir ./docs/build --output-dir ./docs/build --file-ext html --collapse-whitespace --decode-entities --minify-css --minify-js",
"prepare": "npm run build",
"pretest:js": "npm run transpile:es",
"pretest:typescript": "npm run copy-typescript-definition",
"pretest:flow": "npm run copy-flow-definition",
"transpile:es": "rollup -c ./config/rollup.config.js",
"transpile:umd": "rollup -c ./config/rollup.umd.js",
"transpile": "npm-run-all transpile:*",
Expand Down Expand Up @@ -86,6 +88,7 @@
"pretty-bytes": "^5.3.0",
"rollup": "^2.0.0",
"rollup-plugin-babel": "^4.3.3",
"sinon": "^7.3.2",
"typescript": "^3.5.3"
},
"nyc": {
Expand Down