Giter VIP home page Giter VIP logo

Comments (7)

DylanPiercey avatar DylanPiercey commented on July 24, 2024 1

Also just wanted to say thanks for your quick response - awesome to see and I appreciate your work.

from path-to-regexp.

blakeembrey avatar blakeembrey commented on July 24, 2024

I'm not sure I understand, tokensToRegexp is not for settings keys - it's the reverse, it takes tokens and creates a regexp. keys is just a version of tokens without the literal stings. Can you elaborate if there's something I'm missing?

from path-to-regexp.

blakeembrey avatar blakeembrey commented on July 24, 2024

Or are you suggesting a functionality change where it populates keys from now on? Can you share what you're trying to do?

from path-to-regexp.

DylanPiercey avatar DylanPiercey commented on July 24, 2024

Yeah I had a misunderstanding as to what keys were. I was thinking that tokensToRegexp api should have been tokensToRegexp(path, keys, options) but it makes sense that keys are not required since the returned tokens are more/less those keys anyway.

I think it's a little weird that tokensToRegexp has a different api than all of the other ToRegexp's and it might be nice for it to automatically set a keys array similar to stringToRegexp and others. Ultimately that's up to you though.

Thanks for responding, going to close this for now since I have figured out how to get around this.

from path-to-regexp.

blakeembrey avatar blakeembrey commented on July 24, 2024

I think it's only the one exposed function that returns a regexp with keys attached, and that's the main function - it reuses the other methods depending on input. I could definitely expand it, just would like to understand the usage too 😄 Would you expect keys to be added on the returned function for tokensToFunction also?

from path-to-regexp.

DylanPiercey avatar DylanPiercey commented on July 24, 2024

My usage is related to #87.

I am just making a wrapper around pathToRegexp that works like I explained in that issue.
To do that I am parsing the path into tokens and then checking if the last token has an asterisk property and then I force it to be optional.

Here is my current solution.

/**
 * Small wrapper around path to regexp that treats a splat param "/*" as optional.
 * This makes mounting easier since typically when you do a path like "/test/*" you also want to treat "/test" as valid.
 *
 * @param {String} pathname - the path to convert to a regexp.
 * @param {Array} [keys] - a place to store matched param keys.
 * @param {Object} [opts] - options passed to pathToRegExp.
 * @return {RegExp}
 */
function toReg (pathname, keys, opts) {
  // First parse path into tokens.
  var tokens = parse(pathname)

  // Find the last token (checking for splat params).
  var splat = tokens[tokens.length - 1]

  // Check if the last token is a splat and make it optional.
  if (splat && splat.asterisk) splat.optional = true

  // Convert the tokens to a regexp.
  var re = tokensToRegExp(tokens, opts)

  // Assign keys to from regexp.
  for (var i = 0, len = tokens.length; i < len; i++) {
    if (typeof tokens[i] === 'object') keys.push(tokens[i])
  }
  re.keys = keys

  return re
}

Originally my problem was that I didn't know how to get the keys out of the tokens, but then I looked through the source and realized the tokens are the keys and ended up copying https://github.com/pillarjs/path-to-regexp/blob/master/index.js#L322 into my function.

Does that make sense?

from path-to-regexp.

blakeembrey avatar blakeembrey commented on July 24, 2024

I think so. I just submitted #93 to support populating keys, I don't think anyone would be relying on the fact it currently does not return with keys attached.

As an aside, the splat behaviour may be an appropriate change for a 2.0 release. I'm still not 100% sure about the change, since there's some edge cases, but I'll remember to think about it. I can move to the other issue quickly and leave some thoughts now I'm revisiting it.

from path-to-regexp.

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.