<commit_before>import React, { Component, PropTypes } from 'react';
import connect from 'react-redux/lib/components/connect';

import _get from 'lodash/get';
import capitalize from 'lodash/capitalize';
import compose from 'redux/lib/compose';
import identity from 'lodash/identity';
import isEqual from 'lodash/isEqual';
import mapValues from 'lodash/mapValues';
import partial from 'lodash/partial';

import actions from '../actions';
import Control from './control-component';
import { isMulti, getValue, getValidity } from '../utils';

const {
  asyncSetValidity,
  blur,
  change,
  focus,
  setValidity,
  setErrors,
  toggle,
  xor,
} = actions;

function selector(state, { model }) {
  return {
    model,
    modelValue: _get(state, model),
  };
}

const controlPropsMap = {
  default: props => controlPropsMap.text(props),
  checkbox: props => ({
    name: props.model,
    checked: (() => {
      if (isMulti(props.model)) {
        return (props.modelValue || []).filter(item => isEqual(item, props.value)).length;
      }

      return !!props.modelValue;
    })(),
    ...props,
  }),
  radio: props => ({
    name: props.model,
    checked: isEqual(props.modelValue, props.value),
    value: props.value,
    ...props,
  }),
  select: props => ({
    name: props.model,
    value: props.modelValue,
    ...props,
  }),
  text: props => ({
    defaultValue: props.modelValue,
    name: props.model,
    ...props,
  }),
  textarea: props => controlPropsMap.text(props),
};

function changeMethod(model, value, action = change, parser) {
  return compose(partial(action, model), parser, getValue);
}

function isReadOnlyValue(control) {
  return control.type === 'input' // verify === is okay
    && ~['radio', 'checkbox'].indexOf(control.props.type);
}

const controlActionMap = {
  checkbox: props => isMulti(props.model) ? xor : toggle,
  default: () => change,
};

function getControlType(control, options) {
  const { controlPropsMap: _controlPropsMap } = options;

  try {
    let controlDisplayName = control.constructor.displayName
      || control.type.displayName
      || control.type.name
      || control.type; // what was the + '' for? tests pass without it

    if (controlDisplayName === 'input') {
      controlDisplayName = _controlPropsMap[control.props.type] ? control.props.type : 'text';
    }

    return _controlPropsMap[controlDisplayName] ? controlDisplayName : null;
  } catch (error) {
    return undefined;
  }
}

function sequenceEventActions(control, props) {
  const { dispatch, model, updateOn } = props;

  const updateOnEventHandler = (typeof updateOn === 'function')
    ? 'onChange'
    : `on${capitalize(props.updateOn)}`;
  const validateOn = `on${capitalize(props.validateOn)}`;
  const asyncValidateOn = `on${capitalize(props.asyncValidateOn)}`;

  const updaterFn = (typeof updateOn === 'function')
    ? updateOn
    : identity;

  const eventActions = {
    onFocus: [() => dispatch(focus(model))],
    onBlur: [() => dispatch(blur(model))],
    onChange: [],
    _onLoad: [], // pseudo-event
  };

  const controlAction = (controlActionMap[control.props.type] || controlActionMap.default)(props);

  const controlChangeMethod = changeMethod(
    model,
    props.value,
    controlAction,
    props.parser
  );

  let dispatchChange;
  if (control.props.hasOwnProperty('value') && isReadOnlyValue(control)) {
    dispatchChange = () => dispatch(controlChangeMethod(control.props.value));
  } else {
    dispatchChange = event => dispatch(controlChangeMethod(event));
  }

  eventActions[updateOnEventHandler].push(updaterFn(dispatchChange));

  if (props.validators || props.errors) {
    const dispatchValidate = value => {
      props.validators && dispatch(setValidity(model, getValidity(props.validators, value)));
      props.errors && dispatch(setErrors(model, getValidity(props.errors, value)));

      return value;
    };

    eventActions[validateOn].push(dispatchValidate);
    eventActions._onLoad.push(dispatchValidate);
  }

  if (props.asyncValidators) {
    const dispatchAsyncValidate = value => {
      mapValues(props.asyncValidators,
        (validator, key) => dispatch(asyncSetValidity(model,
          (_, done) => {
            const outerDone = valid => done({ [key]: valid });

            validator(getValue(value), outerDone);
          })
        )
      );

      return value;<commit_msg>clear linting errors<commit_after>import React, { Component, PropTypes } from 'react';
import connect from 'react-redux/lib/components/connect';

import _get from 'lodash/get';
import capitalize from 'lodash/capitalize';
import compose from 'redux/lib/compose';
import identity from 'lodash/identity';
import isEqual from 'lodash/isEqual';
import mapValues from 'lodash/mapValues';
import partial from 'lodash/partial';

import actions from '../actions';
import Control from './control-component';
import { isMulti, getValue, getValidity } from '../utils';

const {
  asyncSetValidity,
  blur,
  change,
  focus,
  setValidity,
  setErrors,
  toggle,
  xor,
} = actions;

function selector(state, { model }) {
  return {
    model,
    modelValue: _get(state, model),
  };
}

const controlPropsMap = {
  default: props => controlPropsMap.text(props),
  checkbox: props => ({
    name: props.model,
    checked: (() => {
      if (isMulti(props.model)) {
        return (props.modelValue || []).filter(item => isEqual(item, props.value)).length;
      }

      return !!props.modelValue;
    })(),
    ...props,
  }),
  radio: props => ({
    name: props.model,
    checked: isEqual(props.modelValue, props.value),
    value: props.value,
    ...props,
  }),
  select: props => ({
    name: props.model,
    value: props.modelValue,
    ...props,
  }),
  text: props => ({
    defaultValue: props.modelValue,
    name: props.model,
    ...props,
  }),
  textarea: props => controlPropsMap.text(props),
};

function changeMethod(model, value, action = change, parser) {
  return compose(partial(action, model), parser, getValue);
}

function isReadOnlyValue(control) {
  return control.type === 'input' // verify === is okay
    && ~['radio', 'checkbox'].indexOf(control.props.type);
}

const controlActionMap = {
  checkbox: props => isMulti(props.model) ? xor : toggle,
  default: () => change,
};

function getControlType(control, options) {
  const { controlPropsMap: _controlPropsMap } = options;

  try {
    let controlDisplayName = control.constructor.displayName
      || control.type.displayName
      || control.type.name
      || control.type; // what was the + '' for? tests pass without it

    if (controlDisplayName === 'input') {
      controlDisplayName = _controlPropsMap[control.props.type] ? control.props.type : 'text';
    }

    return _controlPropsMap[controlDisplayName] ? controlDisplayName : null;
  } catch (error) {
    return undefined;
  }
}

function sequenceEventActions(control, props) {
  const { dispatch, model, updateOn } = props;

  const updateOnEventHandler = (typeof updateOn === 'function')
    ? 'onChange'
    : `on${capitalize(props.updateOn)}`;
  const validateOn = `on${capitalize(props.validateOn)}`;
  const asyncValidateOn = `on${capitalize(props.asyncValidateOn)}`;

  const updaterFn = (typeof updateOn === 'function')
    ? updateOn
    : identity;

  const eventActions = {
    onFocus: [() => dispatch(focus(model))],
    onBlur: [() => dispatch(blur(model))],
    onChange: [],
    _onLoad: [], // pseudo-event
  };

  const controlAction = (controlActionMap[control.props.type] || controlActionMap.default)(props);

  const controlChangeMethod = changeMethod(
    model,
    props.value,
    controlAction,
    props.parser
  );

  let dispatchChange;
  if (control.props.hasOwnProperty('value') && isReadOnlyValue(control)) {
    dispatchChange = () => dispatch(controlChangeMethod(control.props.value));
  } else {
    dispatchChange = event => dispatch(controlChangeMethod(event));
  }

  eventActions[updateOnEventHandler].push(updaterFn(dispatchChange));

  if (props.validators || props.errors) {
    const dispatchValidate = value => {
      if (props.validators) {
        dispatch(setValidity(model, getValidity(props.validators, value)));
      }

      if (props.errors) {
        dispatch(setErrors(model, getValidity(props.errors, value)));
      }

      return value;
    };

    eventActions[validateOn].push(dispatchValidate);
    eventActions._onLoad.push(dispatchValidate);
  }

  if (props.asyncValidators) {
    const dispatchAsyncValidate = value => {
      mapValues(props.asyncValidators,
        (validator, key) => dispatch(asyncSetValidity(model,
          (_, done) => {
            const outerDone = valid => done({ [key]: valid });

            validator(getValue(value), outerDone);
          })
        )
      );

      return value;
{'fraction_non_alphanumeric': 0.14344169129347578, 'fraction_numerical': 0.0, 'mean_word_length': 3.5590673575129532, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 41, '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': '6254632', 'n_tokens_mistral': 2660, 'n_tokens_neox': 2619, 'n_words': 567}