Skip to content

Commit e0178ca

Browse files
dimailnkevinongko
authored andcommitted
Customize separators (#49)
* customize thousand and decimal separator * fix * fix awesome js casts * comments * fix default * spec for arbitrary separators
1 parent 6fadcdc commit e0178ca

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

dist/vue-numeric.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/vue-numeric.vue

+28-6
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,26 @@ export default {
9696
required: false
9797
},
9898
99+
/**
100+
* Forced thousand separator.
101+
* Accepts any string.
102+
*/
103+
thousandSeparator: {
104+
default: undefined,
105+
required: false,
106+
type: String
107+
},
108+
109+
/**
110+
* Forced decimal separator.
111+
* Accepts any string.
112+
*/
113+
decimalSeparator: {
114+
default: undefined,
115+
required: false,
116+
type: String
117+
},
118+
99119
/**
100120
* v-model value.
101121
*/
@@ -159,7 +179,8 @@ export default {
159179
* Define decimal separator based on separator props.
160180
* @return {String} '.' or ','
161181
*/
162-
decimalSeparator () {
182+
$decimalSeparator () {
183+
if (typeof this.decimalSeparator !== 'undefined') return this.decimalSeparator
163184
if (this.separator === ',') return '.'
164185
return ','
165186
},
@@ -168,7 +189,8 @@ export default {
168189
* Define thousand separator based on separator props.
169190
* @return {String} '.' or ','
170191
*/
171-
thousandSeparator () {
192+
$thousandSeparator () {
193+
if (typeof this.thousandSeparator !== 'undefined') return this.thousandSeparator
172194
if (this.separator === '.') return '.'
173195
if (this.separator === 'space') return ' '
174196
return ','
@@ -273,7 +295,7 @@ export default {
273295
symbol: '',
274296
format: '%v',
275297
thousand: '',
276-
decimal: this.decimalSeparator,
298+
decimal: this.$decimalSeparator,
277299
precision: Number(this.precision)
278300
})
279301
}
@@ -315,8 +337,8 @@ export default {
315337
symbol: this.currency,
316338
format: this.symbolPosition,
317339
precision: Number(this.precision),
318-
decimal: this.decimalSeparator,
319-
thousand: this.thousandSeparator
340+
decimal: this.$decimalSeparator,
341+
thousand: this.$thousandSeparator
320342
})
321343
},
322344
@@ -327,7 +349,7 @@ export default {
327349
*/
328350
unformat (value) {
329351
const toUnformat = typeof value === 'string' && value === '' ? this.emptyValue : value
330-
return accounting.unformat(toUnformat, this.decimalSeparator)
352+
return accounting.unformat(toUnformat, this.$decimalSeparator)
331353
}
332354
}
333355
}

test/specs/vue-numeric.spec.js

+12
Original file line numberDiff line numberDiff line change
@@ -251,4 +251,16 @@ describe('vue-numeric.vue', () => {
251251
wrapper.setProps({ precision: 1 })
252252
expect(wrapper.data().amount).to.equal('2,000.2')
253253
})
254+
255+
it('allow to use arbitrary separators', () => {
256+
const wrapper = mount(VueNumeric, {
257+
propsData: {
258+
value: 1000.94 ,
259+
precision: 2,
260+
thousandSeparator: ' ',
261+
decimalSeparator: ','
262+
}
263+
})
264+
expect(wrapper.data().amount).to.equal('1 000,94')
265+
})
254266
})

0 commit comments

Comments
 (0)