<commit_before>import isObject from 'lodash/lang/isPlainObject';
import camelCase from 'lodash/string/camelCase';
import Watcher from './watcher';

let getAvailableParams = (component) => {
  let params = component.getParams();
  let keys = [];

  if (isObject(params)) {
    keys = Object.keys(params);
  }

  // TODO: only if restrict == A?
  let name = component.getName();
  keys.push(name);

  let pipes = component.getPipes();
  for (let key of Object.keys(pipes)) {
    let translatedKey = camelCase(key);
    keys.push(translatedKey);
  }

  return keys;
};

let _scope = Symbol('$scope');
let _attrs = Symbol('$attrs');
let _definitions = Symbol('definitions');
let _watcher = Symbol('$watcher');
let _name = Symbol('name');
let _pipes = Symbol('pipes');

/**
 * TODO: redevelop component
 */
export default class DirectiveParams {
  constructor($scope, $attrs, component) {
    this[_scope] = $scope;
    this[_attrs] = $attrs;
    this[_name] = component.getName();
    this[_definitions] = getAvailableParams(component);

    this[_watcher] = new Watcher($scope);

    let pipes = component.getPipes();

    this[_pipes] = {};
    for (let key of Object.keys(pipes)) {
      let translatedKey = camelCase(key);

      this[_pipes][translatedKey] = {
        pipe: pipes[key],
        value: null
      };
    }
  }

  isAvailable(key) {
    return this[_definitions].indexOf(key) != -1;
  }

  get(key) {
    if (!this.isAvailable(key)) {
      throw new Error(`"${this[_name]}" - directive param "${key}" is not defined at directive config`);
    }

    let value = this[_scope][key];
    let attrs = this[_attrs].$attr;

    if (this[_pipes].hasOwnProperty(key)) {
      let state = this[_pipes][key];
      let Pipe = state.pipe;

      if (!attrs.hasOwnProperty(key)) {
        if (!state.value) {
          state.value = new Pipe();
        }

        value = state.value;
      } else {
        if (!(value instanceof Pipe)) {
          throw new Error(`"${this[_name]}" - directive pipe "${key}" has wrong class`);
        }
      }
    }

    return value;
  }

  watch(key, cb) {
    if (!this.isAvailable(key)) {
      throw new Error(`"${this[_name]}" - can not initialize watcher for "${key}" because this params is not defined at directive config`);
    }

    return this[_watcher].watch(key, cb);
  }
}<commit_msg>- add parse(key) method to directive params<commit_after>import isObject from 'lodash/lang/isPlainObject';
import camelCase from 'lodash/string/camelCase';
import Watcher from './watcher';

let getAvailableParams = (component) => {
  let params = component.getParams();
  let keys = [];

  if (isObject(params)) {
    keys = Object.keys(params);
  }

  // TODO: only if restrict == A?
  let name = component.getName();
  keys.push(name);

  let pipes = component.getPipes();
  for (let key of Object.keys(pipes)) {
    let translatedKey = camelCase(key);
    keys.push(translatedKey);
  }

  return keys;
};

let _scope = Symbol('$scope');
let _attrs = Symbol('$attrs');
let _definitions = Symbol('definitions');
let _watcher = Symbol('$watcher');
let _name = Symbol('name');
let _pipes = Symbol('pipes');

/**
 * TODO: redevelop component
 */
export default class DirectiveParams {
  constructor($scope, $attrs, component) {
    this[_scope] = $scope;
    this[_attrs] = $attrs;
    this[_name] = component.getName();
    this[_definitions] = getAvailableParams(component);

    this[_watcher] = new Watcher($scope);

    let pipes = component.getPipes();

    this[_pipes] = {};
    for (let key of Object.keys(pipes)) {
      let translatedKey = camelCase(key);

      this[_pipes][translatedKey] = {
        pipe: pipes[key],
        value: null
      };
    }
  }

  isAvailable(key) {
    return this[_definitions].indexOf(key) != -1;
  }

  get(key) {
    if (!this.isAvailable(key)) {
      throw new Error(`"${this[_name]}" - directive param "${key}" is not defined at directive config`);
    }

    let value = this[_scope][key];
    let attrs = this[_attrs].$attr;

    if (this[_pipes].hasOwnProperty(key)) {
      let state = this[_pipes][key];
      let Pipe = state.pipe;

      if (!attrs.hasOwnProperty(key)) {
        if (!state.value) {
          state.value = new Pipe();
        }

        value = state.value;
      } else {
        if (!(value instanceof Pipe)) {
          throw new Error(`"${this[_name]}" - directive pipe "${key}" has wrong class`);
        }
      }
    }

    return value;
  }

  watch(key, cb) {
    if (!this.isAvailable(key)) {
      throw new Error(`"${this[_name]}" - can not initialize watcher for "${key}" because this params is not defined at directive config`);
    }

    return this[_watcher].watch(key, cb);
  }

  parse(key) {
    let $scope = this[_scope];
    let $attrs = this[_attrs];

    if (!$attrs.hasOwnProperty(key)) {
      throw new Error(`"${this[_name]}" - can not parse "${key}" because this params is not passed to attributes`);
    }

    let expression = $attrs[key];
    return $scope.$eval(expression);
  }
}
{'fraction_non_alphanumeric': 0.15572792362768498, 'fraction_numerical': 0.0003977724741447892, 'mean_word_length': 3.0264211369095277, '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': '3003861', 'n_tokens_mistral': 1620, 'n_tokens_neox': 1566, 'n_words': 407}