<commit_before>/**
 * @constructor
 *
 * @param {Element} el
 * @param {Object} options
 */
function Brique(el, options) {
  this.el = el;
  this.options = options || {};

  this.parent = null;

  if (el === document) {
    this._findAndInitializeChilds();
  } else {
    this.initialize.call(this, options);
    this._insertContent();
  }
}

Brique.prototype = {
  /**
   * Called when Brique is instantiated. By default, it does nothing.
   */
  initialize: function() {},

  /**
   * Returns the The HTML content of the Brique. By default, it returns an
   * empty string.
   */
  content: function() { return ''; },

  /**
   * @private
   */
  _insertContent: function() {
    this.el.innerHTML = this.content();
    this._findAndInitializeChilds();
  },

  /**
   * @private
   */
  _findAndInitializeChilds: function() {
    this.childs = [];

    for (var i = 0, l = Brique.namespaces.length; i < l; i += 1) {
      var namespace = Brique.namespaces[i];
      var childs = this.el.querySelectorAll('[data-' + namespace + '-kind]');

      for (var j = 0, m = childs.length; j < m; j += 1) {
        var el = childs[j];
        var options = getElementOptions(el, namespace);
        var Constructor = Brique.registery[options.kind];
        var brique = new Constructor(el, options);

        brique.parent = this;
        this.childs.push(brique);
      }
    }
  }
};

/**
 * @property namespaces
 * @type Array
 * @default ['brique']
 */
Brique.namespaces = ['brique'];

/**
 * @property registery
 * @type Object
 * @default {}
 */
Brique.registery = {};

/**
 * @param {Object} object
 * @return {Brique} brique
 */
Brique.extend = function extend(object) {
  var constructor = this;

  function Klass() {
    constructor.apply(this, arguments);
  }
  Klass.prototype = Object.create(this.prototype);

  for (var prop in object) {
    Klass.prototype[prop] = object[prop];
  }

  Klass.extend = extend;

  return Klass;
};

/**
 * @param {String} kind
 * @param {Object} object
 */
Brique.register = function(kind, object) {
  Brique.registery[kind] = Brique.extend(object);
};

/**
 * @param {Element} el the root element from where to start to search briques.
 *   If no element is passed, `document` is used.
 */
Brique.initialize = function(el) {
  return new Brique(el || document);
};

/**
 * @private
 *
 * @param {Element} el
 * @param {String} namespace
 * @return {Object} the brique options
 */
function getElementOptions(el, namespace) {
  var options = {};
  var data = el.dataset;

  for (var o in data) {
    var key = o.replace(new RegExp('^' + namespace), '');
    key = key.charAt(0).toLowerCase() + key.slice(1);

    var value;
    try {
      value =
        data[o] === 'true' ? true :
        data[o] === 'false' ? false :
        data[o] === 'null' ? null :
        +data[o] + '' === data[o] ? +data[o] :
        /(?:\{[\s\S]*\}|\[[\s\S]*\])$/.test(data[o]) ? JSON.parse(data[o]) :
        data[o];
    } catch(e) {}

    options[key] = value;
  }

  return options;
}

export Brique;
<commit_msg>Add a bit of documentation<commit_after>/**
 * @constructor
 *
 * The `Brique` base class. Think to a `Brique` as a custom DOM element on which
 * you can pass options and generate a custom content.
 *
 * @param {Element} el
 * @param {Object} options
 */
function Brique(el, options) {
  this.el = el;
  this.options = options || {};

  this.parent = null;

  if (el === document) {
    this._findAndInitializeChilds();
  } else {
    this.initialize.call(this, options);
    this._insertContent();
  }
}

Brique.prototype = {
  /**
   * Called when Brique is instantiated. By default, it does nothing.
   */
  initialize: function() {},

  /**
   * Returns the The HTML content of the Brique. By default, it returns an
   * empty string.
   */
  content: function() { return ''; },

  /**
   * @private
   *
   * Replace the content of `el` DOM element, then it calls
   * `_findAndInitializeChilds`.
   */
  _insertContent: function() {
    this.el.innerHTML = this.content();
    this._findAndInitializeChilds();
  },

  /**
   * @private
   *
   * Resets `childs` array, then finds and initializes childs briques and adds
   * them to the `childs` array.
   */
  _findAndInitializeChilds: function() {
    this.childs = [];

    for (var i = 0, l = Brique.namespaces.length; i < l; i += 1) {
      var namespace = Brique.namespaces[i];
      var childs = this.el.querySelectorAll('[data-' + namespace + '-kind]');

      for (var j = 0, m = childs.length; j < m; j += 1) {
        var el = childs[j];
        var options = getElementOptions(el, namespace);
        var Constructor = Brique.registery[options.kind];
        var brique = new Constructor(el, options);

        brique.parent = this;
        this.childs.push(brique);
      }
    }
  }
};

/**
 * Array of namespaces. They allow us to find briques in the DOM.
 *
 * @property namespaces
 * @type Array
 * @default ['brique']
 */
Brique.namespaces = ['brique'];

/**
 * A registery of briques.
 *
 * @property registery
 * @type Object
 * @default {}
 */
Brique.registery = {};

/**
 * Creates a subclass of `Brique`.
 *
 * @param {Object} object
 * @return {Brique} brique
 */
Brique.extend = function extend(object) {
  var constructor = this;

  function Klass() {
    constructor.apply(this, arguments);
  }
  Klass.prototype = Object.create(this.prototype);

  for (var prop in object) {
    Klass.prototype[prop] = object[prop];
  }

  Klass.extend = extend;

  return Klass;
};

/**
 * Creates a subclass of `Brique` and adds it to the `registery`.
 *
 * @param {String} kind
 * @param {Object} object
 */
Brique.register = function(kind, object) {
  Brique.registery[kind] = Brique.extend(object);
};

/**
 * @param {Element} el the root element from where to start to search briques.
 *   If no element is passed, `document` is used.
 */
Brique.initialize = function(el) {
  return new Brique(el || document);
};

/**
 * @private
 *
 * @param {Element} el
 * @param {String} namespace
 * @return {Object} the brique options
 */
function getElementOptions(el, namespace) {
  var options = {};
  var data = el.dataset;

  for (var o in data) {
    var key = o.replace(new RegExp('^' + namespace), '');
    key = key.charAt(0).toLowerCase() + key.slice(1);

    var value;
    try {
      value =
        data[o] === 'true' ? true :
        data[o] === 'false' ? false :
        data[o] === 'null' ? null :
        +data[o] + '' === data[o] ? +data[o] :
        /(?:\{[\s\S]*\}|\[[\s\S]*\])$/.test(data[o]) ? JSON.parse(data[o]) :
        data[o];
    } catch(e) {}

    options[key] = value;
  }

  return options;
}

export Brique;

{'fraction_non_alphanumeric': 0.1650353862370125, 'fraction_numerical': 0.0018069567836169252, 'mean_word_length': 2.7293655249859627, 'pattern_counts': {'":': 0, '<': 7, '<?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': '173911', 'n_tokens_mistral': 2254, 'n_tokens_neox': 2212, 'n_words': 591}