<commit_before>'use strict';

var path = require('path');
var util = require('util');
var Collection = require('./collection');
var utils = require('./utils');

/**
 * Lazily required dependencies
 */

var lazy = require('lazy-cache')(require);
var hasValues = lazy('has-values');
var get = lazy('get-value');
var mm = lazy('micromatch');
var clone = lazy('clone-deep');

/**
 * Create an instance of `Views`.
 *
 * @api public
 */

function Views(options) {
  Collection.call(this, options);
  this.options.collection = this.options.collection || this;
  utils.defineProp(this, 'View', this.options.View || require('./view'));
  utils.defineProp(this, 'Item', this.View);
}

/**
 * Inherit `Collection`
 */

Collection.extend(Views);

/**
 * Expose `extend`, to allow other classes to inherit
 * from the `Views` class.
 *
 * ```js
 * function MyCustomViews(options) {...}
 * Views.extend(MyCustomViews);
 * ```
 *
 * @param  {Object} `Ctor` Constructor function to extend with `Views`
 * @return {undefined}
 * @api public
 */

Views.extend = function (obj) {
  util.inherits(obj, Views);
};

/**
 * `Views` prototype methods
 */

utils.delegate(Views.prototype, {
  constructor: Views,

  /**
   * Set an item on the collection.
   */

  set: function (key, val) {
    var View = this.get('View');
    key = this.renameKey(key);
    val.key = val.key || key;
    var opts = clone()(this.options);
    this[key] = new View(val, opts);
    this.setItem(key, this[key]);
    return this;
  },

  /**
   * Get a view.
   */

  get: function(prop, fn) {
    var name, res;
    if (arguments.length === 0) {
      res = {};
      for (var key in this) {
        res[key] = this[key];
      }
      return res;
    }

    // use renameKey function passed on args
    if (typeof fn === 'function') {
      prop = fn(prop);
    }

    if (!!(name = this[prop])) {
      return name;
    }

    // try again with the `renameKey` function
    name = this.renameKey(prop);

    if (name && name !== prop && !!(res = this[name])) {
      res.__proto__ = this;
      return res;
    }

    res = get()(this, prop);
    if (!res) {
      res = this.find(prop);
    }
    res.__proto__ = this;
    return res;
  },

  /**
   * Find a view by `key` or glob pattern.
   *
   * @param  {String} `pattern` Key or glob pattern.
   * @param  {Object} `options` Options for [micromatch]
   * @return {Object} Matching view.
   */

  find: function (pattern, options) {
    var self = this;
    function find() {
      var isMatch = mm().matcher(pattern, options);
      for (var key in self) {
        var val = self[key];
        if (typeof val === 'object' && isMatch(key)) {
          return val;
        }
      }
    }
    var res = this.fragmentCache(pattern, find);
    res.__proto__ = this;
    return res;
  },

  /**
   * Compile a view in the collection.<commit_msg>clean up `get` method on `Views`<commit_after>'use strict';

var path = require('path');
var util = require('util');
var Collection = require('./collection');
var utils = require('./utils');

/**
 * Lazily required dependencies
 */

var lazy = require('lazy-cache')(require);
var hasValues = lazy('has-values');
var get = lazy('get-value');
var mm = lazy('micromatch');
var clone = lazy('clone-deep');

/**
 * Create an instance of `Views`.
 *
 * @api public
 */

function Views(options) {
  Collection.call(this, options);
  this.options.collection = this.options.collection || this;
  this.define('View', this.options.View || require('./view'));
  this.define('Item', this.View);
}

/**
 * Inherit `Collection`
 */

Collection.extend(Views);

/**
 * Expose `extend`, to allow other classes to inherit
 * from the `Views` class.
 *
 * ```js
 * function MyCustomViews(options) {...}
 * Views.extend(MyCustomViews);
 * ```
 *
 * @param  {Object} `Ctor` Constructor function to extend with `Views`
 * @return {undefined}
 * @api public
 */

Views.extend = function (obj) {
  util.inherits(obj, Views);
};

/**
 * `Views` prototype methods
 */

utils.delegate(Views.prototype, {
  constructor: Views,

  /**
   * Set an item on the collection.
   */

  set: function (key, val) {
    var View = this.get('View');
    key = this.renameKey(key);
    val.key = val.key || key;
    var opts = clone()(this.options);
    this[key] = new View(val, opts);
    this.setItem(key, this[key]);
    return this;
  },

  /**
   * Get a view.
   */

  get: function(prop) {
    var res = this[prop];
    if (typeof res === 'undefined') {
      // try again with the `renameKey` function
      var name = this.renameKey(prop);
      if (name && name !== prop) {
        res = this[name];
      }
    }
    if (typeof res === 'undefined') {
      res = get()(this, prop);
    }
    if (typeof res === 'undefined') {
      res = this.find(prop);
    }
    return res;
  },

  /**
   * Find a view by `key` or glob pattern.
   *
   * @param  {String} `pattern` Key or glob pattern.
   * @param  {Object} `options` Options for [micromatch]
   * @return {Object} Matching view.
   */

  find: function (pattern, options) {
    var self = this;
    function find() {
      var isMatch = mm().matcher(pattern, options);
      for (var key in self) {
        var val = self[key];
        if (typeof val === 'object' && isMatch(key)) {
          return val;
        }
      }
    }
    var res = this.fragmentCache(pattern, find);
    res.__proto__ = this;
    return res;
  },

  /**
   * Compile a view in the collection.
{'fraction_non_alphanumeric': 0.16963139556207593, 'fraction_numerical': 0.00018338529249954154, 'mean_word_length': 2.65304755525787, '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': '4956934', 'n_tokens_mistral': 1790, 'n_tokens_neox': 1769, 'n_words': 496}