Skip to content

Commit

Permalink
Merge pull request #263 from justinmchase/feature/origin
Browse files Browse the repository at this point in the history
feature(origin) adding origin() accessor
  • Loading branch information
rodneyrehm committed Nov 12, 2015
2 parents c93b3e1 + 9dfe322 commit c6c7013
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 8 deletions.
17 changes: 10 additions & 7 deletions about-uris.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,18 @@ <h2 id="components">Components of an URI</h2>
<h3 id="components-url">Components of an <abbr title="Uniform Resource Locator">URL</abbr> in URI.js</h3>

<pre class="ascii-art">
<a href="docs.html#accessors-authority">authority</a>
<span class="line"> __________|_________
/ \
<a href="docs.html#accessors-origin">origin</a>
<span class="line"> __________|__________
/ \
</span> <a href="docs.html#accessors-authority">authority</a>
<span class="line"> | __________|_________
| / \
</span> <a href="docs.html#accessors-userinfo">userinfo</a> <a href="docs.html#accessors-host">host</a> <a href="docs.html#accessors-resource">resource</a>
<span class="line"> __|___ ___|___ __________|___________
/ \ / \ / \
<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> &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 class="line"> | __|___ __|__ ______|______ | __________|_________ ____|____ |
| / \ / \ / \ / \ / \ / \ / \
</span> foo://username:password@www.example.com:123/hello/world/there.html?name=ferret#foo
<span class="line"> \_/ \ / \ \ / \__________/ \ \__/
| | \ | | \ |
Expand Down
15 changes: 15 additions & 0 deletions docs.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ <h1><a href="https://github.com/medialize/URI.js">URI.js</a></h1>
<li><a href="#accessors-host">host()</a></li>
<li><a href="#accessors-userinfo">userinfo()</a></li>
<li><a href="#accessors-authority">authority()</a></li>
<li><a href="#accessors-origin">origin()</a></li>
<li><a href="#accessors-subdomain">subdomain()</a></li>
<li><a href="#accessors-domain">domain()</a></li>
<li><a href="#accessors-tld">tld()</a></li>
Expand Down Expand Up @@ -352,6 +353,20 @@ <h3 id="accessors-authority">authority()</h3>
<p class="note">.authority() will reset any of username, password and port if they're not specified.</p>
<p class="note">Throws a <code>TypeError</code> if <code>path</code> is part of the input</p>

<h3 id="accessors-origin">origin()</h3>
<p>Origin is comprised of the scheme and authority.</p>
<pre class="prettyprint lang-js">var uri = new URI("http://example.com/foo.html?q=hello");
// get origin
uri.origin(); // returns string "http://example.com"
// set origin
uri.origin('https://other.org'); // returns URI instance for chaining

// the URI will now have the string representation of:
// "https://other.org/foo.html?q=hello"</pre>
<p class="note">.origin() will reset the entire authority, including username, password and port if not specified in the new origin.</p>
<p class="note">.origin() will be empty if there is no authority.</p>
<p class="note">.origin() will be the same as .authority() (e.g. "example.org") if there is no scheme available.</p>

<h3 id="accessors-domain">domain()</h3>
<p>.domain() is a convenience method that returns <code>example.org</code> from the hostname <code>www.example.org</code>.</p>
<pre class="prettyprint lang-js">var uri = new URI("http://example.org/foo/hello.html");
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"main": "./src/URI",
"homepage": "http://medialize.github.io/URI.js/",
"contributors": [
"Francois-Guillaume Ribreau <npm@fgribreau.com> (http://fgribreau.com)"
"Francois-Guillaume Ribreau <npm@fgribreau.com> (http://fgribreau.com)",
"Justin Chase <justin.m.chase@gmail.com> (http://justinmchase.com)"
],
"files": [
"src/URI.js",
Expand Down
21 changes: 21 additions & 0 deletions src/URI.js
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,27 @@
};

// compound accessors
p.origin = function(v, build) {
var parts;

if (this._parts.urn) {
return v === undefined ? '' : this;
}

if (v === undefined) {
var protocol = this.protocol();
var authority = this.authority();
if (!authority) return '';
return (protocol ? protocol + '://' : '') + this.authority();
} else {
var origin = URI(v);
this
.protocol(origin.protocol())
.authority(origin.authority())
.build(!build);
return this;
}
};
p.host = function(v, build) {
if (this._parts.urn) {
return v === undefined ? '' : this;
Expand Down
8 changes: 8 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,14 @@
u.host('foo\\bar.com');
}, TypeError, 'Failing backslash detection in host');
});
test('origin', function () {
var u = new URI('http://foo.bar/foo.html');
equal(u.origin(), 'http://foo.bar', 'invalid origin');

u.origin('http://bar.foo/bar.html');
equal(u.origin(), 'http://bar.foo', 'origin didnt change');
equal(u+'', 'http://bar.foo/foo.html', 'origin path changed');
});
test('authority', function() {
var u = new URI('http://foo.bar/foo.html');

Expand Down
Loading

0 comments on commit c6c7013

Please sign in to comment.