Skip to content

Commit cd98cc5

Browse files
author
Dipak Sarkar
committed
updated some feature
1 parent c08f734 commit cd98cc5

File tree

9 files changed

+61
-55
lines changed

9 files changed

+61
-55
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ yarn add @coders-tm/vue-number-format
1212
```
1313
## Vue 2.0
1414
```bash
15-
npm install @coders-tm/vue-number-format@2.0.4
15+
npm install @coders-tm/vue-number-format@2.0.7
1616
or
17-
yarn add @coders-tm/vue-number-format@2.0.4
17+
yarn add @coders-tm/vue-number-format@2.0.7
1818
```
1919
## Features
2020

src/component.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ export default {
1919
modelValue: {
2020
required: true
2121
},
22-
null_value: {
22+
nullValue: {
2323
type: [Number, String],
24-
default: () => options.null_value
24+
default: () => options.nullValue
2525
},
2626
masked: {
2727
type: Boolean,

src/core.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ export function updateValue(el, vnode, { emit = true, force = false, clean = fal
5959
oldValue = oldValue || ''
6060
currentValue = currentValue || ''
6161

62-
const number = new NumberFormat(config).clean(clean)
62+
const number = new NumberFormat(config).clean(clean && !config.reverseFill)
6363
let masked = number.format(currentValue)
64-
let unmasked = number.clean(true).unformat(currentValue)
64+
let unmasked = number.clean(!config.reverseFill).unformat(currentValue)
6565

6666
// check value with in range max and min value
6767
if (clean) {

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default {
1515
if (config) {
1616
Object.assign(Options, config)
1717
}
18-
app.directive('number', directive)
19-
app.component('number', component)
18+
app.directive('number', VNumber)
19+
app.component('number', Number)
2020
}
2121
}

src/number-format.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,21 @@ 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)
6773
parts[0] = this.toNumber(parts[0]) || 0
6874
if (parts.length > 1) {
6975
parts[1] = parts.slice(1, parts.length).join('')
7076
parts = parts.slice(0, 2)
71-
if (this.isClean && parts[1].length > this.options.precision) {
77+
if (this.isClean && parts[1].length) {
7278
parts[1] = this.toNumber(`.${parts[1]}`).toFixed(this.options.precision).toString().replace('0.', '')
7379
}
7480
}
@@ -80,7 +86,7 @@ export default function NumberFormat(config = options) {
8086
parts[0] = parts[0].toString().replace(/(\d)(?=(?:\d{3})+\b)/gm, `$1${this.options.separator}`)
8187
if (this.isClean) {
8288
parts[1] = this.toNumber(`.${parts[1]}`).toString().replace('0.', '')
83-
return parts[1] && parts[1] >= 0 ? parts.join(this.options.decimal) : parts[0]
89+
return parts[1] && parts[1] > 0 ? parts.join(this.options.decimal) : parts[0]
8490
}
8591
return parts.join(this.options.decimal)
8692
}
@@ -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)