geraintluff / uri-templates

JavaScript utility for RFC 6570: URI Templates
138 stars 21 forks source link

parsing optional path components should keep the same variables bound? #8

Closed jimmymain closed 10 years ago

jimmymain commented 10 years ago

Hi,

Sorry, not sure if this is actually a bug, but it would make my urls match up to MVC really well, perhaps you have some ideas?

Given the following:

new UriTemplate("{+path}/c/capture{/date,id,page}")
     .fromUri('/a/b/c/capture/20140101/1')
Object {path: "/a/b", id: "20140101", page: "1"} // id should be 1, and date should be id

How do I get the 20140101 to be bound to date, with 'id' and 'page' as optional components at the end of the path?

I want:

Object {path: "/a/b", date: "20140101", id: "1"} // page should be undefined.

I basically want the elements bound left to right (as you read) instead of right to left is there any chance this can be done?

The page and id are optional components, so the full url is this:

new UriTemplate("{+path}/c/capture{/date,id,page}")
       .fromUri('/a/b/c/capture/432432432/99/1')
Object {path: "/a/b", date: "432432432", id: "99", page: "1"}
geraintluff commented 10 years ago

Ah, OK - so if there are two values and three variables, then the first ones should be assigned first leaving the third one undefined?

That seems good to me - I'll have a look at it.

geraintluff commented 10 years ago

Turns out I had a small mistake in my code - the original intention was always to fill left-to-right (because that's nice and intuitive) but I wasn't testing for it so a typo slipped through.

Fixed version released as v0.1.4. :)