-
Notifications
You must be signed in to change notification settings - Fork 42
Not only parse, also generate #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
hey michael,
exactly! url-pattern checks whether a string matches a pattern and if it matches it extracts the right values for the named segments in the pattern from the string and returns them as an object. the opposite is just string replacement. > var generate = function(string, object) {
for (var key in object) {
string.replace(':' + key, object[key]);
}
return string;
};
> generate('/posts/:start/:count/:whatever', {
start: 1,
count: 100,
whatever: 'whatever'
});
'/posts/1/100/whatever' i am hesitant to put this kind of functionality into the library. by the way: > var postsUrl = function(start, count, whatever) {
return '/posts/' + start + '/' + count + '/' + whatever;
};
// let's make a pattern
> postsUrl(':start', ':count', ':whatever');
'/posts/:start/:count/:whatever'
// let's make an url
> postsUrl(10, 100, 'whatever');
'/posts/1/100/whatever' we use a couple of helpers to keep this very DRY by reusing parts of urls. it works quite well. hope this helps! best, |
@mixtur you can use https://github.com/jonschlinkert/rte for that thing. |
@tunnckoCore yes, for simple examples like the one I stated above rte will work just fine, but when it comes to more complicated cases like |
@mixtur hmm, yea. Can you share me more fixtures, cuz I have something in mind? You can message me at tunnckoCore/messages, if not here. |
thought about this some more. some thoughts on implementing this:
we could handle optional segments by omitting (replacing by the empty string) all static optional parts (like > var pattern = new UrlPattern('/v:major(.:minor)/');
> pattern.generate({major: 1});
'/v1/'
> pattern.generate({major: 1, minor: 2});
'/v1.2/'
> pattern.generate({});
// throws
if a segment name occurs more than once > var pattern = new UrlPattern('/a/:id/b/:id');
> pattern.generate({id: [1, 2]});
'/a/1/b/2'
> pattern.generate({id: 1})
// throws wildcards in patterns would be omitted (replaced by the empty string) by > var pattern = new UrlPattern('/aaa/*');
> pattern.generate({});
'/aaa/'
> pattern.generate({_: 'bbb'});
'/aaa/bbb'
what do you think ? |
erm.. Im confused, lol. maybe I should sleep a bit. 😴 btw, maybe |
yes, |
@tunnckoCore for more fixtures you can use README.md in url-pattern @snd about invariants: yes that feels natural 1.1) for any 2.1) for any Also that implies exposing some way to check if |
in my opinion the way to check if |
this is not trivial. some current thoughts as a reminder to myself: situation: currently the parser/compiler is a state machine that consumes the pattern while immediately building the regex. this is fairly easy and fast. problem: to implement solution: the implementation of this is a simpler and more elegant implementation overall. |
Maybe you shouldn't actually do it all by yourself. There are a few parser generators around. For example: Actually whole AST approach opens up ability to process more complex patterns, something like assert(isEqual(
new UrlPattern('/((\w+:type)-(\d+:id)+:product)').parse('/cellphone-123-234'),
{
product: {
type: 'cellphone'
id: [123, 234]
}
}
)) ...though that is really seems like overlkill for library called |
thank you for the suggestions. since url-pattern is used in several client-side-routers i would also like to to keep dependencies to a minimum. as for more complex patterns: i think it’s best to parse such patterns with regexes. url-pattern plays nice with them: however, i’m always open to change url-pattern if a much better idea for a simple pattern syntax comes along :) |
If this is something you add, I would suggest putting it in a separate file to explicitly require, or another module like |
the new parser (which produces an AST rather than a regex string directly) is almost finished. this is taking its time because i'm busy with other projects. @spicydonuts i'll keep that in mind. don't worry though. i don't think the filesize will change significantly. |
is there any progress? |
here's a quick update: while implementing this feature i rewrote the parser to use parser combinators. this opened up some exiting possibilities. i discovered some ways to significantly improve this library:
i went bit too far and implemented most of it. i stopped because there were some unresolved questions/problems. i plan to release a new version within the next three weeks (i’m on vacation) with the following features:
i’ll continue to explore the ideas above after that. |
this is now published in 0.10.0. |
This looks awesome! Thanks! |
So basically what your library does is extracting some data structure from url based on certain pattern.
Can you please implement the opposite thing. I mean some method to which you feed data structure and get url string in return.
Something like
Such feature would allow (me) to write routers together with History API manipulation in browser quite easily.
The text was updated successfully, but these errors were encountered: