<commit_before>/**
 * Module for angularjs style dependency injection.
 * @module kbox.core.deps
 */

'use strict';

var FLAG_STRING = 'flag';

var _ = require('lodash');
var events = require('./events.js');

var _deps = {};

function _getDep(name) {
  return _deps[name];
}

function _setDep(name, value) {
  _deps[name] = value;
}

function _clear() {
  _deps = {};
}

function _remove(name) {
  delete _deps[name];
}

exports.keys = function() {
  return _.keys(_deps);
};

/**
 * Clears all registered dependencies.
 * @example
 * var deps = require(...);
 * deps.register('foo', 'bar');
 * assert(deps.contains('foo'));
 * deps.clear();
 * assert(!deps.contains('foo'));
 */
exports.clear = function() {
  _clear();
};

/**
 * Removes a given dependency.
 * @arg {string} name - Name of dependency to remove.
 * @example
 * var deps = require(...);
 * deps.register('key', 'value');
 * deps.remove('key');
 */
exports.remove = function(name) {
  _remove(name);
};

/**
 * Searches to find if a dependency exists.
 * @arg {string} name - Name of dependency.
 * @returns - True if dependency exists otherwise false.
 * @example
 * var deps = require(...);
 * if (!deps.contains('docker')) {
 *   deps.register('docker', new Docker(config));
 * }
 */
var contains = exports.contains = function(name) {
  var result = _getDep(name);
  return result !== undefined;
};

/**
 * Registers dependency for later access by other modules.
 * @arg {string} name - Name of dependency.
 * @arg dependency - Dependency to register.
 * @example
 * var deps = require(...);
 * deps.register('app', new App(config));
 */
var register = exports.register = function(name, dep, done) {
  if (!done) {
    done = function() {};
  }

  if (_getDep(name) !== undefined) {
    var msg =
      'Tried to register a dependency "' +
      name +
      '" that was already registered.';
    throw new Error(msg);
  }
  _setDep(name, dep);
  if (name === 'app') {
    events.emit('dep-registered', {key:name, val:dep}, function(err) {
      if (err) {
        done(err);
      } else {
        events.emit('app-registered', dep, done);
      }
    });
  }
};

// @todo: document<commit_msg>Fixed small bug in deps.register where done was not called for non 'app' registration.<commit_after>/**
 * Module for angularjs style dependency injection.
 * @module kbox.core.deps
 */

'use strict';

var FLAG_STRING = 'flag';

var _ = require('lodash');
var events = require('./events.js');

var _deps = {};

function _getDep(name) {
  return _deps[name];
}

function _setDep(name, value) {
  _deps[name] = value;
}

function _clear() {
  _deps = {};
}

function _remove(name) {
  delete _deps[name];
}

exports.keys = function() {
  return _.keys(_deps);
};

/**
 * Clears all registered dependencies.
 * @example
 * var deps = require(...);
 * deps.register('foo', 'bar');
 * assert(deps.contains('foo'));
 * deps.clear();
 * assert(!deps.contains('foo'));
 */
exports.clear = function() {
  _clear();
};

/**
 * Removes a given dependency.
 * @arg {string} name - Name of dependency to remove.
 * @example
 * var deps = require(...);
 * deps.register('key', 'value');
 * deps.remove('key');
 */
exports.remove = function(name) {
  _remove(name);
};

/**
 * Searches to find if a dependency exists.
 * @arg {string} name - Name of dependency.
 * @returns - True if dependency exists otherwise false.
 * @example
 * var deps = require(...);
 * if (!deps.contains('docker')) {
 *   deps.register('docker', new Docker(config));
 * }
 */
var contains = exports.contains = function(name) {
  var result = _getDep(name);
  return result !== undefined;
};

/**
 * Registers dependency for later access by other modules.
 * @arg {string} name - Name of dependency.
 * @arg dependency - Dependency to register.
 * @example
 * var deps = require(...);
 * deps.register('app', new App(config));
 */
var register = exports.register = function(name, dep, done) {
  if (!done) {
    done = function() {};
  }

  if (_getDep(name) !== undefined) {
    var msg =
      'Tried to register a dependency "' +
      name +
      '" that was already registered.';
    throw new Error(msg);
  }
  _setDep(name, dep);
  if (name === 'app') {
    events.emit('dep-registered', {key:name, val:dep}, function(err) {
      if (err) {
        done(err);
      } else {
        events.emit('app-registered', dep, done);
      }
    });
  } else {
    done();
  }
};

// @todo: document
{'fraction_non_alphanumeric': 0.179249547920434, 'fraction_numerical': 0.0, 'mean_word_length': 3.190340909090909, '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': '5169123', 'n_tokens_mistral': 1472, 'n_tokens_neox': 1451, 'n_words': 394}