<commit_before>import React from "react/addons";

import AppFormErrorMessages from "../constants/AppFormErrorMessages";
import FormActions from "../actions/FormActions";

import Util from "../helpers/Util";

function getNewConsecutiveKey() {
  return parseInt((Util.getUniqueId() + "").slice(-9));
}

var DuplicableRowsMixin = {
  propTypes: {
    errorIndices: React.PropTypes.object.isRequired,
    fields: React.PropTypes.object.isRequired,
    getErrorMessage: React.PropTypes.func.isRequired
  },

  getInitialState: function () {
    if (this.duplicableRowsScheme == null) {
      throw new Error("Please specify an duplicableRowsScheme-object " +
        "in your component.");
    }

    return {
      rows: this.getPopulatedRows()
    };
  },

  componentWillReceiveProps: function (nextProps) {
    this.setState({
      rows: this.getPopulatedRows(nextProps.fields)
    }, this.enforceMinRows);
  },

  componentWillMount: function () {
    this.enforceMinRows();
  },

  enforceMinRows: function () {
    var state = this.state;
    var duplicableRowsScheme = this.duplicableRowsScheme;

    Object.keys(duplicableRowsScheme).forEach(function (fieldId) {
      if (state.rows[fieldId] == null || state.rows[fieldId].length === 0) {
        let rowScheme = Util.extendObject(duplicableRowsScheme[fieldId], {
          consecutiveKey: getNewConsecutiveKey()
        });
        FormActions.insert(fieldId, rowScheme);
      }
    });
  },

  getPopulatedRows: function (fields = this.props.fields) {
    var duplicableRowsScheme = this.duplicableRowsScheme;
    var rows = Object.keys(duplicableRowsScheme).reduce((memo, rowFieldId) => {
      memo[rowFieldId] =
        this.populateInitialConsecutiveKeys(fields[rowFieldId]);
      return memo;
    }, {});
    return rows;
  },

  populateInitialConsecutiveKeys: function (rows) {
    if (rows == null) {
      return null;
    }

    return rows.map(function (row) {
      return Util.extendObject(row, {
        consecutiveKey: row.consecutiveKey != null
          ? row.consecutiveKey
          : getNewConsecutiveKey()
      });
    });
  },

  getDuplicableRowValues: function (fieldId, i) {
    var findDOMNode = React.findDOMNode;
    var refs = this.refs;

    const row = {
      consecutiveKey: this.state.rows[fieldId][i].consecutiveKey
    };

    return Object.keys(this.duplicableRowsScheme[fieldId])
      .reduce(function (memo, key) {
        var input = findDOMNode(refs[`${key}${i}`]);
        memo[key] = input.type !== "checkbox"
          ? input.value
          : input.checked;
        return memo;
      }, row);
  },

  addRow: function (fieldId, position) {
    FormActions.insert(fieldId,
      Util.extendObject(this.duplicableRowsScheme[fieldId], {
        consecutiveKey: getNewConsecutiveKey()
      }),
      position
    );
  },

  updateRow: function (fieldId, position) {
    var row = this.getDuplicableRowValues(fieldId, position);
    FormActions.update(fieldId, row, position);
  },

  removeRow: function (fieldId, position) {
    var row = this.getDuplicableRowValues(fieldId, position);
    FormActions.delete(fieldId, row, position);
  },

  hasOnlyOneSingleEmptyRow: function (fieldId) {<commit_msg>Make getDuplicableRowsValue function more developer save<commit_after>import React from "react/addons";

import AppFormErrorMessages from "../constants/AppFormErrorMessages";
import FormActions from "../actions/FormActions";

import Util from "../helpers/Util";

function getNewConsecutiveKey() {
  return parseInt((Util.getUniqueId() + "").slice(-9));
}

var DuplicableRowsMixin = {
  propTypes: {
    errorIndices: React.PropTypes.object.isRequired,
    fields: React.PropTypes.object.isRequired,
    getErrorMessage: React.PropTypes.func.isRequired
  },

  getInitialState: function () {
    if (this.duplicableRowsScheme == null) {
      throw new Error("Please specify an duplicableRowsScheme-object " +
        "in your component.");
    }

    return {
      rows: this.getPopulatedRows()
    };
  },

  componentWillReceiveProps: function (nextProps) {
    this.setState({
      rows: this.getPopulatedRows(nextProps.fields)
    }, this.enforceMinRows);
  },

  componentWillMount: function () {
    this.enforceMinRows();
  },

  enforceMinRows: function () {
    var state = this.state;
    var duplicableRowsScheme = this.duplicableRowsScheme;

    Object.keys(duplicableRowsScheme).forEach(function (fieldId) {
      if (state.rows[fieldId] == null || state.rows[fieldId].length === 0) {
        let rowScheme = Util.extendObject(duplicableRowsScheme[fieldId], {
          consecutiveKey: getNewConsecutiveKey()
        });
        FormActions.insert(fieldId, rowScheme);
      }
    });
  },

  getPopulatedRows: function (fields = this.props.fields) {
    var duplicableRowsScheme = this.duplicableRowsScheme;
    var rows = Object.keys(duplicableRowsScheme).reduce((memo, rowFieldId) => {
      memo[rowFieldId] =
        this.populateInitialConsecutiveKeys(fields[rowFieldId]);
      return memo;
    }, {});
    return rows;
  },

  populateInitialConsecutiveKeys: function (rows) {
    if (rows == null) {
      return null;
    }

    return rows.map(function (row) {
      return Util.extendObject(row, {
        consecutiveKey: row.consecutiveKey != null
          ? row.consecutiveKey
          : getNewConsecutiveKey()
      });
    });
  },

  getDuplicableRowValues: function (fieldId, i) {
    var findDOMNode = React.findDOMNode;
    var refs = this.refs;

    const row = {
      consecutiveKey: this.state.rows[fieldId][i].consecutiveKey
    };

    return Object.keys(this.duplicableRowsScheme[fieldId])
      .reduce(function (memo, key) {
        var input = findDOMNode(refs[`${key}${i}`]);
        if (input == null) {
          throw new Error("Cannot find input element. " +
            "You need to define a correct ref-attribute at the element.");
        }
        memo[key] = input.type !== "checkbox"
          ? input.value
          : input.checked;
        return memo;
      }, row);
  },

  addRow: function (fieldId, position) {
    FormActions.insert(fieldId,
      Util.extendObject(this.duplicableRowsScheme[fieldId], {
        consecutiveKey: getNewConsecutiveKey()
      }),
      position
    );
  },

  updateRow: function (fieldId, position) {
    var row = this.getDuplicableRowValues(fieldId, position);
    FormActions.update(fieldId, row, position);
  },

  removeRow: function (fieldId, position) {
    var row = this.getDuplicableRowValues(fieldId, position);
    FormActions.delete(fieldId, row, position);
  },

  hasOnlyOneSingleEmptyRow: function (fieldId) {
{'fraction_non_alphanumeric': 0.12320916905444126, 'fraction_numerical': 0.0006032272658724174, 'mean_word_length': 3.1684475172847266, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '3089520', 'n_tokens_mistral': 2032, 'n_tokens_neox': 2013, 'n_words': 380}