<commit_before>import R from 'ramda';
import Promise from 'bluebird';
import React from 'react';
import Immutable from 'immutable';
import localStorage from 'js-util/lib/local-storage';
import bdd from './bdd';
import apiConsole from './api-console';
import GettingStarted from '../components/docs/GettingStarted';

const LOG_LIST = Symbol('log-list');
const COMPONENT = Symbol('component');


/**
 * The API used internally by the UIHarness components.
 */
class Api {
  constructor() {
    this.current = Immutable.Map();
    this[LOG_LIST] = Immutable.List();
    this.loadInvokeCount = 0;
  }


  /**
   * Initializes the UIHarness environment.
   * @return {Promise}.
   */
  init() {
    return new Promise((resolve) => {
      // Put state into global namespace.
      bdd.register();
      global.UIHarness = global.uih = apiConsole;

      // Ensure the last loaded suite is set as the current state.
      const suite = this.lastSelectedSuite();
      if (suite) {
        this.loadSuite(this.lastSelectedSuite(), { storeAsLastSuite: false });
      }

      // Show 'getting started' if empty.
      if (Object.keys(bdd.suites).length === 0) {
        this.setCurrent({
          header: '## Getting Started',
          hr: true,
          scroll: 'y',
          width: '100%',
        });
        this.loadComponent(GettingStarted);
      }

      // Done.
      resolve({});
    });
  }



  /**
   * Resets the internal API.
   * @param {boolean} hard: Flag indicating if all state from local-storage
   *                        should be cleared away, or just current selection state.
   */
  reset({ hard = true } = {}) {
    if (hard) {
      this.clearLocalStorage();
    } else {
      this.clearLocalStorage('lastInvokedSpec:');
    }
    this.lastSelectedSuite(null);
    this.setCurrent(null);
    this.component(null);
    return this;
  }


  /**
   * Removes all ui-harness values stored in local-storage.
   */
  clearLocalStorage(startsWith = null) {
    localStorage.keys().forEach(key => {
      let match = 'ui-harness:';
      if (startsWith) { match += startsWith; }
      if (key.startsWith(match)) {
        localStorage.prop(key, null); // Remove.
      }
    });
  }


  /**
   * Gets or sets the current component instance.
   * Pass {null} to clear.
   */
  component(value) {
    // WRITE.
    if (value !== undefined) {
      if (value === null) {
        // Unload component.
        delete this[COMPONENT];
        delete apiConsole.component;
        this.setCurrent({
          componentType: undefined,
          componentProps: undefined,
          componentChildren: undefined,
          component: undefined,
        });
      } else {
        // Store component instance.
        this[COMPONENT] = value;
        apiConsole.component = value;
        if (this.current.get('component') !== value) {
          // NB: Preform instance comparison before updating the
          //     current state to prevent render loop.
          this.setCurrent({ component: value });
        }<commit_msg>Minor spelling mistake in api-internal.js<commit_after>import R from 'ramda';
import Promise from 'bluebird';
import React from 'react';
import Immutable from 'immutable';
import localStorage from 'js-util/lib/local-storage';
import bdd from './bdd';
import apiConsole from './api-console';
import GettingStarted from '../components/docs/GettingStarted';

const LOG_LIST = Symbol('log-list');
const COMPONENT = Symbol('component');


/**
 * The API used internally by the UIHarness components.
 */
class Api {
  constructor() {
    this.current = Immutable.Map();
    this[LOG_LIST] = Immutable.List();
    this.loadInvokeCount = 0;
  }


  /**
   * Initializes the UIHarness environment.
   * @return {Promise}.
   */
  init() {
    return new Promise((resolve) => {
      // Put state into global namespace.
      bdd.register();
      global.UIHarness = global.uih = apiConsole;

      // Ensure the last loaded suite is set as the current state.
      const suite = this.lastSelectedSuite();
      if (suite) {
        this.loadSuite(this.lastSelectedSuite(), { storeAsLastSuite: false });
      }

      // Show 'getting started' if empty.
      if (Object.keys(bdd.suites).length === 0) {
        this.setCurrent({
          header: '## Getting Started',
          hr: true,
          scroll: 'y',
          width: '100%',
        });
        this.loadComponent(GettingStarted);
      }

      // Done.
      resolve({});
    });
  }



  /**
   * Resets the internal API.
   * @param {boolean} hard: Flag indicating if all state from local-storage
   *                        should be cleared away, or just current selection state.
   */
  reset({ hard = true } = {}) {
    if (hard) {
      this.clearLocalStorage();
    } else {
      this.clearLocalStorage('lastInvokedSpec:');
    }
    this.lastSelectedSuite(null);
    this.setCurrent(null);
    this.component(null);
    return this;
  }


  /**
   * Removes all ui-harness values stored in local-storage.
   */
  clearLocalStorage(startsWith = null) {
    localStorage.keys().forEach(key => {
      let match = 'ui-harness:';
      if (startsWith) { match += startsWith; }
      if (key.startsWith(match)) {
        localStorage.prop(key, null); // Remove.
      }
    });
  }


  /**
   * Gets or sets the current component instance.
   * Pass {null} to clear.
   */
  component(value) {
    // WRITE.
    if (value !== undefined) {
      if (value === null) {
        // Unload component.
        delete this[COMPONENT];
        delete apiConsole.component;
        this.setCurrent({
          componentType: undefined,
          componentProps: undefined,
          componentChildren: undefined,
          component: undefined,
        });
      } else {
        // Store component instance.
        this[COMPONENT] = value;
        apiConsole.component = value;
        if (this.current.get('component') !== value) {
          // NB: Perform instance comparison before updating the
          //     current state to prevent render loop.
          this.setCurrent({ component: value });
        }
{'fraction_non_alphanumeric': 0.1288102261553589, 'fraction_numerical': 0.0016388069485414618, 'mean_word_length': 2.2411046202867766, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '5186817', 'n_tokens_mistral': 1761, 'n_tokens_neox': 1724, 'n_words': 470}