GithubHelp home page GithubHelp logo

Comments (4)

ColinFay avatar ColinFay commented on June 8, 2024 1

Hey @abelborges,

Thanks a lot for this! Awesome.

I love the approach of returning a list with a function and all, smart.
Let me take a closer look into that and see how it could work inside brochure 🤔

from brochure.

ColinFay avatar ColinFay commented on June 8, 2024

Idea:

  • Add a page as /this/{id}/
  • is turned in the name to /this/([^/])/
  • is extracted by handlers and UI and server

How does express.JS does that under the hood? I feel like I'm missing something.

Basically the idea is to define profile/{id} inside the page() function, then have profile/colin and profile/matilda both root to that page, and the id is available to the ui & server.

from brochure.

abelborges avatar abelborges commented on June 8, 2024

Hey, Colin! Great work here with this module.

On this issue, let me know what you think about the direction below.

This first part would be called once, as part of a "compilation" step:

compile_route <- function(route) {
  stopifnot(is.character(route) && length(route) == 1)

  params <- stringr::str_match_all(route, "\\{(\\w+)\\}")[[1]][,2]
  extractor <- stringr::str_replace_all(route, "\\{\\w+\\}", "([^//]+)")[[1]]

  list(
    template = route,
    parse = function(path) {
      parsed <- stringr::str_match_all(path, extractor)[[1]]
      if (!nrow(parsed)) return(NULL)
      setNames(parsed[1,-1], params)
    }
  )
}

route <- compile_route("/prefix/{arg1}/{arg2}-argsuffix/suffix")

And then in request time:

# NULL
route$parse("/blabla")
route$parse("/prefix")
route$parse("/prefix/1")
route$parse("/prefix/1/2-argsuffix")
route$parse("/prefix/1/almost/there-argsuffix/suffix")

# named vectors
route$parse("/prefix/1/2-argsuffix/suffix")
route$parse("/prefix/first/second with spaces-argsuffix/suffix")

Some timings, just for reference (8GB RAM, 2.7 GHz i7 CPU):

Unit: milliseconds
                                                             expr     min        lq
 route$parse("/prefix/first/second with spaces-argsuffix/suffix") 0.01958 0.0199325
        mean    median        uq      max neval
 0.020986522 0.0200675 0.0202895 0.117091  1000

Maybe this is missing some more validation in order to become user facing. I didn't test more weird inputs

from brochure.

ColinFay avatar ColinFay commented on June 8, 2024

Ok back to this.

Internally, express uses https://www.npmjs.com/package/path-to-regexp, so maybe we should have something in the same spirit, that turns path into regex, then does a grepl on the route table 🤔

from brochure.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.