Skip to content

masked method work incorrectly with unmasked results #249

@le0pard

Description

@le0pard

Describe the bug

All inputs have activated number mask with local pt-BR. Last input calculate sum for all other inputs and have mask too:

const mask = new Mask({ number: { locale: 'pt-BR', fraction: 2 } })
const totalAmount = Array.rom(document.querySelectorAll('.inputs')).reduce((agg, target) => {
  const value = target.value ? parseFloat(mask.unmasked(target.value.toString())) : 0

  return agg + (isNaN(value) ? 0 : value)
}, 0)
document.querySelector('.totalInput').value = totalAmount.toString()
document.querySelector('.totalInput').dispatchEvent(new Event('input', { bubbles: true })) // trigger mask.js

This lead that number incorrectly masked in total field. Instead of 10.34 it get 1034. So this mean:

mask.masked(mask.unmasked('10.234,54')) !== '10.234,54'

Steps to reproduce

const { Mask } = Maska;

const mask = new Mask({ number: { locale: 'pt-BR', fraction: 2 } })
console.log('correct', mask.unmasked('10.234,54')) // correct 10234.54
console.log('incorrect', mask.masked('10234.54')) // incorrect 1.023.454 (should be "10.234,54")
console.log('incorrect', mask.masked(mask.unmasked('10.234,54')), '!==', '10.234,54')

Reproduction link

Example

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions