snd / url-pattern

easier than regex string matching patterns for urls and other strings. turn strings into data or data into strings.
MIT License
585 stars 43 forks source link

regex named segments #14

Closed ozmuir closed 9 years ago

ozmuir commented 9 years ago

It'll be a nice little addition to have named regex segments, for ex. new UrlPattern(/^\/user\/(\d+)\//i, ["user_id"])

snd commented 9 years ago

good idea ! i'll implement it.

what do you think about the following edge case behaviour ?

if there are more groups in the regex than there are names in the array that is the second argument: ignore captured values for the remaining superfluous groups.

if there are less groups in the regex than there are names in the array that is the second argument: ignored the remaining superfluous names.

shouldn't break backwards compatibility - which is nice.

ozmuir commented 9 years ago

:thumbsup: however don't you think it'll be more expressive to not just ignore but:

snd commented 9 years ago

i now think we should consider it a user error if the number of groups and names in the array don't match up.

i would like to throw an error in such cases to notify the user of bad usage. better to blow up on that set of user bugs that to let them go unnoticed.

i might use http://stackoverflow.com/a/16046903 to detect the number of groups and be able to check for such errors as soon as possible i.e. on pattern creation and not on match (callsites of .match tend to be far away from pattern creation).

what do you think ?

ozmuir commented 9 years ago

sure, sounds good.

snd commented 9 years ago

this is now published in 0.10.0. click here for the relevant readme section. there are some breaking changes. click to see the changelog entry.