Skip to content

Commit 6713174

Browse files
FlavsditzFlavio Diez
andauthored
Add check for a ZERO value (#104)
The JS language considers the value ZERO to be "false". This has to do with the fact that the numeric representation of true/false are 1/0. This was of course preventing the initial value of a numeric field to be set to zero since there was a check on the mounted function that saw if the value was defined. I've added a simple OR case, where if the value was evaluated to FALSE than we check again for if the value is zero. This fixed the case where the value ZERO was not being shown when it was deliberately chosen. It did break another case when we pass an empty string. To fix it I also test if the original value is not empty (which would be evaluated to zero) Co-authored-by: Flavio Diez <[email protected]>
1 parent d1ac034 commit 6713174

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-numeric",
3-
"version": "2.4.1",
3+
"version": "2.4.2",
44
"description": "Input field component to display currency value based on Vue.",
55
"author": "Kevin Ongko",
66
"main": "dist/vue-numeric.min.js",

src/vue-numeric.vue

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ export default {
275275
276276
mounted () {
277277
// Set default value props when valueNumber has some value
278-
if (this.valueNumber) {
278+
if (this.valueNumber || this.isDeliberatelyZero()) {
279279
this.process(this.valueNumber)
280280
this.amount = this.format(this.valueNumber)
281281
@@ -377,6 +377,14 @@ export default {
377377
unformat (value) {
378378
const toUnformat = typeof value === 'string' && value === '' ? this.emptyValue : value
379379
return accounting.unformat(toUnformat, this.decimalSeparatorSymbol)
380+
},
381+
382+
/**
383+
* Check if value was deliberately set to zero and not just evaluated
384+
* @return {boolean}
385+
*/
386+
isDeliberatelyZero () {
387+
return this.valueNumber === 0 && this.value !== '';
380388
}
381389
}
382390
}

test/specs/vue-numeric.spec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,4 +313,8 @@ describe('vue-numeric.vue', () => {
313313
wrapper.trigger('change')
314314
expect(process.called).to.equal(true)
315315
})
316+
it('initial value is 0 if zero is passed', () => {
317+
const wrapper = mount(VueNumeric, { propsData: { value: 0}})
318+
expect(wrapper.data().amount).to.equal('0')
319+
})
316320
})

0 commit comments

Comments
 (0)