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

Add an origin function #263

Merged
merged 6 commits into from
Nov 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member

Choose a reason for hiding this comment

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

When looking at URN Examples I wonder if the origin of urn:isan:0000-0000-9E59-0000-O-0000-0000-2 would be urn:isan. But since anything but the protocol is considered to be the path, this wouldn't be trivial…

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have no idea on this one. I'll do whatever you think is best, I basically cloned this from the a nearby function.

Copy link
Member

Choose a reason for hiding this comment

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

It's fine, will stick with it for now. We just need to make sure to mention this in docs.html

}

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