<commit_before>import pathToRegexp from 'path-to-regexp';
import { version } from '../package.json';
import { cleanUrl, parsePath } from './utils';

/**
 * Barba router
 *
 * @namespace @barba/router
 * @type {object}
 */
export const router = {
  /**
   * Version
   *
   * @memberof @barba/router
   * @type {string}
   */
  version,

  /**
   * Origin for cleaning URLs
   *
   * @memberof @barba/router
   * @type {string}
   * @private
   */
  _origin: window.location.origin,

  /**
   * List of route names
   *
   * @memberof @barba/router
   * @type {array}
   * @private
   */
  _routeNames: [],

  /**
   * List of routes by name
   *
   * @memberof @barba/router
   * @type {object}
   * @private
   */
  _routesByName: {},

  /**
   * Plugin installation
   *
   * @memberof @barba/router
   * @param {barba} barba barba instance
   * @param {array} [routes = []] routes
   * @returns {undefined}
   */
  install(barba, { routes = [] } = {}) {
    this.barba = barba;

    routes.forEach(route => {
      const { name, path } = route;
      const regex = pathToRegexp(path, []);

      this._routeNames.push(name);

      if (this._routesByName[name]) {
        console.warn(`Duplicated route name [${name}]`);
      } else {
        this._routesByName[name] = {
          path,
          regex,
        };
      }
    });

    // Add property to "pageSchema" (current, next)
    barba.pageSchema.route = undefined;
    // Register hooks
    barba.hooks.go(this._resolveRoutes, this);
    barba.hooks.refresh(this._resolveRoutes, this);
  },

  /**
   * Plugin initialisation
   *
   * @memberof @barba/router
   * @returns {undefined}
   */
  init() {
    // Wait for store initialization then add new rule for routes
    this.barba.store.add('rule', {
      position: 1,
      value: {
        name: 'route',
        type: 'strings',
      },
    });
  },

  /**
   * Plugin reset
   *
   * @memberof @barba/router
   * @returns {undefined}
   */
  // DEV
  // destroy() {
  //   this._routeNames = [];
  //   this._routesByName = {};
  // },

  /**
   * Resolve URL to route name
   *
   * @memberof @barba/router
   * @param {string} url URL to resolve
   * @returns {string|null} route name
   * @private
   */
  _resolve(url) {
    const fullPath = cleanUrl(url, this._origin);
    const { path } = parsePath(fullPath);

    for (let i = 0, l = this._routeNames.length; i < l; i++) {
      const name = this._routeNames[i];
      const { regex } = this._routesByName[name];

      if (path.match(regex)) {
        return name;
      }
    }

    return null;
  },

  /**
   * Hooks: do, refresh
   *
   * @memberof @barba/router
   * @param {object} data transition data
   * @param {object} data.current current page
   * @param {object} data.next next page
   * @returns {undefined}
   * @private
   */
  _resolveRoutes(data) {
    const { current, next } = data;

    current.route = current.url ? this._resolve(current.url) : undefined;
    next.route = next.url ? this._resolve(next.url) : undefined;
  },
};

export default router;
<commit_msg>refactor(router): :ok_hand: move hooks into init<commit_after>import pathToRegexp from 'path-to-regexp';
import { version } from '../package.json';
import { cleanUrl, parsePath } from './utils';

/**
 * Barba router
 *
 * @namespace @barba/router
 * @type {object}
 */
export const router = {
  /**
   * Version
   *
   * @memberof @barba/router
   * @type {string}
   */
  version,

  /**
   * Origin for cleaning URLs
   *
   * @memberof @barba/router
   * @type {string}
   * @private
   */
  _origin: window.location.origin,

  /**
   * List of route names
   *
   * @memberof @barba/router
   * @type {array}
   * @private
   */
  _routeNames: [],

  /**
   * List of routes by name
   *
   * @memberof @barba/router
   * @type {object}
   * @private
   */
  _routesByName: {},

  /**
   * Plugin installation
   *
   * @memberof @barba/router
   * @param {barba} barba barba instance
   * @param {array} [routes = []] routes
   * @returns {undefined}
   */
  install(barba, { routes = [] } = {}) {
    this.barba = barba;

    routes.forEach(route => {
      const { name, path } = route;
      const regex = pathToRegexp(path, []);

      this._routeNames.push(name);

      if (this._routesByName[name]) {
        console.warn(`Duplicated route name [${name}]`);
      } else {
        this._routesByName[name] = {
          path,
          regex,
        };
      }
    });

    // Add property to "pageSchema" (current, next)
    barba.pageSchema.route = undefined;
  },

  /**
   * Plugin initialisation
   *
   * @memberof @barba/router
   * @returns {undefined}
   */
  init() {
    // Wait for store initialization then add new rule for routes
    this.barba.store.add('rule', {
      position: 1,
      value: {
        name: 'route',
        type: 'strings',
      },
    });
    // Register hooks
    this.barba.hooks.go(this._resolveRoutes, this);
    this.barba.hooks.refresh(this._resolveRoutes, this);
  },

  /**
   * Plugin reset
   *
   * @memberof @barba/router
   * @returns {undefined}
   */
  // DEV
  // destroy() {
  //   this._routeNames = [];
  //   this._routesByName = {};
  // },

  /**
   * Resolve URL to route name
   *
   * @memberof @barba/router
   * @param {string} url URL to resolve
   * @returns {string|null} route name
   * @private
   */
  _resolve(url) {
    const fullPath = cleanUrl(url, this._origin);
    const { path } = parsePath(fullPath);

    for (let i = 0, l = this._routeNames.length; i < l; i++) {
      const name = this._routeNames[i];
      const { regex } = this._routesByName[name];

      if (path.match(regex)) {
        return name;
      }
    }

    return null;
  },

  /**
   * Hooks: do, refresh
   *
   * @memberof @barba/router
   * @param {object} data transition data
   * @param {object} data.current current page
   * @param {object} data.next next page
   * @returns {undefined}
   * @private
   */
  _resolveRoutes(data) {
    const { current, next } = data;

    current.route = current.url ? this._resolve(current.url) : undefined;
    next.route = next.url ? this._resolve(next.url) : undefined;
  },
};

export default router;

{'fraction_non_alphanumeric': 0.1544557658094776, 'fraction_numerical': 0.0006469351447517387, 'mean_word_length': 2.2444910807974816, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4713185', 'n_tokens_mistral': 2100, 'n_tokens_neox': 2085, 'n_words': 471}