Skip to content

Commit

Permalink
added option to removeempty values; moved functions to service
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark committed Apr 30, 2014
1 parent 51378e0 commit 0f9c698
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 57 deletions.
6 changes: 3 additions & 3 deletions src/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ var app = angular.module('baw',
}])


.run(['$rootScope', '$location', '$route', '$http', 'AudioEvent', 'conf.paths', 'UserProfile', 'ngAudioEvents',
function ($rootScope, $location, $route, $http, AudioEvent, paths, UserProfile, ngAudioEvents) {
.run(['$rootScope', '$location', '$route', '$http', 'AudioEvent', 'conf.paths', 'UserProfile', 'ngAudioEvents', '$url',
function ($rootScope, $location, $route, $http, AudioEvent, paths, UserProfile, ngAudioEvents, $url) {

// embed configuration for easy site-wide binding
$rootScope.paths = paths;
Expand Down Expand Up @@ -280,7 +280,7 @@ var app = angular.module('baw',
return {};
};
$rootScope.authTokenQuery = function () {
return baw.angularCopies.toKeyValue($rootScope.authTokenParams());
return $url.toKeyValue($rootScope.authTokenParams());
};

$rootScope.loggedIn = false;
Expand Down
40 changes: 0 additions & 40 deletions src/common/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,49 +296,9 @@ if (!Array.prototype.filter) {
};

function Angular() {
this.fixedEncodeURIComponent = function fixedEncodeURIComponent(str) {
str = str || "";
return encodeURIComponent(str)
.replace(/!/g, '%21')
.replace(/'/g, '%27')
.replace(/\(/g, '%28')
.replace(/\)/g, '%29')
.replace(/\*/g, '%2A')
.replace(/%20/g, '+');
};
this.toKeyValue = function toKeyValue(obj) {
var parts = [];
angular.forEach(obj, function (value, key) {

// only add key value pair if value is not undefined, not null, and is not an empty string
var valueIsUndefined = value == undefined;
var valueIsNull = value == null;
var valueIsEmptyString = typeof(value) === "string" && value.length < 1;

if(!valueIsUndefined && !valueIsNull && !valueIsEmptyString){
var encodedKey = encodeUriQuery(key, /* encode spaces */ true);
// if value is true, just include the key without a value
// TODO: why is this done?
var encodedValue = value === true ? '' : '=' + encodeUriQuery(value, /* encode spaces */ true);
parts.push(encodedKey + encodedValue);
}
});
return parts.length ? parts.join('&') : '';
};
this.encodeUriQuery = function encodeUriQuery(val, pctEncodeSpaces) {
val = val || "";
return encodeURIComponent(val).
replace(/%40/gi, '@').
replace(/%3A/gi, ':').
replace(/%24/g, '$').
replace(/%2C/gi, ',').
replace((pctEncodeSpaces ? null : /%20/g), '+');
};

this.isUndefined = function isUndefined(value) {
return typeof value == 'undefined';
};

}

baw.angularCopies = new Angular();
Expand Down
6 changes: 3 additions & 3 deletions src/components/services/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,8 @@
}]);

bawss.factory('AuthenticationProviders',
['$rootScope', 'authService', '$http', 'Authenticator', 'railsFieldRenamingInterceptor', '$q',
function ($rootScope, authService, $http, Authenticator, railsFieldRenamingInterceptor, $q) {
['$rootScope', 'authService', '$http', 'Authenticator', 'railsFieldRenamingInterceptor', '$q','$url',
function ($rootScope, authService, $http, Authenticator, railsFieldRenamingInterceptor, $q, $url) {
var signOutPath = '/security/sign_out';

function signOut() {
Expand Down Expand Up @@ -467,7 +467,7 @@

function openIdLogin(url) {
var popPath = "/security/auth/open_id?openid_url=" +
baw.angularCopies.fixedEncodeURIComponent(url);
$url.fixedEncodeURIComponent(url);
baw.popUpWindow(popPath, 700, 500, function (data) {
data = data || {};

Expand Down
48 changes: 37 additions & 11 deletions src/components/services/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ angular.module('url', ['ng']).
noop = angular.noop,
toJson = angular.toJson;

this.fixedEncodeURIComponent = function fixedEncodeURIComponent(str) {
function fixedEncodeURIComponent(str) {
str = str || "";
return encodeURIComponent(str)
.replace(/!/g, '%21')
Expand All @@ -22,24 +22,50 @@ angular.module('url', ['ng']).
.replace(/\)/g, '%29')
.replace(/\*/g, '%2A')
.replace(/%20/g, '+');
};

this.encodeUriQuery = function encodeUriQuery(val, pctEncodeSpaces) {
val = val || "";
}
this.fixedEncodeURIComponent = fixedEncodeURIComponent;

/**
* This method is intended for encoding *key* or *value* parts of query component. We need a custom
* method because encodeURIComponent is too aggressive and encodes stuff that doesn't have to be
* encoded per http://tools.ietf.org/html/rfc3986:
* query = *( pchar / "/" / "?" )
* pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
* pct-encoded = "%" HEXDIG HEXDIG
* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
* / "*" / "+" / "," / ";" / "="
*/
function encodeUriQuery(val, pctEncodeSpaces) {
if(angular.isUndefined(val) || val === null){
return '';
}
return encodeURIComponent(val).
replace(/%40/gi, '@').
replace(/%3A/gi, ':').
replace(/%24/g, '$').
replace(/%2C/gi, ',').
replace((pctEncodeSpaces ? null : /%20/g), '+');
};


replace(/%20/g, (pctEncodeSpaces ? '%20' : '+'));
}
this.encodeUriQuery = encodeUriQuery;

this.toKeyValue = function toKeyValue(obj) {
this.toKeyValue = function toKeyValue(obj, validateKeys) {
var parts = [];
angular.forEach(obj, function (value, key) {
parts.push(encodeUriQuery(key, true) + (value === true ? '' : '=' + encodeUriQuery(value, true)));
if (validateKeys) {
// only add key value pair if value is not undefined, not null, and is not an empty string
var valueIsEmptyString = angular.isString(value) && value.length < 1;
if (angular.isUndefined(value) || value == null || valueIsEmptyString || value === false) {
return;
}
}

var encodedKey = encodeUriQuery(key, /* encode spaces */ true);

// Angular does this: if value is true, just include the key without a value
var encodedValue = value === true ? '' : '=' + encodeUriQuery(value, /* encode spaces */ true);

parts.push(encodedKey + encodedValue);
});
return parts.length ? parts.join('&') : '';
};
Expand Down
37 changes: 37 additions & 0 deletions src/components/services/url.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
describe("The url service", function () {

var $url;

beforeEach(module('url'));

beforeEach(inject(["$url", function (providedUrl) {
$url = providedUrl;
}]));

it("returns an object", function () {
expect($url).toBeObject();
});

var myQuery = {
blah: '1',
tornado: "attack",
something: null,
hello: true,
bye: false,
'chocolate-chips': 'ring-ring',
monkeys: 'dancing dancing',
imNotHere: undefined,
imEmpty: ''
};

it("encodes a querystring", function(){
var result = $url.toKeyValue(myQuery);
expect(result).toBe('blah=1&tornado=attack&something=&hello&bye=false&chocolate-chips=ring-ring&monkeys=dancing%20dancing&imNotHere=&imEmpty=');
});

it("encodes a querystring without empty values", function(){
var result = $url.toKeyValue(myQuery, true);
expect(result).toBe('blah=1&tornado=attack&hello&chocolate-chips=ring-ring&monkeys=dancing%20dancing');
});

});

0 comments on commit 0f9c698

Please sign in to comment.