<commit_before>import key from './key';
import * as helpers from './helpers';
import { getActionType, getHandlerForAction } from './actions';
import ValueHistory from './valueHistory';
import {ActionType, DragState, Range} from './constants';

const DEFAULTS = {
  scale: 2,
  range: Range.ALL,
  fixed: true,
  thousands: ',',
  decimal: '.',
  shortcuts: {
    'k': 1000,
    'm': 1000000,
    'b': 1000000000
  },
  invalidKeyCallback: () => {}
};

type FinputElement = HTMLInputElement & { rawValue: number };

class Finput {
  private readonly _element: FinputElement;
  private _options: any;
  private readonly _history: ValueHistory;
  private readonly _listeners: { [key: string]: { element: HTMLInputElement | Document, handler: EventListenerObject }};
  private _dragState: DragState;

  constructor(element, options) {
    this._element = element;
    this._options = {
      ...DEFAULTS,
      ...options
    };

    this._history = new ValueHistory();

    this._listeners = {
      blur:     { element: this.element, handler: this.onFocusout.bind(this) },
      focus:    { element: this.element, handler: this.onFocusin.bind(this) },
      drop:     { element: this.element, handler: this.onDrop.bind(this) },
      paste:    { element: this.element, handler: this.onPaste.bind(this) },
      keydown:  { element: this.element, handler: this.onKeydown.bind(this) },
      input:    { element: this.element, handler: this.onInput.bind(this) },

      dragstart:    { element: document, handler: this.onDragstart.bind(this) },
      dragend:    { element: document, handler: this.onDragend.bind(this) }
    };

    // Setup listeners
    this.removeListeners();
    for (let e in this._listeners) {
      this._listeners[e].element.addEventListener(e, this._listeners[e].handler);
    }
  }

  // GETTERS
  get element() {
    return this._element;
  }

  get options() {
    return {
      ...this._options
    };
  }
  set options(options) {
    this._options = {
      ...this._options,
      ...options
    };
  }

  getRawValue(value) {
    return helpers.formattedToRaw(value, this.options);
  }

  setValue(val, notNull) {
    const newValue = helpers.fullFormat(val, this.options);

    if (notNull ? val : true) {
      this.element.value = newValue;
      this.element.rawValue = this.getRawValue(this.element.value);
      this._history.addValue(newValue);
    }
  }

  setRawValue(val: any) {
    let value: string;
    if (!val) {
      value = '';
    } else if (typeof val === 'number' && !isNaN(val)) {
      value = helpers.rawToFormatted(val, this.options);
    } else if (typeof val === 'string') {
      value = val;
    } else {
      return;
    }

    const newValue = helpers.parseString(value, this.options);
    this.setValue(newValue, false);
  }

  onFocusout() {
    this.setValue(this.element.value, false);
  }

  onFocusin(e) {
    if(this.options.onFocusinCallback){
      let selection = this.options.onFocusinCallback(e);
      if(selection){
        this.element.selectionStart = selection.start;
        this.element.selectionEnd = selection.end;
      }
    }
    else {
      this.element.selectionStart = 0;
      this.element.selectionEnd = this.element.value.length;
    }
  }

  onDrop(e) {
    switch (this._dragState) {
      case DragState.INTERNAL:
        // This case is handled by the 'onInput' function
        break;
      case DragState.EXTERNAL:
        const val = helpers.parseString(e.dataTransfer.getData('text'), this.options);
        this.setValue(val, true);
        e.preventDefault();
        break;
      default:
        // Do nothing;
        break;
    }
  }

  onDragstart(e) {
    this._dragState = (e.target === this.element)
      ? DragState.INTERNAL
      : DragState.EXTERNAL;
  }

  onDragend(e) {<commit_msg>chore: remove unused argument<commit_after>import key from './key';
import * as helpers from './helpers';
import { getActionType, getHandlerForAction } from './actions';
import ValueHistory from './valueHistory';
import {ActionType, DragState, Range} from './constants';

const DEFAULTS = {
  scale: 2,
  range: Range.ALL,
  fixed: true,
  thousands: ',',
  decimal: '.',
  shortcuts: {
    'k': 1000,
    'm': 1000000,
    'b': 1000000000
  },
  invalidKeyCallback: () => {}
};

type FinputElement = HTMLInputElement & { rawValue: number };

class Finput {
  private readonly _element: FinputElement;
  private _options: any;
  private readonly _history: ValueHistory;
  private readonly _listeners: { [key: string]: { element: HTMLInputElement | Document, handler: EventListenerObject }};
  private _dragState: DragState;

  constructor(element, options) {
    this._element = element;
    this._options = {
      ...DEFAULTS,
      ...options
    };

    this._history = new ValueHistory();

    this._listeners = {
      blur:     { element: this.element, handler: this.onFocusout.bind(this) },
      focus:    { element: this.element, handler: this.onFocusin.bind(this) },
      drop:     { element: this.element, handler: this.onDrop.bind(this) },
      paste:    { element: this.element, handler: this.onPaste.bind(this) },
      keydown:  { element: this.element, handler: this.onKeydown.bind(this) },
      input:    { element: this.element, handler: this.onInput.bind(this) },

      dragstart:    { element: document, handler: this.onDragstart.bind(this) },
      dragend:    { element: document, handler: this.onDragend.bind(this) }
    };

    // Setup listeners
    this.removeListeners();
    for (let e in this._listeners) {
      this._listeners[e].element.addEventListener(e, this._listeners[e].handler);
    }
  }

  // GETTERS
  get element() {
    return this._element;
  }

  get options() {
    return {
      ...this._options
    };
  }
  set options(options) {
    this._options = {
      ...this._options,
      ...options
    };
  }

  getRawValue(value) {
    return helpers.formattedToRaw(value, this.options);
  }

  setValue(val, notNull) {
    const newValue = helpers.fullFormat(val, this.options);

    if (notNull ? val : true) {
      this.element.value = newValue;
      this.element.rawValue = this.getRawValue(this.element.value);
      this._history.addValue(newValue);
    }
  }

  setRawValue(val: any) {
    let value: string;
    if (!val) {
      value = '';
    } else if (typeof val === 'number' && !isNaN(val)) {
      value = helpers.rawToFormatted(val, this.options);
    } else if (typeof val === 'string') {
      value = val;
    } else {
      return;
    }

    const newValue = helpers.parseString(value, this.options);
    this.setValue(newValue, false);
  }

  onFocusout() {
    this.setValue(this.element.value, false);
  }

  onFocusin(e) {
    if(this.options.onFocusinCallback){
      let selection = this.options.onFocusinCallback(e);
      if(selection){
        this.element.selectionStart = selection.start;
        this.element.selectionEnd = selection.end;
      }
    }
    else {
      this.element.selectionStart = 0;
      this.element.selectionEnd = this.element.value.length;
    }
  }

  onDrop(e) {
    switch (this._dragState) {
      case DragState.INTERNAL:
        // This case is handled by the 'onInput' function
        break;
      case DragState.EXTERNAL:
        const val = helpers.parseString(e.dataTransfer.getData('text'), this.options);
        this.setValue(val, true);
        e.preventDefault();
        break;
      default:
        // Do nothing;
        break;
    }
  }

  onDragstart(e) {
    this._dragState = (e.target === this.element)
      ? DragState.INTERNAL
      : DragState.EXTERNAL;
  }

  onDragend() {
{'fraction_non_alphanumeric': 0.13700249573098647, 'fraction_numerical': 0.006042296072507553, 'mean_word_length': 2.688953488372093, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '5673228', 'n_tokens_mistral': 2450, 'n_tokens_neox': 2395, 'n_words': 507}