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

Tricky to construct a path with trailing slash #106

Closed
djcsdy opened this Issue Aug 13, 2013 · 5 comments

Comments

Projects
None yet
2 participants
@djcsdy
Contributor

djcsdy commented Aug 13, 2013

Given a URI like this:

URI("http://www.example.org/foo/bar/bat");

it’s difficult to programmatically append a trailing slash.

I thought this might work, but it doesn’t:

URI("http://www.example.org/foo/bar/bat")
    .segment("")
    .toString();
// -> "http://www.example.org/foo/bar/bat" :(

This is annoying when you’re constructing a path using successive calls to .segment() or .segmentCoded():

function apiUri(category, name) {
    var baseUri = URI("http://www.example.org/api/")
    return baseUri.segmentCoded(category)
            .segmentCoded(name)
            .segment("");
}

apiUri("furniture", "hatstand").toString();
// -> "http://www.example.org/api/furniture/hatstand"
// but we wanted "http://www.example.org/api/furniture/hatstand/"
// with trailing slash :(

I’m not sure of the best way to solve this problem, but it seems like segment("") would make sense as a way to add a trailing slash to an existing URI.

After all, it would be consistent with the behaviour of the .segment() getter:

URI("http://www.example.org/foo/bar/bat/").segment();
// -> ["foo", "bar", "bat", ""]
@rodneyrehm

This comment has been minimized.

Show comment
Hide comment
@rodneyrehm

rodneyrehm Aug 13, 2013

Member

Yeah, I'm not sure I'm happy with segment() returning an empty-string element in that array. But for consistency, .segment("") should add an empty element, if the last element isn't empty - agreed. Are you already looking into this, or should I have a go at it?

Member

rodneyrehm commented Aug 13, 2013

Yeah, I'm not sure I'm happy with segment() returning an empty-string element in that array. But for consistency, .segment("") should add an empty element, if the last element isn't empty - agreed. Are you already looking into this, or should I have a go at it?

@djcsdy

This comment has been minimized.

Show comment
Hide comment
@djcsdy

djcsdy Aug 13, 2013

Contributor

No, I won’t have time to do anything about this for a few days at least.

For the time being I’m working around the problem by doing this for the last segment:

.segment("foo/");

which is pretty horrible :)

Contributor

djcsdy commented Aug 13, 2013

No, I won’t have time to do anything about this for a few days at least.

For the time being I’m working around the problem by doing this for the last segment:

.segment("foo/");

which is pretty horrible :)

@rodneyrehm

This comment has been minimized.

Show comment
Hide comment
@rodneyrehm

rodneyrehm Aug 13, 2013

Member

… my eyes!

Member

rodneyrehm commented Aug 13, 2013

… my eyes!

@rodneyrehm

This comment has been minimized.

Show comment
Hide comment
@rodneyrehm

rodneyrehm Aug 13, 2013

Member

if this fixes your problem, I'd release a new version tonight/tomorrow…

Member

rodneyrehm commented Aug 13, 2013

if this fixes your problem, I'd release a new version tonight/tomorrow…

@rodneyrehm rodneyrehm closed this Aug 13, 2013

@djcsdy

This comment has been minimized.

Show comment
Hide comment
@djcsdy

djcsdy Aug 13, 2013

Contributor

Yep, that fixes it. Thanks!

Contributor

djcsdy commented Aug 13, 2013

Yep, that fixes it. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment