<commit_before>const MnInput = require('../input/input.class.js')
const evaluate = require('evaluate-string')

module.exports = class MnNumber extends MnInput {
  constructor(self) {
    self = super(self)
    this.delimeterKeys = ['Enter', 'Space']
    return self
  }

  connectedCallback() {
    this.innerHTML = ''
    this._setStyle()
    super._setInput()
    this.setChangeEvents()
    this._setMask()
    this._setMobileKeyboard()
    this._setInputTransforms()
    this._setInputKeys()
    super._setPlaceholder()
    super._setAttributeValue()
    super._setAttributeName()
    super._setAttributeDisabled()
    super._setAttributeReadonly()
    super._setAttributeAutofocus()
    this._setAttributeMaxlength()
    this._setAttributeMax()
    this._setAttributeMin()
    this._setValidations()
  }

  static get observedAttributes() {
    return [
      'value',
      'name',
      'placeholder',
      'disabled',
      'readonly',
      'autofocus',
      'max',
      'min',
    ]
  }

  _setStyle() {
    super._setStyle()
    this.classList.add('mn-number')
  }

  _setMask() {
    this.mask = document.createElement('div')
    this.mask.classList.add('mask')
    this.appendChild(this.mask)

    this.input.addEventListener('input', () => {
      this.updateMask()
    })
  }

  _setMobileKeyboard() {
    this.input.setAttribute('pattern', '\\d*')
  }

  _setInputTransforms() {
    this.input.addEventListener('change', () => {
      const commaOrDot = !this.input.value.endsWith(',')
        && !this.input.value.endsWith('.')

      if (commaOrDot) {
        try {
          const value = eval(this.input.value.replace(/,/g, '.'))

          value !== undefined
            ? this.input.value = String(value).replace(/\./g, ',')
            : null
          const valueIsDefined = value !== undefined

          if (valueIsDefined) {
            const isCurrency = this.hasAttribute('currency')
            const precision = this.getAttribute('precision') || 0

            switch (true) {
              case isCurrency:
                this.input.value = value.toFixed(precision || 2).replace(/\./g, ',')
                break

              default:
                this.input.value = precision
                  ? value.toFixed(precision).replace(/\./g, ',')
                  : String(value).replace(/\./g, ',')
                break
            }
          }
        } catch (e) {
          this.value = undefined
          this.input.value = ''
        }

        this.hasAttribute('percentage')
          ? this.updateMask()
          : null
      }
    })
  }

  setChangeEvents() {
    this.input.addEventListener('blur', this.dispatchChangeEvent)
  }

  _setInputKeys() {
    this.input.addEventListener('keydown', (event) => {
      if (!this.hasAttribute('readonly')) {
        const step = this.hasAttribute('percentage')
          ? ((+this.getAttribute('step') * 100) / 10000) || 0.01
          : +this.getAttribute('step') || 1
        const value = this.value || 0

        switch (event.key) {
          case 'ArrowUp':
            this.value = value + step
            break
          case 'ArrowDown':
            this.value = value - step
            break
        }

        event.key === 'ArrowUp' || event.key === 'ArrowDown'
          ? event.preventDefault()
          : null
      }
    })
  }

  _setAttributeMaxlength() {
    this.maxlength = this.getAttribute('maxlength')
  }

  _setAttributeMax() {
    this.max = this.getAttribute('max')
  }

  _setAttributeMin() {
    this.min = this.getAttribute('min')
  }

  _setValidations() {
    super._setValidations()
    this.validations.required = () => this.hasAttribute('multiple')
      ? this.value.length === 0
      : this.value === undefined
    this.validations.min = () => this.value < this.getAttribute('min')
    this.validations.max = () => this.value > this.getAttribute('max')
    delete this.validations.pattern
  }<commit_msg>adicionada atributo de maxlength ao mn-number.<commit_after>const MnInput = require('../input/input.class.js')
const evaluate = require('evaluate-string')

module.exports = class MnNumber extends MnInput {
  constructor(self) {
    self = super(self)
    this.delimeterKeys = ['Enter', 'Space']
    return self
  }

  connectedCallback() {
    this.innerHTML = ''
    this._setStyle()
    super._setInput()
    this.setChangeEvents()
    this._setMask()
    this._setMobileKeyboard()
    this._setInputTransforms()
    this._setInputKeys()
    super._setPlaceholder()
    super._setAttributeValue()
    super._setAttributeName()
    super._setAttributeDisabled()
    super._setAttributeReadonly()
    super._setAttributeAutofocus()
    this._setAttributeMax()
    this._setAttributeMin()
    this._setValidations()
  }

  static get observedAttributes() {
    return [
      'value',
      'name',
      'placeholder',
      'disabled',
      'readonly',
      'autofocus',
      'max',
      'min',
    ]
  }

  _setStyle() {
    super._setStyle()
    this.classList.add('mn-number')
  }

  _setMask() {
    this.mask = document.createElement('div')
    this.mask.classList.add('mask')
    this.appendChild(this.mask)

    this.input.addEventListener('input', () => {
      this.updateMask()
    })
  }

  _setMobileKeyboard() {
    this.input.setAttribute('pattern', '\\d*')
  }

  _setInputTransforms() {
    this.input.addEventListener('change', () => {
      const commaOrDot = !this.input.value.endsWith(',')
        && !this.input.value.endsWith('.')

      if (commaOrDot) {
        try {
          const value = eval(this.input.value.replace(/,/g, '.'))

          value !== undefined
            ? this.input.value = String(value).replace(/\./g, ',')
            : null
          const valueIsDefined = value !== undefined

          if (valueIsDefined) {
            const isCurrency = this.hasAttribute('currency')
            const precision = this.getAttribute('precision') || 0

            switch (true) {
              case isCurrency:
                this.input.value = value.toFixed(precision || 2).replace(/\./g, ',')
                break

              default:
                this.input.value = precision
                  ? value.toFixed(precision).replace(/\./g, ',')
                  : String(value).replace(/\./g, ',')
                break
            }
          }
        } catch (e) {
          this.value = undefined
          this.input.value = ''
        }

        this.hasAttribute('percentage')
          ? this.updateMask()
          : null
      }
    })
  }

  setChangeEvents() {
    this.input.addEventListener('blur', this.dispatchChangeEvent)
  }

  _setInputKeys() {
    this.input.addEventListener('keydown', (event) => {
      if (!this.hasAttribute('readonly')) {
        const step = this.hasAttribute('percentage')
          ? ((+this.getAttribute('step') * 100) / 10000) || 0.01
          : +this.getAttribute('step') || 1
        const value = this.value || 0

        switch (event.key) {
          case 'ArrowUp':
            this.value = value + step
            break
          case 'ArrowDown':
            this.value = value - step
            break
        }

        event.key === 'ArrowUp' || event.key === 'ArrowDown'
          ? event.preventDefault()
          : null
      }
    })
  }

  _setAttributeMax() {
    this.max = this.getAttribute('max')
  }

  _setAttributeMin() {
    this.min = this.getAttribute('min')
  }

  _setValidations() {
    super._setValidations()
    this.validations.required = () => this.hasAttribute('multiple')
      ? this.value.length === 0
      : this.value === undefined
    this.validations.min = () => this.value < this.getAttribute('min')
    this.validations.max = () => this.value > this.getAttribute('max')
    delete this.validations.pattern
  }
{'fraction_non_alphanumeric': 0.1470173187940988, 'fraction_numerical': 0.004105195638229635, 'mean_word_length': 2.0973381009137864, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4315976', 'n_tokens_mistral': 2314, 'n_tokens_neox': 2272, 'n_words': 363}