Skip to content

Commit

Permalink
removed input value replace by config null value
Browse files Browse the repository at this point in the history
  • Loading branch information
dipaksarkar committed Sep 17, 2022
1 parent b3e4f46 commit bb482d3
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 135 deletions.
4 changes: 0 additions & 4 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,8 @@ export function updateCursor(el, position) {
export function updateValue(el, vnode, { emit = true, force = false, clean = false } = {}) {
const { config } = el[CONFIG_KEY]
let { oldValue } = el[CONFIG_KEY]

let currentValue = vnode && vnode.data.model ? vnode.data.model.value : el.value

oldValue = oldValue || ''
currentValue = currentValue || ''

if (force || oldValue !== currentValue) {
const number = new NumberFormat(config).clean(clean && !config.reverseFill)
let masked = number.format(currentValue)
Expand Down
89 changes: 58 additions & 31 deletions src/number-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ export default function NumberFormat(config = options) {
this.number = ''
this.isClean = false

this.isNull = (input = this.input) => !this.numberOnly(input, new RegExp('[^0-9]+', 'gi'))
this.isNull = (input = this.input) =>
!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) ? '-' : ''
const sign =
this.input.toString().indexOf('-') >= 0 && this.realNumber() > 0
? '-'
: ''
return sign
}

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

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

this.numbers = () => {
if (this.options.reverseFill) {
this.number = toFixed(this.numberOnly(this.input, /\D+/g), this.options.precision).replace('.', this.options.decimal)
this.number = toFixed(
this.numberOnly(this.input, /\D+/g),
this.options.precision
).replace('.', this.options.decimal)
} else if (typeof this.input === 'number') {
if (this.isClean) {
this.number = this.toNumber(this.input.toFixed(this.options.precision)).toString().replace('-', '').replace('.', this.options.decimal)
} else {
this.number = this.toNumber(this.input).toString().replace('-', '').replace('.', this.options.decimal)
}
this.number = this.parts(
this.input.toString().replace('-', ''),
'.'
).join(this.options.decimal)
} else {
this.number = this.numberOnly(this.input, new RegExp(`[^0-9\\${this.options.decimal}]+`, 'gi'))
this.number = this.numberOnly(
this.input,
new RegExp(`[^0-9\\${this.options.decimal}]+`, 'gi')
)
this.number = this.parts(this.number).join(this.options.decimal)
}
return this.number
}

this.realNumber = () => {
const number = this.numbers().toString().replace(this.options.decimal, '.')
if (this.options.reverseFill) {
return number
}
return this.toNumber(number)
return this.numbers().toString().replace(this.options.decimal, '.')
}

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[1] = parts.slice(1, parts.length).join('')
parts = parts.slice(0, 2)
if (this.isClean && parts[1].length > this.options.precision) {
parts[1] = this.toNumber(`.${parts[1]}`).toFixed(this.options.precision).toString().replace('0.', '')
}

if (this.isClean) {
const newNumber = this.toNumber(parts.join('.')).toFixed(
this.options.precision
)
const cleanNumber = this.toNumber(newNumber)
const minimumDigits = cleanNumber.toFixed(
this.options.minimumFractionDigits
)

if (
this.options.minimumFractionDigits &&
this.options.minimumFractionDigits >= 0 &&
cleanNumber.toString().length < minimumDigits.length
) {
parts = minimumDigits.toString().split('.')
} else {
parts = cleanNumber.toString().split('.')
}
}

return parts.slice(0, 2)
}

this.addSeparator = () => {
var parts = this.numbers().split(this.options.decimal)
parts[0] = parts[0].toString().replace(/(\d)(?=(?:\d{3})+\b)/gm, `$1${this.options.separator}`)
if (this.isClean) {
parts[1] = this.toNumber(`.${parts[1]}`).toString().replace('0.', '')
return parts[1] && parts[1] > 0 ? parts.join(this.options.decimal) : parts[0]
}
parts[0] = parts[0]
.toString()
.replace(/(\d)(?=(?:\d{3})+\b)/gm, `$1${this.options.separator}`)
return parts.join(this.options.decimal)
}

Expand All @@ -96,22 +118,27 @@ export default function NumberFormat(config = options) {
* @param {Number, String} input
* @return {String}
*/
this.format = (input) => {
if (input === '') return this.options.nullValue
this.input = input || this.options.nullValue
if (this.isNull()) return this.options.nullValue
return this.sign() + this.options.prefix + this.addSeparator() + this.options.suffix
this.format = (input = '') => {
this.input = input
if (this.isNull() && !this.options.reverseFill)
return this.options.nullValue
return (
this.sign() +
this.options.prefix +
this.addSeparator() +
this.options.suffix
)
}

/**
* Unformat the input with default config if there is no constructor config
* @param {Number, String} input
* @return {String}
*/
this.unformat = (input) => {
if (input === '') return this.options.nullValue
this.input = input || this.options.nullValue
if (this.isNull()) return this.options.nullValue
this.unformat = (input = '') => {
this.input = input
if (this.isNull() && !this.options.reverseFill)
return this.options.nullValue
return this.sign() + this.realNumber()
}
}
81 changes: 28 additions & 53 deletions tests/unit/number-format.custom.spec.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import NumberFormat from '../../src/number-format'

describe('should not throw error on empty config', () => {
expect(() => new NumberFormat({
prefix: '$',
separator: '.',
decimal: ',',
nullValue: 0,
})).not.toThrow()
expect(
() =>
new NumberFormat({
prefix: '$',
separator: '.',
decimal: ',',
null_value: 0,
})
).not.toThrow()
})
describe('when the value is invalid with custom config', () => {
const numberFormat = new NumberFormat({
prefix: '$',
separator: '.',
decimal: ',',
nullValue: 0,
null_value: 0,
})
it('should return as follows', () => {
expect(numberFormat.format('')).toEqual('')
Expand Down Expand Up @@ -44,9 +47,11 @@ describe('format when options are custom', () => {
prefix: '$',
separator: '.',
decimal: ',',
nullValue: '',
null_value: '',
})
it('format string value', () => {
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')
Expand All @@ -55,63 +60,33 @@ describe('format when options are custom', () => {
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,12945')).toEqual('$1.234.554.321,12945')
expect(numberFormat.format('12.345.54321,12945')).toEqual(
'$1.234.554.321,12945'
)
expect(numberFormat.format('-12.345,,54321-')).toEqual('-$12.345,54321')
})
it('format numerical value', () => {
expect(numberFormat.format(0)).toEqual('')
expect(numberFormat.format(0.)).toEqual('')
expect(numberFormat.format(0.0)).toEqual('')
expect(numberFormat.format(-0.10)).toEqual('-$0,1')
expect(numberFormat.format(-0.0)).toEqual('')
expect(numberFormat.format(0.10)).toEqual('$0,1')
expect(numberFormat.format(0)).toEqual('$0')
expect(numberFormat.format(0)).toEqual('$0')
expect(numberFormat.format(0.0)).toEqual('$0')
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('')
expect(numberFormat.clean(true).format(0.)).toEqual('')
expect(numberFormat.clean(true).format(0.0)).toEqual('')
expect(numberFormat.clean(true).format(0.10)).toEqual('$0,1')
expect(numberFormat.clean(true).format(-0.0)).toEqual('')
expect(numberFormat.clean(true).format(-0.10)).toEqual('-$0,1')
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')
})
})
describe('unformat when options are default', () => {
const numberFormat = new NumberFormat({
prefix: '$',
separator: '.',
decimal: ',',
nullValue: '',
})
it('unformat string value', () => {
expect(numberFormat.clean(true).unformat('0')).toEqual('0')
expect(numberFormat.clean(true).unformat('0,')).toEqual('0')
expect(numberFormat.clean(true).unformat('-0,0')).toEqual('0')
expect(numberFormat.clean(true).unformat('0,10')).toEqual('0.1')
expect(numberFormat.clean(true).unformat('0,0-')).toEqual('0')
expect(numberFormat.clean(true).unformat('0,10-')).toEqual('-0.1')
expect(numberFormat.clean(true).unformat('12.345,54921')).toEqual('12345.55')
expect(numberFormat.clean(true).unformat('--12.345,12345')).toEqual('-12345.12')
expect(numberFormat.clean(true).unformat('12.345.54321,12345')).toEqual('1234554321.12')
expect(numberFormat.clean(true).unformat('-12.345,,54321-')).toEqual('-12345.54')
})
it('unformat numerical value', () => {
expect(numberFormat.clean(true).unformat(0)).toEqual('')
expect(numberFormat.clean(true).unformat(0.)).toEqual('')
expect(numberFormat.clean(true).unformat(0.0)).toEqual('')
expect(numberFormat.clean(true).unformat(-0.10)).toEqual('-0.1')
expect(numberFormat.clean(true).unformat(-0.0)).toEqual('')
expect(numberFormat.clean(true).unformat(0.10)).toEqual('0.1')
expect(numberFormat.clean(true).unformat(12345.54921)).toEqual('12345.55')
expect(numberFormat.clean(true).unformat(12345.12345)).toEqual('12345.12')
expect(numberFormat.clean(true).unformat(12345.54321)).toEqual('12345.54')
expect(numberFormat.clean(true).unformat(12345.54321)).toEqual('12345.54')
})
})
47 changes: 47 additions & 0 deletions tests/unit/number-format.custom.unformat.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import NumberFormat from '../../src/number-format'

describe('should not throw error on empty config', () => {
expect(() => new NumberFormat({})).not.toThrow()
})
describe('unformat when options are default', () => {
const numberFormat = new NumberFormat({
prefix: '$',
separator: '.',
decimal: ',',
null_value: '',
})
it('unformat string value', () => {
expect(numberFormat.clean(true).unformat()).toEqual('')
expect(numberFormat.clean(true).unformat('')).toEqual('')
expect(numberFormat.clean(true).unformat('0')).toEqual('0')
expect(numberFormat.clean(true).unformat('0,')).toEqual('0')
expect(numberFormat.clean(true).unformat('-0,0')).toEqual('0')
expect(numberFormat.clean(true).unformat('0,10')).toEqual('0.1')
expect(numberFormat.clean(true).unformat('0,0-')).toEqual('0')
expect(numberFormat.clean(true).unformat('0,10-')).toEqual('-0.1')
expect(numberFormat.clean(true).unformat('12.345,54921')).toEqual(
'12345.55'
)
expect(numberFormat.clean(true).unformat('--12.345,12345')).toEqual(
'-12345.12'
)
expect(numberFormat.clean(true).unformat('12.345.54321,12345')).toEqual(
'1234554321.12'
)
expect(numberFormat.clean(true).unformat('-12.345,,54321-')).toEqual(
'-12345.54'
)
})
it('unformat numerical value', () => {
expect(numberFormat.clean(true).unformat(0)).toEqual('0')
expect(numberFormat.clean(true).unformat(0)).toEqual('0')
expect(numberFormat.clean(true).unformat(0.0)).toEqual('0')
expect(numberFormat.clean(true).unformat(-0.1)).toEqual('-0.1')
expect(numberFormat.clean(true).unformat(-0.0)).toEqual('0')
expect(numberFormat.clean(true).unformat(0.1)).toEqual('0.1')
expect(numberFormat.clean(true).unformat(12345.54921)).toEqual('12345.55')
expect(numberFormat.clean(true).unformat(12345.12345)).toEqual('12345.12')
expect(numberFormat.clean(true).unformat(12345.54321)).toEqual('12345.54')
expect(numberFormat.clean(true).unformat(12345.54321)).toEqual('12345.54')
})
})
Loading

0 comments on commit bb482d3

Please sign in to comment.