Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
implemented origin() method
  • Loading branch information
derek-watson committed Feb 10, 2012
1 parent ae7c16f commit fbc14c2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
4 changes: 4 additions & 0 deletions spec/javascripts/uri.spec.js
Expand Up @@ -113,6 +113,10 @@ describe("Uri", function() {
it('can construct a path and single query kvp', function() { it('can construct a path and single query kvp', function() {
expect(new jsUri("/contacts?name=m").toString()).toEqual('/contacts?name=m'); expect(new jsUri("/contacts?name=m").toString()).toEqual('/contacts?name=m');
}); });

it('returns successfully returns the origin with a scheme, auth, host and port', function() {
expect(new Uri('http://me:here@test.com:81/this/is/a/path').origin()).toEqual('http://me:here@test.com:81');
});
}); });


describe("Manipulation", function() { describe("Manipulation", function() {
Expand Down
40 changes: 34 additions & 6 deletions src/uri.js
Expand Up @@ -115,9 +115,8 @@ var Uri = function (uriString) {
return uriParts.anchor; return uriParts.anchor;
}, },



/* /*
Fluent setters for Uri uri properties Fluent setters for Uri properties
*/ */


setProtocol = function (val) { setProtocol = function (val) {
Expand Down Expand Up @@ -203,9 +202,7 @@ var Uri = function (uriString) {
/* /*
Serialization Serialization
*/ */

scheme = function () {
// toString() stringifies the current state of the uri
toString = function () {


var s = '', var s = '',
is = function (s) { is = function (s) {
Expand All @@ -224,6 +221,22 @@ var Uri = function (uriString) {
} }
} }


return s;
},

/*
Same as Mozilla nsIURI.prePath
+ cf. https://developer.mozilla.org/en/nsIURI
+ */
origin = function () {

var s = '',
is = function (s) {
return (s !== null && s !== '');
};

s += scheme();

if (is(userInfo()) && is(host())) { if (is(userInfo()) && is(host())) {
s += userInfo(); s += userInfo();
if (userInfo().indexOf('@') !== userInfo().length - 1) { if (userInfo().indexOf('@') !== userInfo().length - 1) {
Expand All @@ -238,6 +251,20 @@ var Uri = function (uriString) {
} }
} }


return s;
},


// toString() stringifies the current state of the uri
toString = function () {

var s = '',
is = function (s) {
return (s !== null && s !== '');
};

s += origin();

if (is(path())) { if (is(path())) {
s += path(); s += path();
} else { } else {
Expand Down Expand Up @@ -282,7 +309,8 @@ var Uri = function (uriString) {
path: path, path: path,
query: query, query: query,
anchor: anchor, anchor: anchor,

origin: origin,

setProtocol: setProtocol, setProtocol: setProtocol,
setHasAuthorityPrefix: setHasAuthorityPrefix, setHasAuthorityPrefix: setHasAuthorityPrefix,
setUserInfo: setUserInfo, setUserInfo: setUserInfo,
Expand Down

0 comments on commit fbc14c2

Please sign in to comment.