<commit_before>'use strict';


const pathToRegExp = require('path-to-regexp');

const { assignObject } = require('../../../utilities');


class RoutesManager {

  constructor({ routes = [] }) {
    this._routes = [];
    for (const route of routes) {
      this.add(route);
    }
  }

  add({ path, name, goal }) {
    const regexp = pathToRegExp(path);
    const variables = regexp.keys.map(({ name }) => name);
    const goals = goal && !(goal instanceof Array) ? [goal] : goal;

    this._routes.push({ path, name, regexp, variables, goals });
  }

  // Retrieves correct route, according to path
  find({ path, goal }) {
    // Check path and goals
    return this._routes.find(({ regexp, goals }) => {
      return regexp.test(path) && (!goals || goals.includes(goal));
    });
  }

  // Retrieves path variables, e.g. /path/:id
  getPathVars({ path, route: { regexp, variables } }) {
    return regexp
      .exec(path)
      // Removes first value, which is the full path
      .slice(1)
      // Adds the name of the variable to the value
      // Will be an incrementing index e.g. for /path/* or /path/(maybe)?/
      .map((value, index) => {
        const key = variables[index];
        return { [key]: value };
      })
      .reduce(assignObject, {});
  }

}


module.exports = {
  RoutesManager,
};
<commit_msg>Transtype URL variables<commit_after>'use strict';


const pathToRegExp = require('path-to-regexp');

const { transtype, assignObject } = require('../../../utilities');


class RoutesManager {

  constructor({ routes = [] }) {
    this._routes = [];
    for (const route of routes) {
      this.add(route);
    }
  }

  add({ path, name, goal }) {
    const regexp = pathToRegExp(path);
    const variables = regexp.keys.map(({ name }) => name);
    const goals = goal && !(goal instanceof Array) ? [goal] : goal;

    this._routes.push({ path, name, regexp, variables, goals });
  }

  // Retrieves correct route, according to path
  find({ path, goal }) {
    // Check path and goals
    return this._routes.find(({ regexp, goals }) => {
      return regexp.test(path) && (!goals || goals.includes(goal));
    });
  }

  // Retrieves path variables, e.g. /path/:id
  getPathVars({ path, route: { regexp, variables } }) {
    return regexp
      .exec(path)
      // Removes first value, which is the full path
      .slice(1)
      // Adds the name of the variable to the value
      // Will be an incrementing index e.g. for /path/* or /path/(maybe)?/
      .map((value, index) => {
        const key = variables[index];
        const transtypedValue = transtype(value);
        return { [key]: transtypedValue };
      })
      .reduce(assignObject, {});
  }

}


module.exports = {
  RoutesManager,
};

{'fraction_non_alphanumeric': 0.1606293450420783, 'fraction_numerical': 0.0007317965605561653, 'mean_word_length': 2.5322997416020674, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '922066', 'n_tokens_mistral': 876, 'n_tokens_neox': 866, 'n_words': 242}