<commit_before>/* @flow */

/**
 * @file The prototype object for `Plugin`.
 *
 * @author <NAME>
 * @license MIT
 */

/**
 * A generic prototype for wrapping a third-party plugin to allow
 * for a common execution pattern.
 *
 * @class Plugin
 * @since 0.1.0
 */
export class Plugin implements PluginInterface {
  container: string;
  target: string;
  plugin: Function;
  defaultOptions: Object;
  instanceOptions: Object;
  isJQueryPlugin: boolean;
  initialize: ?string => void;
  callPlugin: (?string, ?string, ?(string | Object), ?Object) => void;
  execute: Function => void;
  isFunction: any => boolean;
  setOptions: ?Object => Object;
  getContainer: ?string => string;

  /**
   * Creates a new {@link Plugin}.
   *
   * @constructor
   * @property {Function} plugin The primary plugin entry.
   * @property {string} [container] A selector string for a container to instantiate the plugin within.
   * @property {string} [target] A selector string for a target to execute the plugin against.
   * @property {Object} [defaultOptions] Default options shared by all instances of the plugin.
   * @property {Object} [instanceOptions] Options specific to the current instance.
   * @property {boolean} [isJQueryPlugin] Whether or not the plugin requires jQuery.
   */
  constructor(props: PluginInterface) {
    this.plugin = props.plugin;
    this.container = props.container || 'body';
    this.target = props.target || 'body';
    this.defaultOptions = props.defaultOptions || {};
    this.instanceOptions = props.instanceOptions || {};
    this.isJQueryPlugin = props.isJQueryPlugin || false;
  }

  /**
   * Initializes the {@link Plugin}.
   *
   * @since 0.1.0
   */
  initialize(container?: string) {
    this.callPlugin(this.getContainer(container));
  }

  /**
   * Call the main {@link Plugin} function against a selector. All arguments are optional,
   * and instance values will be used if one or the other is not provided.
   *
   * @since 0.1.0
   *
   * @param {string} [container] - A valid selector string to execute the plugin within.
   * @param {string} [target] - A valid selector string or element to execute the plugin against.
   * @param {(string|Object)} [options] - Info to provide to the function call.
   * @param {Object} [params] - Additional parameters to provide.
   *
   * @return {void}
   */
  callPlugin(
    container?: string,
    target?: string,
    options?: string | Object,
    params?: Object
  ) {
    container = this.getContainer(container);
    target = target || this.target;
    options = options || this.instanceOptions;

    if (typeof options === 'object') {
      options = this.setOptions(options);
    }

    if (target && this.foundElements(target, container)) {
      // if the plugin requires jQuery, we'll check to make sure
      // jQuery is defined, since this library does not add it.
      if (this.isJQueryPlugin && window.jQuery !== 'undefined') {
        target = window.jQuery(target);
      }

      const args = [].concat([target], [options], params ? [params] : []);
      this.execute(this.plugin(...args));
    }
  }
<commit_msg>Proper check for defined jQuery<commit_after>/* @flow */

/**
 * @file The prototype object for `Plugin`.
 *
 * @author <NAME>
 * @license MIT
 */

/**
 * A generic prototype for wrapping a third-party plugin to allow
 * for a common execution pattern.
 *
 * @class Plugin
 * @since 0.1.0
 */
export class Plugin implements PluginInterface {
  container: string;
  target: string;
  plugin: Function;
  defaultOptions: Object;
  instanceOptions: Object;
  isJQueryPlugin: boolean;
  initialize: ?string => void;
  callPlugin: (?string, ?string, ?(string | Object), ?Object) => void;
  execute: Function => void;
  isFunction: any => boolean;
  setOptions: ?Object => Object;
  getContainer: ?string => string;

  /**
   * Creates a new {@link Plugin}.
   *
   * @constructor
   * @property {Function} plugin The primary plugin entry.
   * @property {string} [container] A selector string for a container to instantiate the plugin within.
   * @property {string} [target] A selector string for a target to execute the plugin against.
   * @property {Object} [defaultOptions] Default options shared by all instances of the plugin.
   * @property {Object} [instanceOptions] Options specific to the current instance.
   * @property {boolean} [isJQueryPlugin] Whether or not the plugin requires jQuery.
   */
  constructor(props: PluginInterface) {
    this.plugin = props.plugin;
    this.container = props.container || 'body';
    this.target = props.target || 'body';
    this.defaultOptions = props.defaultOptions || {};
    this.instanceOptions = props.instanceOptions || {};
    this.isJQueryPlugin = props.isJQueryPlugin || false;
  }

  /**
   * Initializes the {@link Plugin}.
   *
   * @since 0.1.0
   */
  initialize(container?: string) {
    this.callPlugin(this.getContainer(container));
  }

  /**
   * Call the main {@link Plugin} function against a selector. All arguments are optional,
   * and instance values will be used if one or the other is not provided.
   *
   * @since 0.1.0
   *
   * @param {string} [container] - A valid selector string to execute the plugin within.
   * @param {string} [target] - A valid selector string or element to execute the plugin against.
   * @param {(string|Object)} [options] - Info to provide to the function call.
   * @param {Object} [params] - Additional parameters to provide.
   *
   * @return {void}
   */
  callPlugin(
    container?: string,
    target?: string,
    options?: string | Object,
    params?: Object
  ) {
    container = this.getContainer(container);
    target = target || this.target;
    options = options || this.instanceOptions;

    if (typeof options === 'object') {
      options = this.setOptions(options);
    }

    if (target && this.foundElements(target, container)) {
      // if the plugin requires jQuery, we'll check to make sure
      // jQuery is defined, since this library does not add it.
      if (this.isJQueryPlugin && typeof window.jQuery !== 'undefined') {
        target = window.jQuery(target);
      }

      const args = [].concat([target], [options], params ? [params] : []);
      this.execute(this.plugin(...args));
    }
  }

{'fraction_non_alphanumeric': 0.12485993276772851, 'fraction_numerical': 0.002881383063870658, 'mean_word_length': 3.2216216216216216, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, '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': '6690385', 'n_tokens_mistral': 1781, 'n_tokens_neox': 1740, 'n_words': 611}