<commit_before>import fp from 'mostly-func';

export default function depopulate (target, opts = { idField: 'id' }) {

  return function(hook) {
    let options = Object.assign({}, opts);

    const getDepopulated = function (item, target) {
      let field = fp.path(target.split('.'), item);
      if (field === undefined) return undefined;
      if (Array.isArray(field)) {
        field = fp.map((it) => it[options.idField] || it, field);
      } else if (field) {
        field = field[options.idField] || field;
      }
      return field? field : null;
    };

    const setTarget = function (data, target, value) {
      if (value !== undefined) {
        return fp.assocPath(target.split('.'), value, data);
      }
      return data;
    };

    if (hook.type === 'before') {
      hook.data = setTarget(hook.data, target, getDepopulated(hook.data, target));
    } else {
      if (hook.result) {
        if (hook.result.data) {
          hook.result.data = setTarget(hook.result.data, target, getDepopulated(hook.result.data, target));
        } else {
          hook.result = setTarget(hook.result, target, getDepopulated(hook.result, target));
        }
      }
    }
    return hook;
  };
}<commit_msg>Fix depopulate with field in array (like groups.group)<commit_after>import assert from 'assert';
import fp from 'mostly-func';
import { get, set, map } from 'lodash';
import { getField, setField } from '../helpers';

export default function depopulate (target, opts = { idField: 'id' }) {
  assert(target, 'target is empty');

  return function(hook) {
    let options = Object.assign({}, opts);

    const depopulated = function (data, target) {
      const fields = target.split('.');
      const init = fp.init(fields).join('.'), last = fp.last(fields);
      let value = init? get(data, init) : data;
      if (Array.isArray(value)) {
        value = fp.map(it => {
          if (it[last]) {
            it[last] = it[last][options.idField] || it[last];
          }
          return it;
        }, value);
      } else if (value[last]) {
        value[last] = value[last][options.idField] || value[last];
      }
      return data;
    };

    if (hook.type === 'before') {
      hook.data = depopulated(hook.data, target);
    } else {
      if (hook.result) {
        if (hook.result.data) {
          hook.result.data = depopulated(hook.result.data, target);
        } else {
          hook.result = depopulated(hook.result, target);
        }
      }
    }
    return hook;
  };
}
{'fraction_non_alphanumeric': 0.15495207667731628, 'fraction_numerical': 0.0, 'mean_word_length': 2.4221311475409837, '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': '4003283', 'n_tokens_mistral': 783, 'n_tokens_neox': 774, 'n_words': 193}