<commit_before>/*
 * Copyright (c) 2012-2018, Inversoft Inc., All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific
 * language governing permissions and limitations under the License.
 */
'use strict';

import {PrimeDocument} from "./PrimeDocument";

/**
 * The Utils namespace. This contains utility functions.
 *
 * @namespace Utils
 */
const Utils = {
  spaceRegex: /\s+/,
  typeRegex: /^\[object\s(.*)\]$/,

  /**
   * Binds all of the functions inside the object so that <code>this</code> is the object. This is extremely useful for
   * objects that have functions that will be used as event listeners. Rather than having to manage binding and function
   * references manually you can instead bind all of the functions in the object and then use them directly for event
   * listeners.
   *
   * Here's an example:
   *
   * <pre>
   *   function Foo() {
   *     Utils.bindAll(this);
   *
   *     // This function is bound to this (i.e. this.handleClick = this.handleClick.bind(this)).
   *     PrimeDocument.queryFirst('a').addEventListener('click', this.handleClick);
   *   }
   *
   *   Foo.prototype = {
   *     handleClick: function(event) {
   *       ...
   *     }
   *   };
   * </pre>
   *
   * @param {*} object The object to bind all the functions for.
   */
  bindAll: function(object) {
    for (const property of Utils.getAllPropertyNames(object)) {
      if (property !== 'constructor' && typeof object[property] === 'function' &&
          !object[property].name.startsWith('bound ')) {
        Object.defineProperty(object, property, {value: object[property].bind(object)});
      }
    }
  },

  /**
   * HTML escape a string.
   *
   * @param string The string to escape
   * @returns {string} the escaped string
   */
  escapeHTML: function(string) {
    let div = document.createElement('div');
    div.appendChild(document.createTextNode(string));
    return div.innerHTML;
  },

  /**
   * Returns all of the properties for this object and all of its
   * inherited properties from parent objects.
   *
   * @param object
   * @returns {Array<string>}
   */
  getAllPropertyNames: function(object) {
    let props = new Set();

    do {
      Object.getOwnPropertyNames(object).forEach((prop) => {
        if (!props.has(prop)) {
          props.add(prop);
        }
      })
    } while (object = Object.getPrototypeOf(object));

    return Array.from(props);
  },

  /**
   * Binds all of the functions inside the object so that <code>this</code> is the object. This is extremely useful for
   * objects that have functions that will be used as event listeners. Rather than having to manage binding and function
   * references manually you can instead bind all of the functions in the object and then use them directly for event
   * listeners.
   *
   * Here's an example:
   *
   * <pre>
   *   function Foo() {
   *     Utils.bindAll(this);
   *
   *     // This function is bound to this (i.e. this.handleClick = this.handleClick.bind(this)).
   *     PrimeDocument.queryFirst('a').addEventListener('click', this.handleClick);
   *   }
   *
   *   Foo.prototype = {
   *     handleClick: function(event) {
   *       ...
   *     }
   *   };
   * </pre>
   *
   * @param {*} object The object to bind all the functions for.
   * @param {String} arguments A varargs list of function names to bind.
   */<commit_msg>Remove `Array.from` usage to get it working in IE<commit_after>/*
 * Copyright (c) 2012-2018, Inversoft Inc., All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific
 * language governing permissions and limitations under the License.
 */
'use strict';

import {PrimeDocument} from "./PrimeDocument";

/**
 * The Utils namespace. This contains utility functions.
 *
 * @namespace Utils
 */
const Utils = {
  spaceRegex: /\s+/,
  typeRegex: /^\[object\s(.*)\]$/,

  /**
   * Binds all of the functions inside the object so that <code>this</code> is the object. This is extremely useful for
   * objects that have functions that will be used as event listeners. Rather than having to manage binding and function
   * references manually you can instead bind all of the functions in the object and then use them directly for event
   * listeners.
   *
   * Here's an example:
   *
   * <pre>
   *   function Foo() {
   *     Utils.bindAll(this);
   *
   *     // This function is bound to this (i.e. this.handleClick = this.handleClick.bind(this)).
   *     PrimeDocument.queryFirst('a').addEventListener('click', this.handleClick);
   *   }
   *
   *   Foo.prototype = {
   *     handleClick: function(event) {
   *       ...
   *     }
   *   };
   * </pre>
   *
   * @param {*} object The object to bind all the functions for.
   */
  bindAll: function(object) {
    for (const property of Utils.getAllPropertyNames(object)) {
      if (property !== 'constructor' && typeof object[property] === 'function' &&
          !object[property].name.startsWith('bound ')) {
        Object.defineProperty(object, property, {value: object[property].bind(object)});
      }
    }
  },

  /**
   * HTML escape a string.
   *
   * @param string The string to escape
   * @returns {string} the escaped string
   */
  escapeHTML: function(string) {
    let div = document.createElement('div');
    div.appendChild(document.createTextNode(string));
    return div.innerHTML;
  },

  /**
   * Returns all of the properties for this object and all of its
   * inherited properties from parent objects.
   *
   * @param object
   * @returns {Array<string>}
   */
  getAllPropertyNames: function(object) {
    let props = {};

    do {
      Object.getOwnPropertyNames(object).forEach((prop) => {
        if (!props[prop]) {
          props[prop]=prop;
        }
      })
    } while (object = Object.getPrototypeOf(object));

    return Object.keys(props);
  },

  /**
   * Binds all of the functions inside the object so that <code>this</code> is the object. This is extremely useful for
   * objects that have functions that will be used as event listeners. Rather than having to manage binding and function
   * references manually you can instead bind all of the functions in the object and then use them directly for event
   * listeners.
   *
   * Here's an example:
   *
   * <pre>
   *   function Foo() {
   *     Utils.bindAll(this);
   *
   *     // This function is bound to this (i.e. this.handleClick = this.handleClick.bind(this)).
   *     PrimeDocument.queryFirst('a').addEventListener('click', this.handleClick);
   *   }
   *
   *   Foo.prototype = {
   *     handleClick: function(event) {
   *       ...
   *     }
   *   };
   * </pre>
   *
   * @param {*} object The object to bind all the functions for.
   * @param {String} arguments A varargs list of function names to bind.
   */
{'fraction_non_alphanumeric': 0.11664705113116254, 'fraction_numerical': 0.003138485680659082, 'mean_word_length': 2.7545409916543937, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6137558', 'n_tokens_mistral': 2211, 'n_tokens_neox': 2164, 'n_words': 764}