tiny-regex-route-resolver

    1.0.1 • Public • Published

    tiny-regex-route-resolver

    A super tiny regex route resolver

    Implementation

    the resolver appends / to the end of your pathname if its missing

    var routeResolver = require('tiny-regex-route-resolver')
    var routes = [
      {
        regex: /^((\/404\/)?)$/g,
        someKey: function() {}
      },
      {
        regex: /^((\/hello\/world\/)?)$/g
        anything: 'hello world'
      },
      {
        regex: /^(\/(post)\/([\w\d-_+]{1,})\/)$/g
      }
    ]
    
    var routingLocation = history.location || window.location
    var route = routeResolver(routingLocation.pathname, routes, routes[0]) // routes[0] is our 404 in this case
    

    Returned Result

      {
        route: {
          regex: YOUR_REGEX,
          anything: ELSE_YOU_ADDED
        },
        pathname: THE_PATHNAME_YOU_PARSED_BUT_ALWAYS_ENDING_IN /
      }
    

    Anatomy of a regex route

    //g - basic regex start and end. regex is not a string, there is no need to wrap it in '' or ""

    \/ - escaped /

    \/(posts)\/ - a single section of path

    ([\w\d-_+]{1,}) - a wildcard that supports words (a-Z), digits (0-9), and - _ +

    Putting it all together

    /^(\/(hello)\/(world)\/([\w\d-_+]{1,})\/)$/g = /hello/world/Some3-val_4+ue/

    Install

    npm i tiny-regex-route-resolver

    DownloadsWeekly Downloads

    3

    Version

    1.0.1

    License

    MIT

    Last publish

    Collaborators

    • horrorandtropics