Skip to content

Commit d2a84e9

Browse files
author
Dipak Sarkar
committed
updated NumberFromat function
1 parent 657fb56 commit d2a84e9

File tree

5 files changed

+38
-35
lines changed

5 files changed

+38
-35
lines changed

docs/.vuepress/enhanceApp.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* https://v1.vuepress.vuejs.org/guide/basic-config.html#app-level-enhancements
55
*/
66

7-
import number from '../../src'
7+
import number from '../../'
88
import Quasar from 'quasar'
99
import 'quasar/dist/quasar.min.css'
1010

src/component.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export default {
6565
value: {
6666
immediate: true,
6767
handler(newValue) {
68-
const formatted = new NumberFormat(newValue, this.$props).format()
68+
const formatted = new NumberFormat(this.$props).format(newValue)
6969
if (formatted !== this.formattedValue) {
7070
this.formattedValue = formatted
7171
}
@@ -75,8 +75,8 @@ export default {
7575
7676
methods: {
7777
change(evt) {
78-
const number = new NumberFormat(evt.target.value, this.$props)
79-
this.$emit('input', this.masked ? number.format(true) : number.unformat(true))
78+
const number = new NumberFormat(this.$props).clean()
79+
this.$emit('input', this.masked ? number.format(evt.target.value) : number.unformat(evt.target.value))
8080
},
8181
},
8282
}

src/directive.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default function (el, binding) {
2323
el.oninput = () => {
2424
// console.log('oninput()')
2525
var positionFromEnd = el.value.length - el.selectionEnd
26-
el.value = new NumberFormat(el.value, config).format()
26+
el.value = new NumberFormat(config).format(el.value)
2727
positionFromEnd = Math.max(positionFromEnd, config.suffix.length)
2828
positionFromEnd = el.value.length - positionFromEnd
2929
positionFromEnd = Math.max(positionFromEnd, config.prefix.length + 1)
@@ -32,7 +32,7 @@ export default function (el, binding) {
3232

3333
el.onblur = () => {
3434
// clean up after end the input
35-
el.value = new NumberFormat(el.value, config).format(true)
35+
el.value = new NumberFormat(config).clean().format(el.value)
3636
el.dispatchEvent(new Event('input'))
3737
el.dispatchEvent(new Event('change'))
3838
}

src/index.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ export {
1010

1111
function install(Vue, globalOptions) {
1212
if (globalOptions) {
13-
Object.keys(globalOptions).map((key) => {
14-
options[key] = globalOptions[key]
15-
return options
16-
})
13+
Object.assign(options, globalOptions)
1714
}
1815
Vue.directive('number', VNumber)
1916
Vue.component('number', Number)

src/utils.js

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,58 @@
11
import options from './options'
22

3-
function NumberFormat(input, opt = options) {
4-
this.options = opt
5-
this.input = input || this.options.null_value
6-
3+
function NumberFormat(opt = options) {
4+
this.options = Object.assign(options, opt)
5+
this.input = this.options.null_value
6+
this.isClean = false
7+
this.clean = () => {
8+
this.isClean = true
9+
return this
10+
}
11+
this.negative = () => {
12+
const negetive = (this.input.toString().indexOf('-') >= 0 && this.numberOnly() > 0) ? '-' : ''
13+
return negetive
14+
}
715
this.numbers = () => {
8-
let number = this.input
916
if (typeof this.input === 'number') {
10-
number = this.input.toFixed(this.options.precision)
17+
this.numbers = this.input.toFixed(this.options.precision)
1118
} else {
12-
number = this.numberOnly()
19+
this.numbers = this.numberOnly(this.input)
1320
}
14-
return number
21+
return this.numbers
1522
}
16-
17-
this.numberOnly = (clean = false) => {
23+
this.numberOnly = () => {
1824
const regExp = new RegExp(`[^0-9\\${this.options.decimal}]+`, 'gi')
19-
const numbers = this.input.toString().replace(regExp, '')
20-
if (clean) {
21-
const parts = numbers.split(this.options.decimal)
25+
this.numbers = this.input.toString().replace(regExp, '')
26+
if (this.isClean) {
27+
const parts = this.numbers.split(this.options.decimal)
2228
return parts.length > 1 && parts[1] ? parts.join(this.options.decimal) : parts[0]
2329
}
24-
return numbers
30+
return this.numbers
2531
}
26-
27-
this.negative = (this.input.toString().indexOf('-') >= 0 && this.numbers() > 0) ? '-' : ''
28-
2932
this.parts = () => {
3033
const parts = this.numbers().toString().split(this.options.decimal)
31-
parts[0] = this.negative + (Number(parts[0]) ? Number(parts[0]) : 0)
34+
parts[0] = this.negative() + (Number(parts[0]) ? Number(parts[0]) : 0)
3235
if (parts.length > 1) {
3336
parts[1] = parts[1].slice(0, this.options.precision)
3437
}
3538
return parts
3639
}
37-
38-
this.addSeparator = (clean) => {
40+
this.addSeparator = () => {
3941
const parts = this.parts()
4042
parts[0] = parts[0].toString().replace(/(\d)(?=(?:\d{3})+\b)/gm, `$1${this.options.separator}`)
41-
if (clean) {
43+
if (this.isClean) {
4244
return parts[1] && parts[1].length > 0 ? parts.join(this.options.decimal) : parts[0]
4345
}
4446
return parts.join(this.options.decimal)
4547
}
46-
47-
this.format = (clean = false) => this.options.prefix + this.addSeparator(clean) + this.options.suffix
48-
49-
this.unformat = (clean = true) => this.negative + this.numberOnly(clean)
48+
this.format = (input) => {
49+
this.input = input
50+
return this.options.prefix + this.addSeparator() + this.options.suffix
51+
}
52+
this.unformat = (input) => {
53+
this.input = input
54+
return this.negative() + this.numberOnly()
55+
}
5056
}
5157

5258
function setCursor(el, position) {

0 commit comments

Comments
 (0)