<commit_before>'use strict';

var Base = require('./base');
var plugin = require('./plugins');
var utils = require('./utils');

/**
 * Expose `Collection`
 */

module.exports = Collection;

/**
 * Create an instance of `Collection` with the given `options`.
 *
 * ```js
 * var collection = new Collection();
 * collection.addItem('foo', {content: 'bar'});
 * ```
 * @param {Object} `options`
 * @api public
 */

function Collection(options) {
  if (!(this instanceof Collection)) {
    return new Collection(options);
  }

  this.options = options || {};
  utils.isName(this, 'Collection');

  Base.call(this);
  this.init(this.options);
}

/**
 * Inherit `Base`
 */

Base.extend(Collection);

/**
 * Initialize `Collection` defaults
 */

Collection.prototype.init = function(opts) {
  this.is('Collection');

  this.define('List', opts.List || require('./list'));
  this.define('Item', opts.Item || require('./item'));
  this.define('loaded', false);

  this.use(plugin.renameKey());
  this.use(plugin.item('item', 'Item'));

  this.queue = [];
  this.items = {};

  // if an instance of `List` or `Collection` is passed, load it now
  if (Array.isArray(opts) || opts.isList) {
    this.options = opts.options;
    this.addList(opts.items);

  } else if (opts.isCollection) {
    this.options = opts.options;
    this.addItems(opts.items);

  } else {
    this.options = opts;
  }
};

/**
 * Set an item on the collection. This is identical to [addItem](#addItem)
 * except `setItem` does not emit an event for each item and does not
 * iterate over the item `queue`.
 *
 * ```js
 * collection.setItem('foo', {content: 'bar'});
 * ```
 *
 * @param {String|Object} `key` Item key or object
 * @param {Object} `value` If key is a string, value is the item object.
 * @developer The `item` method is decorated onto the collection using the `item` plugin
 * @return {Object} returns the `item` instance.
 * @api public
 */

Collection.prototype.setItem = function(key, value) {
  var item = this.item(key, value);
  if (item.use) this.run(item);
  this.items[item.key] = item;
  return item;
};

/**
 * Similar to `setItem`, adds an item to the collection but also fires an
 * event and iterates over the item `queue` to load items from the
 * `addItem` event listener.  An item may be an instance of `Item`, if
 * not, the item is converted to an instance of `Item`.
 *
 * ```js
 * var list = new List(...);
 * list.addItem('a.html', {path: 'a.html', contents: '...'});
 * ```
 * @param {String} `key`
 * @param {Object} `value`
 * @api public
 */

Collection.prototype.addItem = function(/*key, value*/) {
  var args = [].slice.call(arguments);
  this.emit.call(this, 'addItem', args);

  var item = this.setItem.apply(this, args);
  while (this.queue.length) {
    this.setItem(this.queue.shift());
  }
  return item;
};

/**
 * Load multiple items onto the collection.
 *
 * ```js
 * collection.addItems({
 *   'a.html': {content: '...'},
 *   'b.html': {content: '...'},
 *   'c.html': {content: '...'}
 * });
 * ```
 * @param {Object|Array} `items`
 * @return {Object} returns the instance for chaining
 * @api public
 */<commit_msg>adds `deleteItem` method and `isCollection` static method<commit_after>'use strict';

var plugin = require('./plugins');
var utils = require('./utils');
var Base = require('./base');

/**
 * Expose `Collection`
 */

module.exports = Collection;

/**
 * Create an instance of `Collection` with the given `options`.
 *
 * ```js
 * var collection = new Collection();
 * collection.addItem('foo', {content: 'bar'});
 * ```
 * @param {Object} `options`
 * @api public
 */

function Collection(options) {
  if (!(this instanceof Collection)) {
    return new Collection(options);
  }

  utils.isName(this, 'Collection');

  Base.call(this);
  this.init(options || {});
}

/**
 * Inherit `Base`
 */

Base.extend(Collection);

/**
 * Mixin static methods
 */

plugin.is(Collection);

/**
 * Initialize `Collection` defaults
 */

Collection.prototype.init = function(opts) {
  this.is('Collection');

  this.define('List', opts.List || require('./list'));
  this.define('Item', opts.Item || require('./item'));
  this.define('loaded', false);

  this.use(plugin.renameKey());
  this.use(plugin.item('item', 'Item'));

  this.queue = [];
  this.items = {};

  // if an instance of `List` or `Collection` is passed, load it now
  if (Array.isArray(opts) || opts.isList) {
    this.options = opts.options;
    this.addList(opts.items);

  } else if (opts.isCollection) {
    this.options = opts.options;
    this.addItems(opts.items);

  } else {
    this.options = opts;
  }
};

/**
 * Set an item on the collection. This is identical to [addItem](#addItem)
 * except `setItem` does not emit an event for each item and does not
 * iterate over the item `queue`.
 *
 * ```js
 * collection.setItem('foo', {content: 'bar'});
 * ```
 *
 * @param {String|Object} `key` Item key or object
 * @param {Object} `value` If key is a string, value is the item object.
 * @developer The `item` method is decorated onto the collection using the `item` plugin
 * @return {Object} returns the `item` instance.
 * @api public
 */

Collection.prototype.setItem = function(key, value) {
  var item = this.item(key, value);
  if (item.use) this.run(item);
  this.items[item.key] = item;
  return item;
};

/**
 * Similar to `setItem`, adds an item to the collection but also fires an
 * event and iterates over the item `queue` to load items from the
 * `addItem` event listener.  An item may be an instance of `Item`, if
 * not, the item is converted to an instance of `Item`.
 *
 * ```js
 * var list = new List(...);
 * list.addItem('a.html', {path: 'a.html', contents: '...'});
 * ```
 * @param {String} `key`
 * @param {Object} `value`
 * @api public
 */

Collection.prototype.addItem = function(/*key, value*/) {
  var args = [].slice.call(arguments);
  this.emit.call(this, 'addItem', args);

  var item = this.setItem.apply(this, args);
  while (this.queue.length) {
    this.setItem(this.queue.shift());
  }
  return item;
};

/**
 * Delete an item from collection `items`.
 *
 * ```js
 * items.deleteItem('abc');
 * ```
 * @param {String} `key`
 * @return {Object} Returns the instance for chaining
 * @api public
 */

Collection.prototype.deleteItem = function(item) {
  if (typeof item === 'string') {
    item = this.getItem(item);
  }
  delete this.items[item.key];
  return this;
};

/**
 * Load multiple items onto the collection.
 *
 * ```js
 * collection.addItems({
 *   'a.html': {content: '...'},
 *   'b.html': {content: '...'},
 *   'c.html': {content: '...'}
 * });
 * ```
 * @param {Object|Array} `items`
 * @return {Object} returns the instance for chaining
 * @api public
 */
{'fraction_non_alphanumeric': 0.18389261744966443, 'fraction_numerical': 0.0, 'mean_word_length': 3.506720430107527, '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': '7345329', 'n_tokens_mistral': 2171, 'n_tokens_neox': 2171, 'n_words': 634}