<commit_before>routes =
  get: []
  post: []
  put: []
  delete: []
  
routeMethods = ["get", "post", "put", "delete"]

define ["jquery", "underscore"], ($, _) ->

  zipObject = (k, v) ->
    arr = _.zip(k, v)
    red = (m, x)->
      m[x[0]] = x[1]
      return m
    _.reduce(arr, red, {})

  merge = _.extend

  # from: https://github.com/senchalabs/connect/blob/master/lib/middleware/router.js

  normalizePath = (path, keys) ->

    buildRegex  = (_, slash, format, key, capture, optional) ->
      keys.push(key)
      slash ?= ''
      return '' +
        (if optional then '' else slash) +
        '(?:' +
        (if optional then slash else '') +
        (format || '') + (capture || '([^/]+?)') + ')' +
        (optional || '')

    path = path.concat('/?')
               .replace(/\/\(/g, '(?:/')
               .replace( /(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?/g, buildRegex)
               .replace(/([\/.])/g, '\\$1')
               .replace(/\*/g, '(.+)')
    return new RegExp('^' + path + '$', 'i')

  getHelper = (url) ->
    helperClassName = url.replace /^[a-zA-Z]|\/[a-zA-Z]/g, (x) -> x.toUpperCase()
    helperClassName = helperClassName.replace(/\//g, "")+ "Helper"
    return window[helperClassName]

  window.onpopstate = (event) ->
    event ?= {}
    state = event.state || {}
    method = state.method || "get"
    params = state.params || {}
    Router[method](document.location.pathname, params)

  matchRoute = (method, url, params, push) ->
    params ?= {}

    for i in [0...routes[method].length]
      obj = routes[method][i]
      matches = url.match obj.regex
      if matches
        merge params, zipObject(obj.keys, matches.slice(1))
        methods =
          url: url
        
        stopRoute = false
        _.each obj.filters.before, (filter) ->
          if !stopRoute && filter.call(methods, params) == false
            stopRoute = true
        if stopRoute
          return

        obj.callback.call methods, params

        _.each obj.filters.after, (filter) ->
          filter(params)

        if push
          $("body").attr("class", url.replace(/\//g, " ").trim())
          window.history.pushState { method: method, params: params }, "", url

  registerRoute = (method, route, filters, callback) ->
    keys = []
    routes[method].push { "regex": normalizePath(route, keys)
                        , "keys": keys
                        , "original": route
                        , "filters": filters
                        , "callback": callback }

  Controller = (base, scope) ->
      
    router = {}
    filters = {before:[], after:[]}
    routeMethods.forEach (method) ->

      router[method] = (route, callback) ->
        if typeof route != "string"
          callback = route
          route = ""
        registerRoute method, base + route, filters, callback
        
    router.before = (filter) ->
      filters.before.push filter

    router.after = (filter) ->
      filters.after.push filter
<commit_msg>removed more cruft<commit_after>routes =
  get: []
  post: []
  put: []
  delete: []
  
routeMethods = ["get", "post", "put", "delete"]

define ["jquery", "underscore"], ($, _) ->

  zipObject = (k, v) ->
    arr = _.zip(k, v)
    red = (m, x)->
      m[x[0]] = x[1]
      return m
    _.reduce(arr, red, {})

  merge = _.extend

  # from: https://github.com/senchalabs/connect/blob/master/lib/middleware/router.js

  normalizePath = (path, keys) ->

    buildRegex  = (_, slash, format, key, capture, optional) ->
      keys.push(key)
      slash ?= ''
      return '' +
        (if optional then '' else slash) +
        '(?:' +
        (if optional then slash else '') +
        (format || '') + (capture || '([^/]+?)') + ')' +
        (optional || '')

    path = path.concat('/?')
               .replace(/\/\(/g, '(?:/')
               .replace( /(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?/g, buildRegex)
               .replace(/([\/.])/g, '\\$1')
               .replace(/\*/g, '(.+)')
    return new RegExp('^' + path + '$', 'i')

  window.onpopstate = (event) ->
    event ?= {}
    state = event.state || {}
    method = state.method || "get"
    params = state.params || {}
    Router[method](document.location.pathname, params)

  matchRoute = (method, url, params, push) ->
    params ?= {}

    for i in [0...routes[method].length]
      obj = routes[method][i]
      matches = url.match obj.regex
      if matches
        merge params, zipObject(obj.keys, matches.slice(1))
        methods =
          url: url
        
        stopRoute = false
        _.each obj.filters.before, (filter) ->
          if !stopRoute && filter.call(methods, params) == false
            stopRoute = true
        if stopRoute
          return

        if push
          $("body").attr("class", url.replace(/\//g, " ").trim())
          window.history.pushState { method: method, params: params }, "", url

        obj.callback.call methods, params

        _.each obj.filters.after, (filter) ->
          filter(params)

  registerRoute = (method, route, filters, callback) ->
    keys = []
    routes[method].push { "regex": normalizePath(route, keys)
                        , "keys": keys
                        , "original": route
                        , "filters": filters
                        , "callback": callback }

  Controller = (base, scope) ->
      
    router = {}
    filters = {before:[], after:[]}
    routeMethods.forEach (method) ->

      router[method] = (route, callback) ->
        if typeof route != "string"
          callback = route
          route = ""
        registerRoute method, base + route, filters, callback
        
    router.before = (filter) ->
      filters.before.push filter

    router.after = (filter) ->
      filters.after.push filter

{'fraction_non_alphanumeric': 0.18555806283631315, 'fraction_numerical': 0.0017358097552508246, 'mean_word_length': 1.851063829787234, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 35, 'https://': 2, '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': '91042', 'n_tokens_mistral': 1803, 'n_tokens_neox': 1746, 'n_words': 396}