Comments (7)
Also just wanted to say thanks for your quick response - awesome to see and I appreciate your work.
from path-to-regexp.
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.
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.
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.
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.
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.
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)
- Publish to NPM HOT 1
- Next dynamic route with spread (...) throws 'Unexpected MODIFIER at 70, expected END' HOT 2
- end option does not work as expected HOT 2
- When parsed to tokens, why are backslashes stripped from pattern? HOT 2
- Invalid regex patterns not properly detected. HOT 2
- Another dumb question: How to run tests? HOT 2
- Erroneous test result. HOT 3
- `?` in optional capture group isn't connected to group in output - named param HOT 11
- SyntaxError: Invalid regular expression: invalid group specifier name HOT 1
- The validate config in options is not work in compile function. HOT 1
- throw new TypeError(`Unexpected ${nextType} at ${index}, expected ${type}`) HOT 3
- How about match an empty array? HOT 2
- Regex for non-negative integers only matches two or more digits HOT 1
- Return the matched path/regex
- Escaping the question mark for a query string throws and error HOT 2
- Checking if a path is a subset of another path HOT 1
- params are being joined without `/` HOT 1
- Issue with converted value used in Next.js rewrite HOT 3
- Contributing named capture groups to pathToRegexp HOT 10
- Expected "id" to be a string error with release 7.0.0 HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from path-to-regexp.