Skip to content

Instantly share code, notes, and snippets.

@streamich
Last active April 25, 2023 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save streamich/4447f7bc2c6b804a0bcda8e01f697fc1 to your computer and use it in GitHub Desktop.
Save streamich/4447f7bc2c6b804a0bcda8e01f697fc1 to your computer and use it in GitHub Desktop.

Requirements

  • Ability to match static routes GET /foo/bar
    • ['GET /foo/bar']
  • Ability to match a single step in a route POST /users/{userId}/edit or POST /users/{userId:*/}edit
    • ['POST /users/', match('userId', '*/'), '/edit']
  • Ability to wildcard the ending of a route GET /static/{filename:*}
    • ['GET /static/', match('filename', '*')]
  • Ability to make the last route step optional GET /users{userId?:/*/}
    • ['GET /users'] and
    • ['GET /users', match('userId', '/*/')]
  • Ability to match strings withing a step GET /coordinates/{lat}-{lng} or GET /coordinates/{lat:*-}{lng:*}
    • ['GET /coordinates', match('lat', '*-'), '-', match('lng', '*/')] and
    • ['GET /coordinates', match('lat', '*-'), '-', match('lng', '*')]
  • Ability to match any request method {} /rpc or {:* }/rpc
    • [match('', '* '), '/rpc']
  • Ability to match specific request methods {method:{GET,POST}} /rpc
    • ['GET /rpc'] and
    • ['POST /rpc']
  • Ability to specify "one-of" and optional ending wildcard GET /{:{user,users}}{?:/*}
    • ['GET /user'] and
    • ['GET /user', match('', '/*')] and
    • ['GET /users'] and
    • ['GET /users', match('', '/*')]
  • Ability to match a file name GET /files/{name}.gif
  • Ability to match file name and extension GET /blobs/{filename}.{extension}
    • Expands to:
      • GET /blobs/{filename:*:.}.{filename:*:/}

Syntax

[<name>][?]:[<pattern>]:[<until>]
@streamich
Copy link
Author

streamich commented Apr 25, 2023

Tree

  • Exact matches:
    • Exactly "GET /foo/bar"GET /foo/bar
    • Exactly "GET /users"GET /users/{:userId?}
    • Exactly "GET /rpc"GET /rpc
    • Exactly "POST /rpc"POST /rpc
  • Prefix matches:
    • Starts with "POST /users/"
      • Parameter matches:
        • Until "/"
          • Exact matches:
            • Exactly "/edit"POST /users/{:userId}/edit
    • Starts with "GET /users/"
      • Parameter matches:
        • Until EOLGET /users/{:userId?}
    • Starts with "GET /static/"
      • Parameter matches:
        • Until EOLGET /static/{...:filename}
    • Starts with "GET /coordinates/"
      • Parameter matches:
        • Until "-"
          • Prefix matches:
            • Starts with "-"
              • Parameter matches:
                • Until "/"GET /coordinates/{:lat}-{:lng}
  • Parameter matches:
    • * until " "
      • Exact matches:
        • Exactly " /rpc"{} /rpc

@streamich
Copy link
Author

streamich commented Apr 25, 2023

Template string syntax

Instead of

POST /users/{userId}/edit

write

path`POST /users/${'userId'}/edit`

Instead of

GET /coordinates/{lat:*-}-{lng:*/}

write

path`GET /coordinates/${'lat:*-'}-${'lng:*/'}`
// or
path`GET /coordinates/${/[^\-]+/}-${/[^/]+/}`

@streamich
Copy link
Author

streamich commented Apr 25, 2023

Codegen

const router = eval((function(str) {
  var len = str.length;
    switch (len) {
      case 12:
        switch (str) {
           case "GET /foo/bar": return m1;
        }
    }
    if (str.startsWith("POST /users")) {
      str = str.slice("POST /users".length);
      var i1 = str.indexOf("/");
      if (i1 === -1) return undefined;
      var s2 = str.slice(i1);
      if (str === '/edit') {
        m2.params = [str.slice(0, i1)];
        return m2;
      }
    }

  return undefined;
})());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment