Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
refactor(ngMocks): clean up MockHttpExpectation#params()
Browse files Browse the repository at this point in the history
  • Loading branch information
gkalpak committed Aug 25, 2018
1 parent b074d71 commit bd772ab
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2145,7 +2145,9 @@ function MockHttpExpectation(method, url, data, headers, keys) {
};

this.params = function(u) {
return angular.extend(parseQuery(), pathParams());
var queryStr = u.indexOf('?') === -1 ? '' : u.substring(u.indexOf('?') + 1);

return angular.extend(parseQuery(queryStr), pathParams());

function pathParams() {
var keyObj = {};
Expand All @@ -2164,30 +2166,29 @@ function MockHttpExpectation(method, url, data, headers, keys) {
return keyObj;
}

function parseQuery() {
var obj = {}, key_value, key,
queryStr = u.indexOf('?') > -1
? u.substring(u.indexOf('?') + 1)
: '';

angular.forEach(queryStr.split('&'), function(keyValue) {
if (keyValue) {
key_value = keyValue.replace(/\+/g,'%20').split('=');
key = tryDecodeURIComponent(key_value[0]);
if (angular.isDefined(key)) {
var val = angular.isDefined(key_value[1]) ? tryDecodeURIComponent(key_value[1]) : true;
if (!hasOwnProperty.call(obj, key)) {
obj[key] = val;
} else if (angular.isArray(obj[key])) {
obj[key].push(val);
} else {
obj[key] = [obj[key],val];
}
function parseQuery(queryStr) {
var obj = {},
keyValuePairs = queryStr.split('&').
filter(angular.identity). // Ignore empty segments.
map(function(keyValue) { return keyValue.replace(/\+/g, '%20').split('='); });

angular.forEach(keyValuePairs, function(pair) {
var key = tryDecodeURIComponent(pair[0]);
if (angular.isDefined(key)) {
var val = angular.isDefined(pair[1]) ? tryDecodeURIComponent(pair[1]) : true;
if (!hasOwnProperty.call(obj, key)) {
obj[key] = val;
} else if (angular.isArray(obj[key])) {
obj[key].push(val);
} else {
obj[key] = [obj[key], val];
}
}
});

return obj;
}

function tryDecodeURIComponent(value) {
try {
return decodeURIComponent(value);
Expand Down

0 comments on commit bd772ab

Please sign in to comment.