Skip to content

Commit bb482d3

Browse files
committed
removed input value replace by config null value
1 parent b3e4f46 commit bb482d3

8 files changed

+239
-135
lines changed

src/core.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,8 @@ export function updateCursor(el, position) {
7474
export function updateValue(el, vnode, { emit = true, force = false, clean = false } = {}) {
7575
const { config } = el[CONFIG_KEY]
7676
let { oldValue } = el[CONFIG_KEY]
77-
7877
let currentValue = vnode && vnode.data.model ? vnode.data.model.value : el.value
7978

80-
oldValue = oldValue || ''
81-
currentValue = currentValue || ''
82-
8379
if (force || oldValue !== currentValue) {
8480
const number = new NumberFormat(config).clean(clean && !config.reverseFill)
8581
let masked = number.format(currentValue)

src/number-format.js

Lines changed: 58 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,19 @@ export default function NumberFormat(config = options) {
1010
this.number = ''
1111
this.isClean = false
1212

13-
this.isNull = (input = this.input) => !this.numberOnly(input, new RegExp('[^0-9]+', 'gi'))
13+
this.isNull = (input = this.input) =>
14+
!this.numberOnly(input, new RegExp('[^0-9]+', 'gi'))
1415

1516
this.clean = (clean = false) => {
1617
this.isClean = clean
1718
return this
1819
}
1920

2021
this.sign = () => {
21-
const sign = (this.input.toString().indexOf('-') >= 0 && this.realNumber() > 0) ? '-' : ''
22+
const sign =
23+
this.input.toString().indexOf('-') >= 0 && this.realNumber() > 0
24+
? '-'
25+
: ''
2226
return sign
2327
}
2428

@@ -34,7 +38,7 @@ export default function NumberFormat(config = options) {
3438
function toFixed(numbers, precision) {
3539
// eslint-disable-next-line no-restricted-properties
3640
var exp = Math.pow(10, precision)
37-
var float = parseFloat(numbers) / exp
41+
var float = parseFloat(numbers) / exp || 0
3842
return float.toFixed(fixed(precision))
3943
}
4044

@@ -46,48 +50,66 @@ export default function NumberFormat(config = options) {
4650

4751
this.numbers = () => {
4852
if (this.options.reverseFill) {
49-
this.number = toFixed(this.numberOnly(this.input, /\D+/g), this.options.precision).replace('.', this.options.decimal)
53+
this.number = toFixed(
54+
this.numberOnly(this.input, /\D+/g),
55+
this.options.precision
56+
).replace('.', this.options.decimal)
5057
} else if (typeof this.input === 'number') {
51-
if (this.isClean) {
52-
this.number = this.toNumber(this.input.toFixed(this.options.precision)).toString().replace('-', '').replace('.', this.options.decimal)
53-
} else {
54-
this.number = this.toNumber(this.input).toString().replace('-', '').replace('.', this.options.decimal)
55-
}
58+
this.number = this.parts(
59+
this.input.toString().replace('-', ''),
60+
'.'
61+
).join(this.options.decimal)
5662
} else {
57-
this.number = this.numberOnly(this.input, new RegExp(`[^0-9\\${this.options.decimal}]+`, 'gi'))
63+
this.number = this.numberOnly(
64+
this.input,
65+
new RegExp(`[^0-9\\${this.options.decimal}]+`, 'gi')
66+
)
5867
this.number = this.parts(this.number).join(this.options.decimal)
5968
}
6069
return this.number
6170
}
6271

6372
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)
73+
return this.numbers().toString().replace(this.options.decimal, '.')
6974
}
7075

7176
this.parts = (number = '', decimal = this.options.decimal) => {
7277
var parts = number.toString().split(decimal)
7378
parts[0] = this.toNumber(parts[0]) || 0
79+
7480
if (parts.length > 1) {
7581
parts[1] = parts.slice(1, parts.length).join('')
7682
parts = parts.slice(0, 2)
77-
if (this.isClean && parts[1].length > this.options.precision) {
78-
parts[1] = this.toNumber(`.${parts[1]}`).toFixed(this.options.precision).toString().replace('0.', '')
83+
}
84+
85+
if (this.isClean) {
86+
const newNumber = this.toNumber(parts.join('.')).toFixed(
87+
this.options.precision
88+
)
89+
const cleanNumber = this.toNumber(newNumber)
90+
const minimumDigits = cleanNumber.toFixed(
91+
this.options.minimumFractionDigits
92+
)
93+
94+
if (
95+
this.options.minimumFractionDigits &&
96+
this.options.minimumFractionDigits >= 0 &&
97+
cleanNumber.toString().length < minimumDigits.length
98+
) {
99+
parts = minimumDigits.toString().split('.')
100+
} else {
101+
parts = cleanNumber.toString().split('.')
79102
}
80103
}
104+
81105
return parts.slice(0, 2)
82106
}
83107

84108
this.addSeparator = () => {
85109
var parts = this.numbers().split(this.options.decimal)
86-
parts[0] = parts[0].toString().replace(/(\d)(?=(?:\d{3})+\b)/gm, `$1${this.options.separator}`)
87-
if (this.isClean) {
88-
parts[1] = this.toNumber(`.${parts[1]}`).toString().replace('0.', '')
89-
return parts[1] && parts[1] > 0 ? parts.join(this.options.decimal) : parts[0]
90-
}
110+
parts[0] = parts[0]
111+
.toString()
112+
.replace(/(\d)(?=(?:\d{3})+\b)/gm, `$1${this.options.separator}`)
91113
return parts.join(this.options.decimal)
92114
}
93115

@@ -96,22 +118,27 @@ export default function NumberFormat(config = options) {
96118
* @param {Number, String} input
97119
* @return {String}
98120
*/
99-
this.format = (input) => {
100-
if (input === '') return this.options.nullValue
101-
this.input = input || this.options.nullValue
102-
if (this.isNull()) return this.options.nullValue
103-
return this.sign() + this.options.prefix + this.addSeparator() + this.options.suffix
121+
this.format = (input = '') => {
122+
this.input = input
123+
if (this.isNull() && !this.options.reverseFill)
124+
return this.options.nullValue
125+
return (
126+
this.sign() +
127+
this.options.prefix +
128+
this.addSeparator() +
129+
this.options.suffix
130+
)
104131
}
105132

106133
/**
107134
* Unformat the input with default config if there is no constructor config
108135
* @param {Number, String} input
109136
* @return {String}
110137
*/
111-
this.unformat = (input) => {
112-
if (input === '') return this.options.nullValue
113-
this.input = input || this.options.nullValue
114-
if (this.isNull()) return this.options.nullValue
138+
this.unformat = (input = '') => {
139+
this.input = input
140+
if (this.isNull() && !this.options.reverseFill)
141+
return this.options.nullValue
115142
return this.sign() + this.realNumber()
116143
}
117144
}
Lines changed: 28 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
import NumberFormat from '../../src/number-format'
22

33
describe('should not throw error on empty config', () => {
4-
expect(() => new NumberFormat({
5-
prefix: '$',
6-
separator: '.',
7-
decimal: ',',
8-
nullValue: 0,
9-
})).not.toThrow()
4+
expect(
5+
() =>
6+
new NumberFormat({
7+
prefix: '$',
8+
separator: '.',
9+
decimal: ',',
10+
null_value: 0,
11+
})
12+
).not.toThrow()
1013
})
1114
describe('when the value is invalid with custom config', () => {
1215
const numberFormat = new NumberFormat({
1316
prefix: '$',
1417
separator: '.',
1518
decimal: ',',
16-
nullValue: 0,
19+
null_value: 0,
1720
})
1821
it('should return as follows', () => {
1922
expect(numberFormat.format('')).toEqual('')
@@ -44,9 +47,11 @@ describe('format when options are custom', () => {
4447
prefix: '$',
4548
separator: '.',
4649
decimal: ',',
47-
nullValue: '',
50+
null_value: '',
4851
})
4952
it('format string value', () => {
53+
expect(numberFormat.format()).toEqual('')
54+
expect(numberFormat.format('')).toEqual('')
5055
expect(numberFormat.format('0')).toEqual('$0')
5156
expect(numberFormat.format('0,')).toEqual('$0,')
5257
expect(numberFormat.format('-0,0')).toEqual('$0,0')
@@ -55,63 +60,33 @@ describe('format when options are custom', () => {
5560
expect(numberFormat.format('0,10-')).toEqual('-$0,10')
5661
expect(numberFormat.format('12.345,54921')).toEqual('$12.345,54921')
5762
expect(numberFormat.format('--12.345,12345')).toEqual('-$12.345,12345')
58-
expect(numberFormat.format('12.345.54321,12945')).toEqual('$1.234.554.321,12945')
63+
expect(numberFormat.format('12.345.54321,12945')).toEqual(
64+
'$1.234.554.321,12945'
65+
)
5966
expect(numberFormat.format('-12.345,,54321-')).toEqual('-$12.345,54321')
6067
})
6168
it('format numerical value', () => {
62-
expect(numberFormat.format(0)).toEqual('')
63-
expect(numberFormat.format(0.)).toEqual('')
64-
expect(numberFormat.format(0.0)).toEqual('')
65-
expect(numberFormat.format(-0.10)).toEqual('-$0,1')
66-
expect(numberFormat.format(-0.0)).toEqual('')
67-
expect(numberFormat.format(0.10)).toEqual('$0,1')
69+
expect(numberFormat.format(0)).toEqual('$0')
70+
expect(numberFormat.format(0)).toEqual('$0')
71+
expect(numberFormat.format(0.0)).toEqual('$0')
72+
expect(numberFormat.format(-0.1)).toEqual('-$0,1')
73+
expect(numberFormat.format(-0.0)).toEqual('$0')
74+
expect(numberFormat.format(0.1)).toEqual('$0,1')
6875
expect(numberFormat.format(12345.54921)).toEqual('$12.345,54921')
6976
expect(numberFormat.format(12345.12345)).toEqual('$12.345,12345')
7077
expect(numberFormat.format(12345.54321)).toEqual('$12.345,54321')
7178
expect(numberFormat.format(12345.54321)).toEqual('$12.345,54321')
7279
})
7380
it('format and clean numerical value', () => {
74-
expect(numberFormat.clean(true).format(0)).toEqual('')
75-
expect(numberFormat.clean(true).format(0.)).toEqual('')
76-
expect(numberFormat.clean(true).format(0.0)).toEqual('')
77-
expect(numberFormat.clean(true).format(0.10)).toEqual('$0,1')
78-
expect(numberFormat.clean(true).format(-0.0)).toEqual('')
79-
expect(numberFormat.clean(true).format(-0.10)).toEqual('-$0,1')
81+
expect(numberFormat.clean(true).format(0)).toEqual('$0')
82+
expect(numberFormat.clean(true).format(0)).toEqual('$0')
83+
expect(numberFormat.clean(true).format(0.0)).toEqual('$0')
84+
expect(numberFormat.clean(true).format(0.1)).toEqual('$0,1')
85+
expect(numberFormat.clean(true).format(-0.0)).toEqual('$0')
86+
expect(numberFormat.clean(true).format(-0.1)).toEqual('-$0,1')
8087
expect(numberFormat.clean(true).format(12345.54921)).toEqual('$12.345,55')
8188
expect(numberFormat.clean(true).format(12345.12345)).toEqual('$12.345,12')
8289
expect(numberFormat.clean(true).format(12345.54321)).toEqual('$12.345,54')
8390
expect(numberFormat.clean(true).format(12345.54321)).toEqual('$12.345,54')
8491
})
8592
})
86-
describe('unformat when options are default', () => {
87-
const numberFormat = new NumberFormat({
88-
prefix: '$',
89-
separator: '.',
90-
decimal: ',',
91-
nullValue: '',
92-
})
93-
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')
104-
})
105-
it('unformat numerical value', () => {
106-
expect(numberFormat.clean(true).unformat(0)).toEqual('')
107-
expect(numberFormat.clean(true).unformat(0.)).toEqual('')
108-
expect(numberFormat.clean(true).unformat(0.0)).toEqual('')
109-
expect(numberFormat.clean(true).unformat(-0.10)).toEqual('-0.1')
110-
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')
116-
})
117-
})
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import NumberFormat from '../../src/number-format'
2+
3+
describe('should not throw error on empty config', () => {
4+
expect(() => new NumberFormat({})).not.toThrow()
5+
})
6+
describe('unformat when options are default', () => {
7+
const numberFormat = new NumberFormat({
8+
prefix: '$',
9+
separator: '.',
10+
decimal: ',',
11+
null_value: '',
12+
})
13+
it('unformat string value', () => {
14+
expect(numberFormat.clean(true).unformat()).toEqual('')
15+
expect(numberFormat.clean(true).unformat('')).toEqual('')
16+
expect(numberFormat.clean(true).unformat('0')).toEqual('0')
17+
expect(numberFormat.clean(true).unformat('0,')).toEqual('0')
18+
expect(numberFormat.clean(true).unformat('-0,0')).toEqual('0')
19+
expect(numberFormat.clean(true).unformat('0,10')).toEqual('0.1')
20+
expect(numberFormat.clean(true).unformat('0,0-')).toEqual('0')
21+
expect(numberFormat.clean(true).unformat('0,10-')).toEqual('-0.1')
22+
expect(numberFormat.clean(true).unformat('12.345,54921')).toEqual(
23+
'12345.55'
24+
)
25+
expect(numberFormat.clean(true).unformat('--12.345,12345')).toEqual(
26+
'-12345.12'
27+
)
28+
expect(numberFormat.clean(true).unformat('12.345.54321,12345')).toEqual(
29+
'1234554321.12'
30+
)
31+
expect(numberFormat.clean(true).unformat('-12.345,,54321-')).toEqual(
32+
'-12345.54'
33+
)
34+
})
35+
it('unformat numerical value', () => {
36+
expect(numberFormat.clean(true).unformat(0)).toEqual('0')
37+
expect(numberFormat.clean(true).unformat(0)).toEqual('0')
38+
expect(numberFormat.clean(true).unformat(0.0)).toEqual('0')
39+
expect(numberFormat.clean(true).unformat(-0.1)).toEqual('-0.1')
40+
expect(numberFormat.clean(true).unformat(-0.0)).toEqual('0')
41+
expect(numberFormat.clean(true).unformat(0.1)).toEqual('0.1')
42+
expect(numberFormat.clean(true).unformat(12345.54921)).toEqual('12345.55')
43+
expect(numberFormat.clean(true).unformat(12345.12345)).toEqual('12345.12')
44+
expect(numberFormat.clean(true).unformat(12345.54321)).toEqual('12345.54')
45+
expect(numberFormat.clean(true).unformat(12345.54321)).toEqual('12345.54')
46+
})
47+
})

0 commit comments

Comments
 (0)