Skip to content

Customize separators #49

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

Merged
merged 7 commits into from
Jan 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
})
})