Skip to content

Tricky to construct a path with trailing slash #106

@djcsdy

Description

@djcsdy

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", ""]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions