Permalink
Browse files

implemented origin() method

  • Loading branch information...
1 parent ae7c16f commit fbc14c2b484173122c3e84d211ee0c0886350a9c @derek-watson committed Feb 10, 2012
Showing with 38 additions and 6 deletions.
  1. +4 −0 spec/javascripts/uri.spec.js
  2. +34 −6 src/uri.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() {
View
@@ -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,

0 comments on commit fbc14c2

Please sign in to comment.