Skip to content

Commit

Permalink
adding segment() accessor - Issue #34
Browse files Browse the repository at this point in the history
  • Loading branch information
rodneyrehm committed Aug 7, 2012
1 parent e96df35 commit 612f2b9
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ URI.js is published under the [MIT license](http://www.opensource.org/licenses/m

## Changelog ##

### upcomming (work in progress...) ###

* fixing URI() constructor passing of `base` ([Issue #33](https://github.com/medialize/URI.js/issues/33) [LarryBattle](https://github.com/LarryBattle))
* adding `segment()` accessor ([Issue #34](https://github.com/medialize/URI.js/issues/34))

### 1.6.3 (June 24th 2012) ###

* fixing `.absoluteTo()` to join two relative paths properly ([Issue #29](https://github.com/medialize/URI.js/issues/29))
Expand Down
4 changes: 2 additions & 2 deletions about-uris.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ <h3>Components of an <abbr title="Uniform Resource Locator">URL</abbr> in URI.js
</span> <a href="docs.html#accessors-userinfo">userinfo</a> <a href="docs.html#accessors-host">host</a>
<span class="line"> __|___ ___|___
/ \ / \
</span> <a href="docs.html#accessors-username">username</a> <a href="docs.html#accessors-password">password</a> <a href="docs.html#accessors-hostname">hostname</a> <a href="docs.html#accessors-port">port</a> <a href="docs.html#accessors-pathname">path</a> <a href="docs.html#accessors-search">query</a> <a href="docs.html#accessors-hash">fragment</a>
</span> <a href="docs.html#accessors-username">username</a> <a href="docs.html#accessors-password">password</a> <a href="docs.html#accessors-hostname">hostname</a> <a href="docs.html#accessors-port">port</a> <a href="docs.html#accessors-pathname">path</a> &amp; <a href="docs.html#accessors-segment">segment</a> <a href="docs.html#accessors-search">query</a> <a href="docs.html#accessors-hash">fragment</a>
<span class="line"> __|___ __|__ ______|______ | __________|_________ ____|____ |
/ \ / \ / \ / \ / \ / \ / \
</span> foo://username:password@www.example.com:123/hello/world/there.html?name=ferret#foo
Expand Down Expand Up @@ -103,7 +103,7 @@ <h3>Components of an <abbr title="Uniform Resource Name">URN</abbr> in URI.js</h
urn:example:animal:ferret:nose?name=ferret#foo
<span class="line"> \ / \________________________/ \_________/ \ /
| | | |
</span> <a href="docs.html#accessors-protocol">scheme</a> <a href="docs.html#accessors-pathname">path</a> <a href="docs.html#accessors-search">query</a> <a href="docs.html#accessors-hash">fragment</a>
</span> <a href="docs.html#accessors-protocol">scheme</a> <a href="docs.html#accessors-pathname">path</a> &amp; <a href="docs.html#accessors-segment">segment</a> <a href="docs.html#accessors-search">query</a> <a href="docs.html#accessors-hash">fragment</a>
</pre>

<p>While <a href="http://tools.ietf.org/html/rfc3986">RFC 3986</a> does not define URNs having a query or fragment component, URI.js enables these accessors for convenience.</p>
Expand Down
21 changes: 21 additions & 0 deletions docs.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ <h1><a href="https://github.com/medialize/URI.js">URI.js</a></h1>
<li><a href="#accessors-directory">directory()</a></li>
<li><a href="#accessors-filename">filename()</a></li>
<li><a href="#accessors-suffix">suffix()</a></li>
<li><a href="#accessors-segment">segment()</a></li>

<li><a href="#accessors-search">search(), query()</a></li>
<li><a href="#accessors-hash">hash(), fragment()</a></li>

Expand Down Expand Up @@ -378,6 +380,25 @@ <h3 id="accessors-suffix">suffix()</h3>
// will decode for you
uri.suffix(true) === "würgh";</pre>

<h3 id="accessors-segment">segment()</h3>
<p>.segment() allows convenient access to directory levels / URN segments within the path</p>
<pre class="prettyprint lang-js">var uri = new URI("http://example.org/foo/hello.html");
// get segments
uri.segment(); // returns array ["foo", "hello.html"]
// set segments
uri.segment(["foo", "foobar.html"]); // -> http://example.org/foo/foobar.html

// get specific level
uri.segment(0); // returns "foo"
// set specific level
uri.segment(0, "bar"); // -> http://example.org/bar/foobar.html
// remove specific level
uri.segment(0, ""); // -> http://example.org/foobar.html

// append level
uri.segment("appendthis"); // -> http://example.org/foobar.html/appendthis</pre>


<h3 id="accessors-search">search(), query()</h3>
<pre class="prettyprint lang-js">var uri = new URI("http://example.org/foo/hello.html?foo=bar&amp;bar=baz");
// get search
Expand Down
44 changes: 44 additions & 0 deletions src/URI.js
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,50 @@ p.suffix = function(v, build) {
return this;
}
};
p.segment = function(segment, v, build) {
var separator = this._parts.urn ? ':' : '/',
path = this.path(),
absolute = path.substring(0, 1) === '/',
segments = path.split(separator);

if (typeof segment !== 'number') {
build = v;
v = segment;
segment = undefined;
}

if (segment !== undefined && typeof segment !== 'number') {
throw new Error("Bad segment '" + segment + "', must be 0-based integer");
}

if (absolute) {
segments.shift();
}

if (v === undefined) {
return segment === undefined
? segments
: segments[segment];
} else if (segment === null || segments[segment] === undefined) {
if (isArray(v)) {
segments = v;
} else if (v || (typeof v === "string" && v.length)) {
segments.push(v);
}
} else {
if (v || (typeof v === "string" && v.length)) {
segments[segment] = v;
} else {
segments.splice(segment, 1);
}
}

if (absolute) {
segments.unshift("");
}

return this.path(segments.join(separator), build);
};

// mutating query string
var q = p.query;
Expand Down
33 changes: 33 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,39 @@ test("suffix", function() {
equal(u.suffix(), 'c%C3%B6rt', "suffix encoded"); // suffix is expected to be alnum!
equal(u.suffix(true), 'cört', "suffix decoded"); // suffix is expected to be alnum!
});
test("segment", function() {
var u = new URI("http://www.example.org/some/directory/foo.html"),
s = u.segment();

equal(s.join('||'), "some||directory||foo.html", "segment get array");

u.segment(['hello', 'world', 'foo.html']);
equal(u.path(), "/hello/world/foo.html", "segment set array");

equal(u.segment(0), "hello", "segment get 0");
equal(u.segment(2), "foo.html", "segment get 2");
equal(u.segment(3), undefined, "segment get 3");

u.segment(0, "goodbye");
equal(u.path(), "/goodbye/world/foo.html", "segment set 0");
u.segment(2, "bar.html");
equal(u.path(), "/goodbye/world/bar.html", "segment set 2");
u.segment(3, "zupp");
equal(u.path(), "/goodbye/world/bar.html/zupp", "segment set 3");
u.segment("zapp");
equal(u.path(), "/goodbye/world/bar.html/zupp/zapp", "segment append");

u.segment(3, "");
equal(u.path(), "/goodbye/world/bar.html/zapp", "segment del 3 ''");
u.segment(3, null);
equal(u.path(), "/goodbye/world/bar.html", "segment del 3 null");

u = new URI("someurn:foo:bar:baz"),
equal(u.segment().join('||'), "foo||bar||baz", "segment get array URN");
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");
});

module("mutating query strings");
test("mutating object", function() {
Expand Down

0 comments on commit 612f2b9

Please sign in to comment.