From fbc14c2b484173122c3e84d211ee0c0886350a9c Mon Sep 17 00:00:00 2001 From: Derek Watson Date: Thu, 9 Feb 2012 23:51:49 -0500 Subject: [PATCH] implemented origin() method --- spec/javascripts/uri.spec.js | 4 ++++ src/uri.js | 40 ++++++++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/spec/javascripts/uri.spec.js b/spec/javascripts/uri.spec.js index c66796b..ea0f193 100644 --- a/spec/javascripts/uri.spec.js +++ b/spec/javascripts/uri.spec.js @@ -113,6 +113,10 @@ describe("Uri", function() { it('can construct a path and single query kvp', function() { 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() { diff --git a/src/uri.js b/src/uri.js index 0a359e0..ab40177 100644 --- a/src/uri.js +++ b/src/uri.js @@ -115,9 +115,8 @@ var Uri = function (uriString) { return uriParts.anchor; }, - /* - Fluent setters for Uri uri properties + Fluent setters for Uri properties */ setProtocol = function (val) { @@ -203,9 +202,7 @@ var Uri = function (uriString) { /* Serialization */ - - // toString() stringifies the current state of the uri - toString = function () { + scheme = function () { var s = '', is = function (s) { @@ -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())) { s += userInfo(); if (userInfo().indexOf('@') !== userInfo().length - 1) { @@ -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())) { s += path(); } else { @@ -282,7 +309,8 @@ var Uri = function (uriString) { path: path, query: query, anchor: anchor, - + origin: origin, + setProtocol: setProtocol, setHasAuthorityPrefix: setHasAuthorityPrefix, setUserInfo: setUserInfo,