Skip to content

Commit

Permalink
[Fix] @input:model-value doesn't allow to write negative numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
dipaksarkar committed Oct 8, 2022
1 parent e8129bb commit fac8cf1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 67 deletions.
4 changes: 4 additions & 0 deletions src/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ export default {
core.updateCursor(el, positionFromEnd)
// trigger input event
el.dispatchEvent(new Event('input'))
} else if ([config.prefix, '-'].includes(character)) {
e.preventDefault()
el.value = ''
el.dispatchEvent(new Event('input'))
}
}
}
Expand Down
19 changes: 12 additions & 7 deletions src/number-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,27 @@ export default function NumberFormat(config = options) {
this.options = Object.assign(options, config)
this.input = ''
this.number = ''
this.isClean = false
this.isClean = true

this.isNull = (input = this.input) =>
!this.numberOnly(input, new RegExp('[^0-9]+', 'gi'))
this.isNull = (input = this.input) => {
if (this.isClean) {
return !this.numberOnly(input, new RegExp('[^0-9]+', 'gi'))
}
return !this.numberOnly(input, new RegExp('[^0-9\\-]+', 'gi'))
}

this.clean = (clean = false) => {
this.isClean = clean
return this
}

this.sign = () => {
const sign =
this.input.toString().indexOf('-') >= 0 && this.realNumber() > 0
if (this.isClean) {
return this.input.toString().indexOf('-') >= 0 && this.realNumber() > 0
? '-'
: ''
return sign
}
return this.input.toString().indexOf('-') >= 0 ? '-' : ''
}

function between(min, n, max) {
Expand Down Expand Up @@ -75,9 +80,9 @@ export default function NumberFormat(config = options) {

this.parts = (number = '', decimal = this.options.decimal) => {
var parts = number.toString().split(decimal)
parts[0] = this.toNumber(parts[0]) || 0

if (parts.length > 1) {
parts[0] = this.toNumber(parts[0]) || 0
parts[1] = parts.slice(1, parts.length).join('')
parts = parts.slice(0, 2)
}
Expand Down
46 changes: 13 additions & 33 deletions tests/unit/number-format.custom.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ describe('when the value is invalid with custom config', () => {
expect(numberFormat.format('-fo.o-')).toEqual('')
expect(numberFormat.format('!@#$%^&*()')).toEqual('')
})
it('should return as follows', () => {
expect(numberFormat.clean(true).format('')).toEqual('')
expect(numberFormat.clean(true).format('foo')).toEqual('')
expect(numberFormat.clean(true).format('-foo')).toEqual('')
expect(numberFormat.clean(true).format('-fo.o-')).toEqual('')
expect(numberFormat.clean(true).format('-fo,o-')).toEqual('')
expect(numberFormat.clean(true).format('!@#$%^&*()')).toEqual('')
})
it('should return as follows', () => {
expect(numberFormat.clean(true).unformat('')).toEqual('')
expect(numberFormat.clean(true).unformat('foo')).toEqual('')
Expand All @@ -53,17 +45,17 @@ describe('format when options are custom', () => {
expect(numberFormat.format()).toEqual('')
expect(numberFormat.format('')).toEqual('')
expect(numberFormat.format('0')).toEqual('$0')
expect(numberFormat.format('0,')).toEqual('$0,')
expect(numberFormat.format('-0,0')).toEqual('$0,0')
expect(numberFormat.format('0,10')).toEqual('$0,10')
expect(numberFormat.format('0,0-')).toEqual('$0,0')
expect(numberFormat.format('0,10-')).toEqual('-$0,10')
expect(numberFormat.format('12.345,54921')).toEqual('$12.345,54921')
expect(numberFormat.format('--12.345,12345')).toEqual('-$12.345,12345')
expect(numberFormat.format('0,')).toEqual('$0')
expect(numberFormat.format('-0,0')).toEqual('$0')
expect(numberFormat.format('0,10')).toEqual('$0,1')
expect(numberFormat.format('0,0-')).toEqual('$0')
expect(numberFormat.format('0,10-')).toEqual('-$0,1')
expect(numberFormat.format('12.345,54921')).toEqual('$12.345,55')
expect(numberFormat.format('--12.345,12345')).toEqual('-$12.345,12')
expect(numberFormat.format('12.345.54321,12945')).toEqual(
'$1.234.554.321,12945'
'$1.234.554.321,13'
)
expect(numberFormat.format('-12.345,,54321-')).toEqual('-$12.345,54321')
expect(numberFormat.format('-12.345,,54321-')).toEqual('-$12.345,54')
})
it('format numerical value', () => {
expect(numberFormat.format(0)).toEqual('$0')
Expand All @@ -72,21 +64,9 @@ describe('format when options are custom', () => {
expect(numberFormat.format(-0.1)).toEqual('-$0,1')
expect(numberFormat.format(-0.0)).toEqual('$0')
expect(numberFormat.format(0.1)).toEqual('$0,1')
expect(numberFormat.format(12345.54921)).toEqual('$12.345,54921')
expect(numberFormat.format(12345.12345)).toEqual('$12.345,12345')
expect(numberFormat.format(12345.54321)).toEqual('$12.345,54321')
expect(numberFormat.format(12345.54321)).toEqual('$12.345,54321')
})
it('format and clean numerical value', () => {
expect(numberFormat.clean(true).format(0)).toEqual('$0')
expect(numberFormat.clean(true).format(0)).toEqual('$0')
expect(numberFormat.clean(true).format(0.0)).toEqual('$0')
expect(numberFormat.clean(true).format(0.1)).toEqual('$0,1')
expect(numberFormat.clean(true).format(-0.0)).toEqual('$0')
expect(numberFormat.clean(true).format(-0.1)).toEqual('-$0,1')
expect(numberFormat.clean(true).format(12345.54921)).toEqual('$12.345,55')
expect(numberFormat.clean(true).format(12345.12345)).toEqual('$12.345,12')
expect(numberFormat.clean(true).format(12345.54321)).toEqual('$12.345,54')
expect(numberFormat.clean(true).format(12345.54321)).toEqual('$12.345,54')
expect(numberFormat.format(12345.54921)).toEqual('$12.345,55')
expect(numberFormat.format(12345.12345)).toEqual('$12.345,12')
expect(numberFormat.format(12345.54321)).toEqual('$12.345,54')
expect(numberFormat.format(12345.54321)).toEqual('$12.345,54')
})
})
40 changes: 13 additions & 27 deletions tests/unit/number-format.default.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,15 @@ describe('format when options are default', () => {
expect(numberFormat.format()).toEqual('')
expect(numberFormat.format('')).toEqual('')
expect(numberFormat.format('0')).toEqual('0')
expect(numberFormat.format('0.')).toEqual('0.')
expect(numberFormat.format('-0.0')).toEqual('0.0')
expect(numberFormat.format('0.10')).toEqual('0.10')
expect(numberFormat.format('0.0-')).toEqual('0.0')
expect(numberFormat.format('0.10-')).toEqual('-0.10')
expect(numberFormat.format('12,345.54921')).toEqual('12,345.54921')
expect(numberFormat.format('--12,345.12345')).toEqual('-12,345.12345')
expect(numberFormat.format('12,345.54321.12345')).toEqual(
'12,345.5432112345'
)
expect(numberFormat.format('-12,345..54321-')).toEqual('-12,345.54321')
expect(numberFormat.format('0.')).toEqual('0')
expect(numberFormat.format('-0.0')).toEqual('0')
expect(numberFormat.format('0.10')).toEqual('0.1')
expect(numberFormat.format('0.0-')).toEqual('0')
expect(numberFormat.format('0.10-')).toEqual('-0.1')
expect(numberFormat.format('12,345.54921')).toEqual('12,345.55')
expect(numberFormat.format('--12,345.12345')).toEqual('-12,345.12')
expect(numberFormat.format('12,345.54321.12345')).toEqual('12,345.54')
expect(numberFormat.format('-12,345..54321-')).toEqual('-12,345.54')
})
it('format numerical value', () => {
expect(numberFormat.format(0)).toEqual('0')
Expand All @@ -55,21 +53,9 @@ describe('format when options are default', () => {
expect(numberFormat.format(-0.1)).toEqual('-0.1')
expect(numberFormat.format(-0.0)).toEqual('0')
expect(numberFormat.format(0.1)).toEqual('0.1')
expect(numberFormat.format(12345.54921)).toEqual('12,345.54921')
expect(numberFormat.format(12345.12345)).toEqual('12,345.12345')
expect(numberFormat.format(12345.54321)).toEqual('12,345.54321')
expect(numberFormat.format(12345.54321)).toEqual('12,345.54321')
})
it('format and clean numerical value', () => {
expect(numberFormat.clean(true).format(0)).toEqual('0')
expect(numberFormat.clean(true).format(0)).toEqual('0')
expect(numberFormat.clean(true).format(0.0)).toEqual('0')
expect(numberFormat.clean(true).format(0.1)).toEqual('0.1')
expect(numberFormat.clean(true).format(-0.0)).toEqual('0')
expect(numberFormat.clean(true).format(-0.1)).toEqual('-0.1')
expect(numberFormat.clean(true).format(12345.54921)).toEqual('12,345.55')
expect(numberFormat.clean(true).format(12345.12345)).toEqual('12,345.12')
expect(numberFormat.clean(true).format(12345.54321)).toEqual('12,345.54')
expect(numberFormat.clean(true).format(12345.54321)).toEqual('12,345.54')
expect(numberFormat.format(12345.54921)).toEqual('12,345.55')
expect(numberFormat.format(12345.12345)).toEqual('12,345.12')
expect(numberFormat.format(12345.54321)).toEqual('12,345.54')
expect(numberFormat.format(12345.54321)).toEqual('12,345.54')
})
})

0 comments on commit fac8cf1

Please sign in to comment.