Skip to content

Commit

Permalink
Customize separators (#49)
Browse files Browse the repository at this point in the history
* customize thousand and decimal separator

* fix

* fix awesome js casts

* comments

* fix default

* spec for arbitrary separators
  • Loading branch information
dimailn authored and kevinongko committed Jan 9, 2018
1 parent 6fadcdc commit e0178ca
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
2 changes: 1 addition & 1 deletion dist/vue-numeric.min.js

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

34 changes: 28 additions & 6 deletions src/vue-numeric.vue
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,26 @@ export default {
required: false
},
/**
* Forced thousand separator.
* Accepts any string.
*/
thousandSeparator: {
default: undefined,
required: false,
type: String
},
/**
* Forced decimal separator.
* Accepts any string.
*/
decimalSeparator: {
default: undefined,
required: false,
type: String
},
/**
* v-model value.
*/
Expand Down Expand Up @@ -159,7 +179,8 @@ export default {
* Define decimal separator based on separator props.
* @return {String} '.' or ','
*/
decimalSeparator () {
$decimalSeparator () {
if (typeof this.decimalSeparator !== 'undefined') return this.decimalSeparator
if (this.separator === ',') return '.'
return ','
},
Expand All @@ -168,7 +189,8 @@ export default {
* Define thousand separator based on separator props.
* @return {String} '.' or ','
*/
thousandSeparator () {
$thousandSeparator () {
if (typeof this.thousandSeparator !== 'undefined') return this.thousandSeparator
if (this.separator === '.') return '.'
if (this.separator === 'space') return ' '
return ','
Expand Down Expand Up @@ -273,7 +295,7 @@ export default {
symbol: '',
format: '%v',
thousand: '',
decimal: this.decimalSeparator,
decimal: this.$decimalSeparator,
precision: Number(this.precision)
})
}
Expand Down Expand Up @@ -315,8 +337,8 @@ export default {
symbol: this.currency,
format: this.symbolPosition,
precision: Number(this.precision),
decimal: this.decimalSeparator,
thousand: this.thousandSeparator
decimal: this.$decimalSeparator,
thousand: this.$thousandSeparator
})
},
Expand All @@ -327,7 +349,7 @@ export default {
*/
unformat (value) {
const toUnformat = typeof value === 'string' && value === '' ? this.emptyValue : value
return accounting.unformat(toUnformat, this.decimalSeparator)
return accounting.unformat(toUnformat, this.$decimalSeparator)
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions test/specs/vue-numeric.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,16 @@ describe('vue-numeric.vue', () => {
wrapper.setProps({ precision: 1 })
expect(wrapper.data().amount).to.equal('2,000.2')
})

it('allow to use arbitrary separators', () => {
const wrapper = mount(VueNumeric, {
propsData: {
value: 1000.94 ,
precision: 2,
thousandSeparator: ' ',
decimalSeparator: ','
}
})
expect(wrapper.data().amount).to.equal('1 000,94')
})
})

0 comments on commit e0178ca

Please sign in to comment.