<commit_before>import { observe } from 'mobx';
import _ from 'lodash';
import utils from '../utils';
import parser from '../parser';

/**
  Field Utils
*/
export default {

  on(event, callback) {
    const isField = _.has(this, 'isField');
    const path = isField ? this.path : true;
    return observe(this.state.events.$running, change =>
      (event === change.name &&
      (change.newValue !== false) &&
      (this.state.events.$running[event] === path))
        && callback({
          path: this.state.events.$running[event],
          change: _.omit(change, 'type', 'object'),
          field: isField ? this : null,
          form: this.state.form,
          event,
        }));
  },

  /**
   Observer
   */
  observe({ path = null, key, call }) {
    const $field = _.has(this, 'isField') ? this : this.select(path);
    const params = { form: this.state.form, path: $field.path, $field };
    const disposer = `${key}@${$field.path}`;

    _.merge(this.state.disposers, {
      [disposer]: (key === 'fields')
        ? $field.fields.observe(change => call.apply(null, [{ ...params, change }]))
        : observe($field, key, change => call.apply(null, [{ ...params, change }])),
    });
  },

  /**
   Disposer
   */
  dispose(key, path = null) {
    const $path = parser.parsePath(path || this.path);
    this.state.disposers[`${key}@${$path}`]();
    delete this.state.disposers[`${key}@${$path}`];
  },

  /**
   Fields Selector
   */
  select(path, fields = null, isStrict = true) {
    const $path = parser.parsePath(path);

    const keys = _.split($path, '.');
    const head = _.head(keys);

    keys.shift();

    let $fields = _.isNil(fields)<commit_msg>fix: #227<commit_after>import { observe } from 'mobx';
import _ from 'lodash';
import utils from '../utils';
import parser from '../parser';

/**
  Field Utils
*/
export default {

  on(event, callback) {
    const isField = _.has(this, 'isField');
    const path = isField ? this.path : true;
    return observe(this.state.events.$running, change =>
      (event === change.name &&
      (change.newValue !== false) &&
      (this.state.events.$running[event] === path))
        && callback({
          path: this.state.events.$running[event],
          change: _.omit(change, 'type', 'object'),
          field: isField ? this : null,
          form: this.state.form,
          event,
        }));
  },

  /**
   Observer
   */
  observe({ path = null, key, call }) {
    const $field = _.has(this, 'isField') ? this : this.select(path);

    const params = {
      form: this.state.form,
      path: $field.path,
      field: $field,
      $field, // to be removed
    };

    const disposer = `${key}@${$field.path}`;

    _.merge(this.state.disposers, {
      [disposer]: (key === 'fields')
        ? $field.fields.observe(change => call.apply(null, [{ ...params, change }]))
        : observe($field, key, change => call.apply(null, [{ ...params, change }])),
    });
  },

  /**
   Disposer
   */
  dispose(key, path = null) {
    const $path = parser.parsePath(path || this.path);
    this.state.disposers[`${key}@${$path}`]();
    delete this.state.disposers[`${key}@${$path}`];
  },

  /**
   Fields Selector
   */
  select(path, fields = null, isStrict = true) {
    const $path = parser.parsePath(path);

    const keys = _.split($path, '.');
    const head = _.head(keys);

    keys.shift();

    let $fields = _.isNil(fields)
{'fraction_non_alphanumeric': 0.194793799356537, 'fraction_numerical': 0.0008774495466510676, 'mean_word_length': 2.5477178423236513, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '2087809', 'n_tokens_mistral': 1136, 'n_tokens_neox': 1122, 'n_words': 245}