Skip to content

Commit 54b2e2a

Browse files
author
Dipak Sarkar
committed
updated directive and exported number format
1 parent a247638 commit 54b2e2a

File tree

8 files changed

+59
-49
lines changed

8 files changed

+59
-49
lines changed

src/component.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ export default {
2020
required: true,
2121
type: [Number, String]
2222
},
23-
null_value: {
23+
nullValue: {
2424
type: [Number, String],
25-
default: () => options.null_value
25+
default: () => options.nullValue
2626
},
2727
masked: {
2828
type: Boolean,

src/core.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ export function updateValue(el, vnode, { emit = true, force = false, clean = fal
7575
oldValue = oldValue || ''
7676
currentValue = currentValue || ''
7777

78-
const number = new NumberFormat(config).clean(clean)
78+
const number = new NumberFormat(config).clean(clean && !config.reverseFill)
7979
let masked = number.format(currentValue)
80-
let unmasked = number.clean(true).unformat(currentValue)
80+
let unmasked = number.clean(!config.reverseFill).unformat(currentValue)
8181

8282
// check value with in range max and min value
8383
if (clean) {
@@ -90,6 +90,8 @@ export function updateValue(el, vnode, { emit = true, force = false, clean = fal
9090
}
9191
}
9292

93+
console.log(unmasked, masked)
94+
9395
if (force || oldValue !== currentValue) {
9496
el[CONFIG_KEY].oldValue = masked
9597
el.unmaskedValue = unmasked

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import Number from './component.vue'
22
import VNumber from './directive'
33
import options from './options'
4+
import NumberFormat from './number-format'
45

56
export {
7+
NumberFormat,
68
Number,
79
VNumber,
810
options

src/number-format.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ export default function NumberFormat(config = options) {
6060
return this.number
6161
}
6262

63-
this.realNumber = () => this.toNumber(this.numbers().toString().replace(this.options.decimal, '.'))
63+
this.realNumber = () => {
64+
const number = this.numbers().toString().replace(this.options.decimal, '.')
65+
if (this.options.reverseFill) {
66+
return number
67+
}
68+
return this.toNumber(number)
69+
}
6470

6571
this.parts = (number = '', decimal = this.options.decimal) => {
6672
var parts = number.toString().split(decimal)
@@ -91,9 +97,9 @@ export default function NumberFormat(config = options) {
9197
* @return {String}
9298
*/
9399
this.format = (input) => {
94-
if (input === '') return this.options.null_value
95-
this.input = input || this.options.null_value
96-
if (this.isNull()) return this.options.null_value
100+
if (input === '') return this.options.nullValue
101+
this.input = input || this.options.nullValue
102+
if (this.isNull()) return this.options.nullValue
97103
return this.sign() + this.options.prefix + this.addSeparator() + this.options.suffix
98104
}
99105

@@ -103,9 +109,9 @@ export default function NumberFormat(config = options) {
103109
* @return {String}
104110
*/
105111
this.unformat = (input) => {
106-
if (input === '') return this.options.null_value
107-
this.input = input || this.options.null_value
108-
if (this.isNull()) return this.options.null_value
109-
return this.toNumber(this.sign() + this.realNumber())
112+
if (input === '') return this.options.nullValue
113+
this.input = input || this.options.nullValue
114+
if (this.isNull()) return this.options.nullValue
115+
return this.sign() + this.realNumber()
110116
}
111117
}

src/options.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ export default {
88
reverseFill: false,
99
min: false,
1010
max: false,
11-
null_value: ''
11+
nullValue: ''
1212
}

tests/unit/number-format.custom.spec.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,27 +91,27 @@ describe('unformat when options are default', () => {
9191
null_value: '',
9292
})
9393
it('unformat string value', () => {
94-
expect(numberFormat.clean(true).unformat('0')).toEqual(0)
95-
expect(numberFormat.clean(true).unformat('0,')).toEqual(0)
96-
expect(numberFormat.clean(true).unformat('-0,0')).toEqual(0)
97-
expect(numberFormat.clean(true).unformat('0,10')).toEqual(0.1)
98-
expect(numberFormat.clean(true).unformat('0,0-')).toEqual(0)
99-
expect(numberFormat.clean(true).unformat('0,10-')).toEqual(-0.1)
100-
expect(numberFormat.clean(true).unformat('12.345,54921')).toEqual(12345.55)
101-
expect(numberFormat.clean(true).unformat('--12.345,12345')).toEqual(-12345.12)
102-
expect(numberFormat.clean(true).unformat('12.345.54321,12345')).toEqual(1234554321.12)
103-
expect(numberFormat.clean(true).unformat('-12.345,,54321-')).toEqual(-12345.54)
94+
expect(numberFormat.clean(true).unformat('0')).toEqual('0')
95+
expect(numberFormat.clean(true).unformat('0,')).toEqual('0')
96+
expect(numberFormat.clean(true).unformat('-0,0')).toEqual('0')
97+
expect(numberFormat.clean(true).unformat('0,10')).toEqual('0.1')
98+
expect(numberFormat.clean(true).unformat('0,0-')).toEqual('0')
99+
expect(numberFormat.clean(true).unformat('0,10-')).toEqual('-0.1')
100+
expect(numberFormat.clean(true).unformat('12.345,54921')).toEqual('12345.55')
101+
expect(numberFormat.clean(true).unformat('--12.345,12345')).toEqual('-12345.12')
102+
expect(numberFormat.clean(true).unformat('12.345.54321,12345')).toEqual('1234554321.12')
103+
expect(numberFormat.clean(true).unformat('-12.345,,54321-')).toEqual('-12345.54')
104104
})
105105
it('unformat numerical value', () => {
106106
expect(numberFormat.clean(true).unformat(0)).toEqual('')
107107
expect(numberFormat.clean(true).unformat(0.)).toEqual('')
108108
expect(numberFormat.clean(true).unformat(0.0)).toEqual('')
109-
expect(numberFormat.clean(true).unformat(-0.10)).toEqual(-0.1)
109+
expect(numberFormat.clean(true).unformat(-0.10)).toEqual('-0.1')
110110
expect(numberFormat.clean(true).unformat(-0.0)).toEqual('')
111-
expect(numberFormat.clean(true).unformat(0.10)).toEqual(0.1)
112-
expect(numberFormat.clean(true).unformat(12345.54921)).toEqual(12345.55)
113-
expect(numberFormat.clean(true).unformat(12345.12345)).toEqual(12345.12)
114-
expect(numberFormat.clean(true).unformat(12345.54321)).toEqual(12345.54)
115-
expect(numberFormat.clean(true).unformat(12345.54321)).toEqual(12345.54)
111+
expect(numberFormat.clean(true).unformat(0.10)).toEqual('0.1')
112+
expect(numberFormat.clean(true).unformat(12345.54921)).toEqual('12345.55')
113+
expect(numberFormat.clean(true).unformat(12345.12345)).toEqual('12345.12')
114+
expect(numberFormat.clean(true).unformat(12345.54321)).toEqual('12345.54')
115+
expect(numberFormat.clean(true).unformat(12345.54321)).toEqual('12345.54')
116116
})
117117
})

tests/unit/number-format.default.spec.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,27 +69,27 @@ describe('format when options are default', () => {
6969
describe('unformat when options are default', () => {
7070
const numberFormat = new NumberFormat({})
7171
it('unformat string value', () => {
72-
expect(numberFormat.clean(true).unformat('0')).toEqual(0)
73-
expect(numberFormat.clean(true).unformat('0.')).toEqual(0)
74-
expect(numberFormat.clean(true).unformat('-0.0')).toEqual(0)
75-
expect(numberFormat.clean(true).unformat('0.10')).toEqual(0.1)
76-
expect(numberFormat.clean(true).unformat('0.0-')).toEqual(0)
77-
expect(numberFormat.clean(true).unformat('0.10-')).toEqual(-0.1)
78-
expect(numberFormat.clean(true).unformat('12,345.54921')).toEqual(12345.55)
79-
expect(numberFormat.clean(true).unformat('--12,345.12345')).toEqual(-12345.12)
80-
expect(numberFormat.clean(true).unformat('12,345.54321.12345')).toEqual(12345.54)
81-
expect(numberFormat.clean(true).unformat('-12,345..54321-')).toEqual(-12345.54)
72+
expect(numberFormat.clean(true).unformat('0')).toEqual('0')
73+
expect(numberFormat.clean(true).unformat('0.')).toEqual('0')
74+
expect(numberFormat.clean(true).unformat('-0.0')).toEqual('0')
75+
expect(numberFormat.clean(true).unformat('0.10')).toEqual('0.1')
76+
expect(numberFormat.clean(true).unformat('0.0-')).toEqual('0')
77+
expect(numberFormat.clean(true).unformat('0.10-')).toEqual('-0.1')
78+
expect(numberFormat.clean(true).unformat('12,345.54921')).toEqual('12345.55')
79+
expect(numberFormat.clean(true).unformat('--12,345.12345')).toEqual('-12345.12')
80+
expect(numberFormat.clean(true).unformat('12,345.54321.12345')).toEqual('12345.54')
81+
expect(numberFormat.clean(true).unformat('-12,345..54321-')).toEqual('-12345.54')
8282
})
8383
it('unformat numerical value', () => {
8484
expect(numberFormat.clean(true).unformat(0)).toEqual('')
8585
expect(numberFormat.clean(true).unformat(0.)).toEqual('')
8686
expect(numberFormat.clean(true).unformat(0.0)).toEqual('')
87-
expect(numberFormat.clean(true).unformat(-0.10)).toEqual(-0.1)
87+
expect(numberFormat.clean(true).unformat(-0.10)).toEqual('-0.1')
8888
expect(numberFormat.clean(true).unformat(-0.0)).toEqual('')
89-
expect(numberFormat.clean(true).unformat(0.10)).toEqual(0.1)
90-
expect(numberFormat.clean(true).unformat(12345.54921)).toEqual(12345.55)
91-
expect(numberFormat.clean(true).unformat(12345.12345)).toEqual(12345.12)
92-
expect(numberFormat.clean(true).unformat(12345.54321)).toEqual(12345.54)
93-
expect(numberFormat.clean(true).unformat(12345.54321)).toEqual(12345.54)
89+
expect(numberFormat.clean(true).unformat(0.10)).toEqual('0.1')
90+
expect(numberFormat.clean(true).unformat(12345.54921)).toEqual('12345.55')
91+
expect(numberFormat.clean(true).unformat(12345.12345)).toEqual('12345.12')
92+
expect(numberFormat.clean(true).unformat(12345.54321)).toEqual('12345.54')
93+
expect(numberFormat.clean(true).unformat(12345.54321)).toEqual('12345.54')
9494
})
9595
})

tests/unit/number-format.reverse-fill.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ describe('when enabled reverse fill', () => {
1111
expect(numberFormat.format('-1234.6512')).toEqual('-123,465.12')
1212
})
1313
it('should return as follows', () => {
14-
expect(numberFormat.unformat('sdfgasd55468.546')).toEqual(554685.46)
15-
expect(numberFormat.unformat('sdfgasd55468.546')).toEqual(554685.46)
16-
expect(numberFormat.unformat('sdfgasd55468.546-')).toEqual(-554685.46)
17-
expect(numberFormat.unformat('-1234.6512')).toEqual(-123465.12)
14+
expect(numberFormat.unformat('sdfgasd55468.546')).toEqual('554685.46')
15+
expect(numberFormat.unformat('sdfgasd55468.546')).toEqual('554685.46')
16+
expect(numberFormat.unformat('sdfgasd55468.546-')).toEqual('-554685.46')
17+
expect(numberFormat.unformat('-1234.6512')).toEqual('-123465.12')
1818
})
1919
})
2020

0 commit comments

Comments
 (0)