<commit_before>import NumberField from '../number-field/component';
import { l } from '../../helpers/l';

export default NumberField.extend({

  classNames: ['percent-field'],

  min: 0,

  max: 100,

  precision: 2,

  _format(number) {
    const precision = this.precision;

    return l('percent', number, {
      precision,
      'strip-insignificant-zeros': true
    }) || '';
  },

  _getValue() {
    const value = this.value;
    if (value != null) {
      return this.value * 100;
    }
  },

  _setValue(value) {
    if (value != null) {
      this.onchange(parseFloat((value / 100).toFixed(this.precision + 2), 10));
    } else {
      this.onchange(value);
    }
    this._updateDisplayValue(value);
  },

  _stepValue(step) {
    this._setValue(this._clamp((this._getValue() || 0) + step));
  },

  _trimDecimals(oldString, newString, precision) {
    let trimmedString = newString;

    if (trimmedString.indexOf('.') === -1 &&
        oldString.indexOf('.') !== -1) {
      trimmedString = trimmedString.replace(/%/g, '') + '.' + oldString.slice(
        oldString.indexOf('.'),
        oldString.indexOf('.') + precision + 1
      ).replace(/[.%]/g, '') + '%';
    }

    return trimmedString;
  },

  _moveCursor(previousCursorPosition, previousValue, newValue) {
    if (!this.isFocused) {
      return;
    }

    const input = this.element.querySelector('input');

    if (parseFloat(previousValue, 10) > this.max) {
      input.selectionStart = input.selectionEnd = newValue.length - 1;
    } else if (previousCursorPosition !== null &&
        previousCursorPosition === previousValue.length) {
      input.selectionStart = newValue.length - 1;
      input.selectionEnd = newValue.length - 1;
    } else {
      this._super(previousCursorPosition, previousValue, newValue);
    }
  }
});<commit_msg>update percent field to be localized as well<commit_after>import NumberField from '../number-field/component';

export default NumberField.extend({

  classNames: ['percent-field'],

  min: 0,

  max: 1,

  precision: 2,
  
  step: 0.01,

  multiplier: 0.01,

  style: 'percent'
});
{'fraction_non_alphanumeric': 0.15929624346172136, 'fraction_numerical': 0.015216357584403234, 'mean_word_length': 2.8819188191881917, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '922390', 'n_tokens_mistral': 704, 'n_tokens_neox': 654, 'n_words': 136}