<commit_before>var asyncEach = require('async-each');
var asyncEachSeries = require('async-each-series');

var createPhrase = require('../create-phrase');

//Choice::handleParse
module.exports = createPhrase({
  name: 'choice',
  getDefaultProps: function () {
    return {limit: 0};
  },
  _handleParse: function (options, data, done) {
    var this_ = this;

    var eachChild;
    var childrenDone;
    var childHits = {};

    //If there is no limit, parse them all asyncronously
    if (this.props.limit === 0) {

      //function eachChild
      // parse the child and call data if it calls its data
      eachChild = function eachChild(child, done) {

        //function childData
        // set this Choice's value to the value of the child, then call data
        var childData = function(option) {
          var newResult;
          newResult = option.handleValue(this_.props.id, option.result[child.props.id]);
          data(newResult);
        };

        //parse the child
        child.parse(options, childData, done);
      };

      //Parse each child asyncronously
      asyncEach(this.props.children, eachChild, done);

    //If there is a limit, parse them syncronously
    } else {

      //function eachChild
      // parse the child and call data if it calls data
      // track hitCount so that childDone can stop it at the limit
      eachChild = function(child, done) {
        //function childData
        // set this Choice's value to the value of the child, then call data
        // note that this child has had a hit, so increment the hitcount
        var childData = function(option) {
          var newResult;

          childHits[child.props.id] = true;
          newResult = option.handleValue(this_.props.id, option.result[child.props.id]);
          data(newResult);
        };

        //function childDone
        // if hitCount has hit the limit, then pass a true to to the done() cb.
        //  this will make asyncEachSeries stop executing, but childrenDone
        //  will notice that it is not actually an error and behave accordingly
        var childDone = function(err) {
          if (err) {
            done(err);
          } else {
            done(Object.keys(childHits).length >= this_.props.limit ? true : null);
          }
        };

        //parse the child, calling childData on data and childDone on done
        child.parse(options, childData, childDone);
      };

      //function childrenDone
      // if err is defined, it could be either an actual error, or just a true
      //  passed from childDone. If it is an error, pass it on. Otherwise,
      //  it was just a termination indicator - call done without an error.
      childrenDone = function(err) {
        if (!err || err === true) {
          return done();
        } else {
          return done(err);
        }
      };

      //parse the children syncronously, so we can stop once we hit the limit
      asyncEachSeries(this.props.children, eachChild, childrenDone);
    }
  }
});<commit_msg>Refactored choice<commit_after>var asyncEach = require('async-each');
var asyncEachSeries = require('async-each-series');

var createPhrase = require('../create-phrase');

//Choice::handleParse
module.exports = createPhrase({
  name: 'choice',
  getDefaultProps: function () {
    return {limit: 0};
  },
  handleUnlimited: function(options, data, done) {
    var this_ = this;
    //function eachChild
    // parse the child and call data if it calls its data
    function eachChild(child, done) {

      //function childData
      // set this Choice's value to the value of the child, then call data
      function childData(option) {
        var newResult;
        newResult = option.handleValue(this_.props.id, option.result[child.props.id]);
        data(newResult);
      }

      //parse the child
      child.parse(options, childData, done);
    }

    //Parse each child asyncronously
    asyncEach(this.props.children, eachChild, done);

  },
  handleLimited: function (options, data, done) {
    var this_ = this;
    var childHits = {};

    //function eachChild
    // parse the child and call data if it calls data
    // track hitCount so that childDone can stop it at the limit
    function eachChild(child, done) {
      //function childData
      // set this Choice's value to the value of the child, then call data
      // note that this child has had a hit, so increment the hitcount
      var childData = function(option) {
        var newResult;

        childHits[child.props.id] = true;
        newResult = option.handleValue(this_.props.id, option.result[child.props.id]);
        data(newResult);
      };

      //function childDone
      // if hitCount has hit the limit, then pass a true to to the done() cb.
      //  this will make asyncEachSeries stop executing, but childrenDone
      //  will notice that it is not actually an error and behave accordingly
      function childDone(err) {
        if (err) {
          done(err);
        } else {
          done(Object.keys(childHits).length >= this_.props.limit ? true : null);
        }
      }

      //parse the child, calling childData on data and childDone on done
      child.parse(options, childData, childDone);
    }

    //function childrenDone
    // if err is defined, it could be either an actual error, or just a true
    //  passed from childDone. If it is an error, pass it on. Otherwise,
    //  it was just a termination indicator - call done without an error.
    function childrenDone(err) {
      if (!err || err === true) {
        return done();
      } else {
        return done(err);
      }
    }

    //parse the children syncronously, so we can stop once we hit the limit
    asyncEachSeries(this.props.children, eachChild, childrenDone);
  },
  _handleParse: function (options, data, done) {

    if (this.props.limit === 0) {
      this.handleUnlimited(options, data, done);
    } else {
      this.handleLimited(options, data, done);
    }
  }
});
{'fraction_non_alphanumeric': 0.09988308000668114, 'fraction_numerical': 0.0006681142475363287, 'mean_word_length': 2.319290465631929, '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': '6501989', 'n_tokens_mistral': 1664, 'n_tokens_neox': 1623, 'n_words': 597}