<commit_before>var _ = require('lodash'),
  references = require('./references'),
  label = require('./label'),
  promises = require('./promises'),
  // hash of all behaviors
  behaviorsHash = {};

/**
 * add a behavior to the hash. called by users who want to create custom behaviors
 * @param {string}   name
 * @param {Function} fn   usually from a browserify require() call
 */
function add(name, fn) {
  // note: this WILL overwrite behaviors already in the hash,
  // allowing people to use custom versions of our core behaviors
  behaviorsHash[name] = fn;
}

function omitMissingBehaviors(behavior) {
  var name = behavior[references.behaviorKey],
    found = !!behaviorsHash[name];

  if (!found) {
    console.warn('Behavior "' + name + '" not found. Make sure you add it!');
  }

  return found;
}

/**
 * expand a single behavior
 * @param  {*} behavior
 * @return {{}}
 */
function expandBehavior(behavior) {
  if (_.isString(behavior) && behavior.length) {
    // _has: text
    return { fn: behavior, args: {} };
  } else if (_.isPlainObject(behavior) && _.isString(behavior[references.behaviorKey])) {
    /* _has:
     *   fn: text
     *   required: true
     */
    return { fn: behavior[references.behaviorKey], args: _.omit(behavior, references.behaviorKey) };
  } else {
    throw new Error('Cannot parse behavior: ' + behavior);
  }
}

/**
 * _has: 'text' or _has: { thing: thing } is shorthand
 * @param {*} behaviors
 * @returns {Boolean|*}
 */
function isShortHandDefinition(behaviors) {
  return _.isString(behaviors) || _.isPlainObject(behaviors);
}

/**
 * get an array of expanded behaviors
 * @param  {*} behaviors
 * @return {[]}           array of {fn: string, args: {}}
 */
function getExpandedBehaviors(behaviors) {
  if (isShortHandDefinition(behaviors)) {
    return [expandBehavior(behaviors)];
  }

  return behaviors.map(expandBehavior);
}

/**
 * run behaviors for a field, in order
 * @param {object} context
 * @param {*} context.value
 * @param {object} context._schema
 * @return {Promise}
 */
function run(context) {
  var contextName = _.get(context, '_schema._name'),
    contextLabel = label(contextName, context._schema),
    behaviors = getExpandedBehaviors(context._schema[references.fieldProperty]),
    runnableBehaviors = _(behaviors).filter(omitMissingBehaviors);

  return promises.transform(runnableBehaviors, function (currentContext, behavior) {
    // apply behaviours
    var name = behavior[references.behaviorKey];<commit_msg>lodash unwrap<commit_after>var _ = require('lodash'),
  references = require('./references'),
  label = require('./label'),
  promises = require('./promises'),
  // hash of all behaviors
  behaviorsHash = {};

/**
 * add a behavior to the hash. called by users who want to create custom behaviors
 * @param {string}   name
 * @param {Function} fn   usually from a browserify require() call
 */
function add(name, fn) {
  // note: this WILL overwrite behaviors already in the hash,
  // allowing people to use custom versions of our core behaviors
  behaviorsHash[name] = fn;
}

function omitMissingBehaviors(behavior) {
  var name = behavior[references.behaviorKey],
    found = !!behaviorsHash[name];

  if (!found) {
    console.warn('Behavior "' + name + '" not found. Make sure you add it!');
  }

  return found;
}

/**
 * expand a single behavior
 * @param  {*} behavior
 * @return {{}}
 */
function expandBehavior(behavior) {
  if (_.isString(behavior) && behavior.length) {
    // _has: text
    return { fn: behavior, args: {} };
  } else if (_.isPlainObject(behavior) && _.isString(behavior[references.behaviorKey])) {
    /* _has:
     *   fn: text
     *   required: true
     */
    return { fn: behavior[references.behaviorKey], args: _.omit(behavior, references.behaviorKey) };
  } else {
    throw new Error('Cannot parse behavior: ' + behavior);
  }
}

/**
 * _has: 'text' or _has: { thing: thing } is shorthand
 * @param {*} behaviors
 * @returns {Boolean|*}
 */
function isShortHandDefinition(behaviors) {
  return _.isString(behaviors) || _.isPlainObject(behaviors);
}

/**
 * get an array of expanded behaviors
 * @param  {*} behaviors
 * @return {[]}           array of {fn: string, args: {}}
 */
function getExpandedBehaviors(behaviors) {
  if (isShortHandDefinition(behaviors)) {
    return [expandBehavior(behaviors)];
  }

  return behaviors.map(expandBehavior);
}

/**
 * run behaviors for a field, in order
 * @param {object} context
 * @param {*} context.value
 * @param {object} context._schema
 * @return {Promise}
 */
function run(context) {
  var contextName = _.get(context, '_schema._name'),
    contextLabel = label(contextName, context._schema),
    behaviors = getExpandedBehaviors(context._schema[references.fieldProperty]),
    runnableBehaviors = _.filter(behaviors, omitMissingBehaviors);

  return promises.transform(runnableBehaviors, function (currentContext, behavior) {
    // apply behaviours
    var name = behavior[references.behaviorKey];
{'fraction_non_alphanumeric': 0.13961430293290478, 'fraction_numerical': 0.0, 'mean_word_length': 3.8292919495635305, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4515027', 'n_tokens_mistral': 1527, 'n_tokens_neox': 1489, 'n_words': 407}