<commit_before>import React from 'react';
import {Map} from 'immutable';
import ValidatingValue from './ValidatingValue';
import {getFirstErrorInMap} from './validation';
import Q from 'q';

const createForm = (setup) => (Component) => {
  // .values is required in setup
  // Discussion: in the future, a function might be allowed, like getValues,
  // that takes props as the first argument...

  return class DecoratedComponent extends React.Component {
    constructor(props) {
      // This needs to be in the constructor because we may want to fetch
      // default text values based on props
      super(props);

      const initialValues = Map(setup.values)
        .map((value, k) => {
          if (typeof value === 'function') {
            return new ValidatingValue(value(props));
          } else if (typeof value === 'object' && value !== null) {
            return new ValidatingValue(value);
          } else {
            throw new Error(`the value "${k}" must be either an object or a function`);
          }
        });

      this.state = {
        values: initialValues,
      };
    }
    render() {
      const getValueForKey = (key) => this.state.values.get(key);

      const changeValueForKey = (key) => (event) => {
        this.setState({
          values: this.state.values.setIn([key, 'text'], event.target.value).setIn([key, 'shouldValidate'], false),
        });
      };

      const checkForValidValues = () => {
        return Q.Promise((resolve, reject) => {
          const values = this.state.values.map(value => value.set('shouldValidate', true));
          const error = getFirstErrorInMap(values);

          this.setState({
            values,
          }, () => {
            if (error) {
              reject(error);
            } else {
              resolve(this.state.values.map(value => value.text));
            }
          });
        });
      };<commit_msg>Some comments<commit_after>import React from 'react';
import {Map} from 'immutable';
import ValidatingValue from './ValidatingValue';
import {getFirstErrorInMap} from './validation';
import Q from 'q';

const createForm = (setup) => (Component) => {
  // .values is required in setup
  // Discussion: in the future, a function might be allowed, like getValues,
  // that takes props as the first argument...

  return class DecoratedComponent extends React.Component {
    constructor(props) {
      // This needs to be in the constructor because we may want to fetch
      // default text values based on props
      super(props);

      const initialValues = Map(setup.values)
        .map((value, k) => {
          if (typeof value === 'function') {
            return new ValidatingValue(value(props));
          } else if (typeof value === 'object' && value !== null) {
            return new ValidatingValue(value);
          } else {
            throw new Error(`the value "${k}" must be either an object or a function`);
          }
        });

      this.state = {
        values: initialValues,
      };
    }
    render() {
      const getValueForKey = (key) => this.state.values.get(key);

      const changeValueForKey = (key) => (event) => {
        this.setState({
          values: this.state.values.setIn([key, 'text'], event.target.value).setIn([key, 'shouldValidate'], false),
        });
      };

      const checkForValidValues = () => {
        return Q.Promise((resolve, reject) => {
          const values = this.state.values.map(value => value.set('shouldValidate', true));
          const error = getFirstErrorInMap(values);

          // This sets the state of the `ValidatingValue`s to shouldValidate
          this.setState({
            values,
          }, () => {
            if (error) {
              reject(error);
            } else {
              resolve(this.state.values.map(value => value.text));
            }
          });
        });
      };
{'fraction_non_alphanumeric': 0.13234162605796357, 'fraction_numerical': 0.0, 'mean_word_length': 2.135048231511254, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, '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': '3983143', 'n_tokens_mistral': 1036, 'n_tokens_neox': 1034, 'n_words': 308}