<commit_before>//
// Router module for microbi.js
//
//


/**
 *
 *
 */
exports.getOp = function( requestInfo, api ) {
  var routeParts = getParts( requestInfo.pathname )
  if ( ! routeParts.length ) return null

  var pathNode = getPathNode( routeParts, api )
  if ( ! pathNode.node ) return null

  var apiFunction = pathNode.node[ requestInfo.method ]
  if ( ! apiFunction ) return null

  var streamFlag = pathNode.node[ requestInfo.method + ':stream' ]

  return {
    fn: apiFunction,
    stream: streamFlag,
    params: pathNode.params
  }
}

/**
 * Splits a path and discards empty elements
 *
 * For example for the path:
 *     stuff/items
 * creates the array:
 *     [ 'stuff', 'items' ]
 */
var getParts = function( path ) {
  return path.split( '/' ).filter(function(el) { return el })
}


/**
 * Takes an array of path pieces, and an api object.
 * finds if there is tree of properties in the api object
 * that correspond to the path pieces.
 * For example, for the array path:
 *     [ 'stuff', 'items' ]
 * it searches the api object tree as follows:
 *     api.stuff.items
 * return the value of the last object in the tree,
 * or null if the properties.
 * Properties of the form "$x", will match any path pieces
 * and will also be returned as path parameters.
 * @todo explain better.
 */
var getPathNode = function( paths, api ) {
  var apiNode = api, paramNode, urlParams = []
  console.log('paths', paths )
  for (var i in paths) {
    console.log('path', paths[i], apiNode )
    paramNode = apiNode.$x
    apiNode = apiNode[ paths[i] ]
    if ( ! apiNode && paramNode ) {<commit_msg>Remove console.logs<commit_after>//
// Router module for microbi.js
//
//


/**
 *
 *
 */
exports.getOp = function( requestInfo, api ) {
  var routeParts = getParts( requestInfo.pathname )
  if ( ! routeParts.length ) return null

  var pathNode = getPathNode( routeParts, api )
  if ( ! pathNode.node ) return null

  var apiFunction = pathNode.node[ requestInfo.method ]
  if ( ! apiFunction ) return null

  var streamFlag = pathNode.node[ requestInfo.method + ':stream' ]

  return {
    fn: apiFunction,
    stream: streamFlag,
    params: pathNode.params
  }
}

/**
 * Splits a path and discards empty elements
 *
 * For example for the path:
 *     stuff/items
 * creates the array:
 *     [ 'stuff', 'items' ]
 */
var getParts = function( path ) {
  return path.split( '/' ).filter(function(el) { return el })
}


/**
 * Takes an array of path pieces, and an api object.
 * finds if there is tree of properties in the api object
 * that correspond to the path pieces.
 * For example, for the array path:
 *     [ 'stuff', 'items' ]
 * it searches the api object tree as follows:
 *     api.stuff.items
 * return the value of the last object in the tree,
 * or null if the properties.
 * Properties of the form "$x", will match any path pieces
 * and will also be returned as path parameters.
 * @todo explain better.
 */
var getPathNode = function( paths, api ) {
  var apiNode = api, paramNode, urlParams = []
  for (var i in paths) {
    paramNode = apiNode.$x
    apiNode = apiNode[ paths[i] ]
    if ( ! apiNode && paramNode ) {
{'fraction_non_alphanumeric': 0.11622737376945062, 'fraction_numerical': 0.0, 'mean_word_length': 2.9873417721518987, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3983506', 'n_tokens_mistral': 985, 'n_tokens_neox': 958, 'n_words': 358}