Skip to content
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

Should segment() take care of leading slash? #236

Closed
orlando opened this issue Jul 29, 2015 · 9 comments
Closed

Should segment() take care of leading slash? #236

orlando opened this issue Jul 29, 2015 · 9 comments
Labels

Comments

@orlando
Copy link
Contributor

orlando commented Jul 29, 2015

I'm using URI.js to append a cdn host to URLs, the problem is that some assets have a leading slash and other don't.

Should segment() method take care of this?

Example

URI('https://google.com').segment('font.ttf').toString() //-> https://google.com/font.ttf
URI('https://google.com').segment('/font.ttf').toString() //-> https://google.com//font.ttf

Result of second example should be like the first one.

Thanks!

@orlando orlando changed the title Should segment() take care of leading slash? Should segment() take care of leading slash? Jul 29, 2015
@rodneyrehm
Copy link
Member

is it possible you're looking for .absoluteTo() instead?

URI('font.ttf').absoluteTo('https://google.com/').toString()
// https://google.com/font.ttf
URI('/font.ttf').absoluteTo('https://google.com/').toString()
// https://google.com/font.ttf
URI('font.ttf').absoluteTo('https://google.com/somewhere/').toString()
// https://google.com/somewhere/font.ttf
URI('/font.ttf').absoluteTo('https://google.com/somewhere/').toString()
// https://google.com/font.ttf

But to answer your question: the .segment() function is nothing but a helper to split and join paths. There also is .segmentCoded() to take care of en/decoding segments. There is no logic along the lines of "is the value to set a segment, or really an entire path". I'm not sure this should be added, as you may very well replace a given segment by foo/bar, which is valid.

Removing leading and trailing slashes would be a convenience thing. Feel free to submit a PR for that :)

@orlando
Copy link
Contributor Author

orlando commented Jul 30, 2015

@rodneyrehm abosoluteTo() works for this use case.

if you think segment() should do this, I can implement it. Do you have any suggestion about the API, or instead sanitize leading/trailing slashes by default?

@rodneyrehm
Copy link
Member

if you think segment() should do this, I can implement it. Do you have any suggestion about the API, or instead sanitize leading/trailing slashes by default?

leading and trailing / make no sense in a segment, so'd trim them by default.

@orlando
Copy link
Contributor Author

orlando commented Jul 30, 2015

Perfect, I'll work on this over the weekend.

Thanks!

@rodneyrehm
Copy link
Member

any progress on this, @orlando?

@orlando
Copy link
Contributor Author

orlando commented Sep 2, 2015

@rodneyrehm actually I made the fix, but totally forgot about the PR. Thanks for the heads up, I'll make the PR between tonight and tomorrow.

@rodneyrehm
Copy link
Member

any progress on the PR, @orlando?

@orlando
Copy link
Contributor Author

orlando commented Sep 29, 2015

@rodneyrehm PR up

@rodneyrehm
Copy link
Member

released in v1.17.0

@orlando orlando closed this as completed Nov 13, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants