Skip to content

Commit 21a0f28

Browse files
committed
Merge branch 'dev'
2 parents 4e4e0fb + 32e7458 commit 21a0f28

File tree

6 files changed

+88
-17
lines changed

6 files changed

+88
-17
lines changed

README.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,55 @@ module.exports = {
117117

118118
+ Arguments:
119119
* `{String} [symbol] - default: '$'`
120-
* `{Number} [decimal places] - default: 2`
120+
* `{Number} [decimalDigits] - default: 2`
121+
* `{Object} [options] - default: {}`
122+
123+
+ Options:
124+
* `{String} [thousandsSeparator] - default: ','`
125+
* `{String} [decimalSeparator] - default: '.'`
126+
* `{Boolean} [symbolOnLeft] - default: true`
127+
* `{Boolean} [spaceBetweenAmountAndSymbol] - default: false`
121128

122129
+ Example:
123130

124131
```js
125132
{{ amount | currency }} // 12345 => $12,345.00
126133
```
127134
Use a different symbol:
135+
128136
```js
129137
{{ amount | currency('£') }} // 12345 => £12,345.00
130138
```
131139
Use a different number decimal places:
140+
132141
```js
133142
{{ amount | currency('', 0) }} // 12345 => ₽12,345
134143
```
144+
Use a different thousands separator:
145+
146+
```js
147+
{{ amount | currency('$', 0, { thousandsSeparator: '.' }) }} // 12345 => $12.345
148+
```
149+
Use a different decimal separator:
150+
151+
```js
152+
{{ amount | currency('$', 2, { decimalSeparator: ',' }) }} // 12345 => $12,345,00
153+
```
154+
Use symbol on right:
155+
156+
```js
157+
{{ amount | currency('$', 0, { symbolOnLeft: false }) }} // 12345 => 12,345$
158+
```
159+
Add space between amound and symbol:
160+
161+
```js
162+
{{ amount | currency('$', 0, { spaceBetweenAmountAndSymbol: true }) }} // 12345 => $ 12,345
163+
```
164+
Use multiple options:
165+
166+
```js
167+
{{ amount | currency('kr', 2, { symbolOnLeft: false, spaceBetweenAmountAndSymbol: true }) }} // 12345 => 12,345.00 kr
168+
```
135169

136170
#### pluralize
137171

dist/vue2-filters.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -605,31 +605,46 @@ Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
605605
*
606606
* 12345 => $12,345.00
607607
*
608-
* @param {String} sign
608+
* @param {String} symbol
609609
* @param {Number} decimals Decimal places
610+
* @param {Object} options
610611
*/
611612

612-
function currency (value, currency, decimals) {
613+
function currency (value, symbol, decimals, options) {
614+
var thousandsSeparator, symbolOnLeft, spaceBetweenAmountAndSymbol
613615
var digitsRE = /(\d{3})(?=\d)/g
616+
options = options || {}
614617
value = parseFloat(value)
615618
if (!isFinite(value) || (!value && value !== 0)) return ''
616-
currency = currency != null ? currency : '$'
619+
symbol = symbol != null ? symbol : '$'
617620
decimals = decimals != null ? decimals : 2
621+
thousandsSeparator = options.thousandsSeparator != null ? options.thousandsSeparator : ','
622+
symbolOnLeft = options.symbolOnLeft != null ? options.symbolOnLeft : true
623+
spaceBetweenAmountAndSymbol = options.spaceBetweenAmountAndSymbol != null ? options.spaceBetweenAmountAndSymbol : false
618624
var stringified = Math.abs(value).toFixed(decimals)
625+
stringified = options.decimalSeparator
626+
? stringified.replace('.', options.decimalSeparator)
627+
: stringified
619628
var _int = decimals
620629
? stringified.slice(0, -1 - decimals)
621630
: stringified
622631
var i = _int.length % 3
623632
var head = i > 0
624-
? (_int.slice(0, i) + (_int.length > 3 ? ',' : ''))
633+
? (_int.slice(0, i) + (_int.length > 3 ? thousandsSeparator : ''))
625634
: ''
626635
var _float = decimals
627636
? stringified.slice(-1 - decimals)
628637
: ''
638+
symbol = spaceBetweenAmountAndSymbol
639+
? (symbolOnLeft ? symbol + ' ' : ' ' + symbol)
640+
: symbol
641+
symbol = symbolOnLeft
642+
? symbol + head +
643+
_int.slice(i).replace(digitsRE, '$1' + thousandsSeparator) + _float
644+
: head +
645+
_int.slice(i).replace(digitsRE, '$1' + thousandsSeparator) + _float + symbol
629646
var sign = value < 0 ? '-' : ''
630-
return sign + currency + head +
631-
_int.slice(i).replace(digitsRE, '$1,') +
632-
_float
647+
return sign + symbol
633648
}
634649

635650
/* harmony default export */ __webpack_exports__["a"] = (currency);

dist/vue2-filters.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue2-filters",
3-
"version": "0.2.2",
3+
"version": "0.3.0",
44
"description": "The list of standard filters Vue.js 1.* adapted for use in Vue.js 2.*",
55
"main": "dist/vue2-filters.js",
66
"scripts": {

src/other/currency.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,46 @@
22
*
33
* 12345 => $12,345.00
44
*
5-
* @param {String} sign
5+
* @param {String} symbol
66
* @param {Number} decimals Decimal places
7+
* @param {Object} options
78
*/
89

9-
function currency (value, currency, decimals) {
10+
function currency (value, symbol, decimals, options) {
11+
var thousandsSeparator, symbolOnLeft, spaceBetweenAmountAndSymbol
1012
var digitsRE = /(\d{3})(?=\d)/g
13+
options = options || {}
1114
value = parseFloat(value)
1215
if (!isFinite(value) || (!value && value !== 0)) return ''
13-
currency = currency != null ? currency : '$'
16+
symbol = symbol != null ? symbol : '$'
1417
decimals = decimals != null ? decimals : 2
18+
thousandsSeparator = options.thousandsSeparator != null ? options.thousandsSeparator : ','
19+
symbolOnLeft = options.symbolOnLeft != null ? options.symbolOnLeft : true
20+
spaceBetweenAmountAndSymbol = options.spaceBetweenAmountAndSymbol != null ? options.spaceBetweenAmountAndSymbol : false
1521
var stringified = Math.abs(value).toFixed(decimals)
22+
stringified = options.decimalSeparator
23+
? stringified.replace('.', options.decimalSeparator)
24+
: stringified
1625
var _int = decimals
1726
? stringified.slice(0, -1 - decimals)
1827
: stringified
1928
var i = _int.length % 3
2029
var head = i > 0
21-
? (_int.slice(0, i) + (_int.length > 3 ? ',' : ''))
30+
? (_int.slice(0, i) + (_int.length > 3 ? thousandsSeparator : ''))
2231
: ''
2332
var _float = decimals
2433
? stringified.slice(-1 - decimals)
2534
: ''
35+
symbol = spaceBetweenAmountAndSymbol
36+
? (symbolOnLeft ? symbol + ' ' : ' ' + symbol)
37+
: symbol
38+
symbol = symbolOnLeft
39+
? symbol + head +
40+
_int.slice(i).replace(digitsRE, '$1' + thousandsSeparator) + _float
41+
: head +
42+
_int.slice(i).replace(digitsRE, '$1' + thousandsSeparator) + _float + symbol
2643
var sign = value < 0 ? '-' : ''
27-
return sign + currency + head +
28-
_int.slice(i).replace(digitsRE, '$1,') +
29-
_float
44+
return sign + symbol
3045
}
3146

3247
export default currency

test/filters.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ describe('Filters', function() {
7171
expect(filter(-50)).toBe('-$50.00')
7272
expect(filter(-150.43)).toBe('-$150.43')
7373
expect(filter(-1500.4343434)).toBe('-$1,500.43')
74+
// options
75+
expect(filter(1234, '@', 0, {thousandsSeparator: ','})).toBe('@1,234')
76+
expect(filter(1234, '', 2, {decimalSeparator: '|'})).toBe('1,234|00')
77+
expect(filter(1234, '$', 2, {symbolOnLeft: false})).toBe('1,234.00$')
78+
expect(filter(1234, '$', 0, {spaceBetweenAmountAndSymbol: true})).toBe('$ 1,234')
79+
expect(filter(1234, '$', 0, {symbolOnLeft: false,spaceBetweenAmountAndSymbol: true})).toBe('1,234 $')
80+
expect(filter(-12345, 'VND', 0, {symbolOnLeft: true})).toBe('-VND12,345')
7481
})
7582

7683
it('pluralize', function() {

0 commit comments

Comments
 (0)