Skip to content

Commit

Permalink
fixing issue #39 - segment() won't append properly if path ended with…
Browse files Browse the repository at this point in the history
… "/"
  • Loading branch information
rodneyrehm committed Aug 14, 2012
1 parent 520d0d2 commit 96d7854
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ URI.js is published under the [MIT license](http://www.opensource.org/licenses/m

## Changelog ##

### next release ###

* fixing [`.segment()`](http://medialize.github.com/URI.js/docs.html#accessors-segment)'s append operation ([Issue #39](https://github.com/medialize/URI.js/issues/39)

### 1.7.0 (August 11th 2012) ###

* fixing URI() constructor passing of `base` ([Issue #33](https://github.com/medialize/URI.js/issues/33) [LarryBattle](https://github.com/LarryBattle))
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
"url",
"uri mutation",
"url mutation",
"uri manipulation",
"url manipulation",
"uri template",
"url template",
"unified resource locator",
"unified resource identifier",
"query string",
Expand Down
8 changes: 7 additions & 1 deletion src/URI.js
Original file line number Diff line number Diff line change
Expand Up @@ -1141,7 +1141,13 @@ p.segment = function(segment, v, build) {
if (isArray(v)) {
segments = v;
} else if (v || (typeof v === "string" && v.length)) {
segments.push(v);
if (segments[segments.length -1] === "") {
// empty trailing elements have to be overwritten
// to prefent results such as /foo//bar
segments[segments.length -1] = v;
} else {
segments.push(v);
}
}
} else {
if (v || (typeof v === "string" && v.length)) {
Expand Down
6 changes: 6 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,12 @@ test("segment", function() {
u.segment(1, 'mars');
equal(u.path(), "foo:mars:baz", "segment set 1 URN");
equal(u.toString(), "someurn:foo:mars:baz", "segment set 1 URN");

u = new URI('/foo/');
equal(u.segment().join('||'), "foo||", "segment get array trailing empty");

u.segment('test');
equal(u.path(), "/foo/test", "segment append trailing empty");
});

module("mutating query strings");
Expand Down

2 comments on commit 96d7854

@dotnetchris
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should if (segments[segments.length -1] === "") {
actually be if (segments[segments.length -1] === "/") { ?

@rodneyrehm
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, no. Segments are the tokens between /. if a / is in a segment, it is encoded to %2F.
The path is split by / to get those segments - in turn / will never be part of the resulting array.

Please sign in to comment.