Skip to content

Commit

Permalink
Added showPlusSign option to currency filter
Browse files Browse the repository at this point in the history
Resolve #76
  • Loading branch information
freearhey committed Nov 4, 2019
1 parent e5ed190 commit 452975b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ export default {
* `{String} [decimalSeparator] - default: '.'`
* `{Boolean} [symbolOnLeft] - default: true`
* `{Boolean} [spaceBetweenAmountAndSymbol] - default: false`
* `{Boolean} [showPlusSign] - default: false`

+ Example:

Expand Down Expand Up @@ -284,6 +285,12 @@ export default {
```js
{{ amount | currency('$', 0, { spaceBetweenAmountAndSymbol: true }) }} // 12345 => $ 12,345
```

Show the plus sign if the value is greater than zero:

```js
{{ amount | currency('$', 0, { showPlusSign: true }) }} // 12345 => +$12,345
```
Use multiple options:

```js
Expand Down Expand Up @@ -468,7 +475,8 @@ var Vue2FiltersConfig = {
thousandsSeparator: ',',
decimalSeparator: '.',
symbolOnLeft: true,
spaceBetweenAmountAndSymbol: false
spaceBetweenAmountAndSymbol: false,
showPlusSign: false
},
pluralize: {
includeNumber: false
Expand Down
6 changes: 4 additions & 2 deletions src/other/currency.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function currency (value, symbol, decimals, options) {
symbol = util.exist(symbol) ? symbol : globalOptions.symbol
decimals = util.exist(decimals) ? decimals : globalOptions.decimalDigits
options = options || globalOptions
var thousandsSeparator, symbolOnLeft, spaceBetweenAmountAndSymbol
var thousandsSeparator, symbolOnLeft, spaceBetweenAmountAndSymbol, showPlusSign
var digitsRE = /(\d{3})(?=\d)/g
value = parseFloat(value)
if (!isFinite(value) || (!value && value !== 0)) return ''
Expand All @@ -23,6 +23,7 @@ function currency (value, symbol, decimals, options) {
thousandsSeparator = options.thousandsSeparator != null ? options.thousandsSeparator : ','
symbolOnLeft = options.symbolOnLeft != null ? options.symbolOnLeft : true
spaceBetweenAmountAndSymbol = options.spaceBetweenAmountAndSymbol != null ? options.spaceBetweenAmountAndSymbol : false
showPlusSign = options.showPlusSign != null ? options.showPlusSign : false
var number = Math.abs(value)
var stringified = toFixed(number, decimals)
stringified = options.decimalSeparator
Expand All @@ -47,7 +48,8 @@ function currency (value, symbol, decimals, options) {
: head +
_int.slice(i).replace(digitsRE, '$1' + thousandsSeparator) + _float + symbol
var sign = value < 0 ? '-' : ''
return sign + symbol
var plusSign = (value > 0 && showPlusSign) ? '+' : ''
return plusSign + sign + symbol
}

function toFixed(num, precision) {
Expand Down
8 changes: 6 additions & 2 deletions test/filters.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ describe('Filters', function() {
expect(filter(1234, '$', 0, {spaceBetweenAmountAndSymbol: true})).toBe('$ 1,234')
expect(filter(1234, '$', 0, {symbolOnLeft: false,spaceBetweenAmountAndSymbol: true})).toBe('1,234 $')
expect(filter(-12345, 'VND', 0, {symbolOnLeft: true})).toBe('-VND12,345')
expect(filter(12345, 'VND', 0, {showPlusSign: true})).toBe('+VND12,345')
expect(filter(-12345, 'VND', 0, {showPlusSign: true})).toBe('-VND12,345')
expect(filter(0, 'VND', 0, {showPlusSign: true})).toBe('VND0')
// round up
expect(filter(4514.275)).toBe('$4,514.28')
expect(filter(9446.975)).toBe('$9,446.98')
Expand All @@ -158,11 +161,12 @@ describe('Filters', function() {
thousandsSeparator: ',',
decimalSeparator: '|',
symbolOnLeft: false,
spaceBetweenAmountAndSymbol: true
spaceBetweenAmountAndSymbol: true,
showPlusSign: true
}
})

expect(filter(1234)).toBe('1,234|000 @')
expect(filter(1234)).toBe('+1,234|000 @')
})

it('pluralize', function() {
Expand Down

0 comments on commit 452975b

Please sign in to comment.