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

Commit 321ee99

Browse files
committed
refactor(ngMocks): clean up MockHttpExpectation#params()
1 parent 9824c59 commit 321ee99

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

src/ngMock/angular-mocks.js

+21-20
Original file line numberDiff line numberDiff line change
@@ -2145,7 +2145,9 @@ function MockHttpExpectation(method, url, data, headers, keys) {
21452145
};
21462146

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

21502152
function pathParams() {
21512153
var keyObj = {};
@@ -2164,30 +2166,29 @@ function MockHttpExpectation(method, url, data, headers, keys) {
21642166
return keyObj;
21652167
}
21662168

2167-
function parseQuery() {
2168-
var obj = {}, key_value, key,
2169-
queryStr = u.indexOf('?') > -1
2170-
? u.substring(u.indexOf('?') + 1)
2171-
: '';
2172-
2173-
angular.forEach(queryStr.split('&'), function(keyValue) {
2174-
if (keyValue) {
2175-
key_value = keyValue.replace(/\+/g,'%20').split('=');
2176-
key = tryDecodeURIComponent(key_value[0]);
2177-
if (angular.isDefined(key)) {
2178-
var val = angular.isDefined(key_value[1]) ? tryDecodeURIComponent(key_value[1]) : true;
2179-
if (!hasOwnProperty.call(obj, key)) {
2180-
obj[key] = val;
2181-
} else if (angular.isArray(obj[key])) {
2182-
obj[key].push(val);
2183-
} else {
2184-
obj[key] = [obj[key],val];
2185-
}
2169+
function parseQuery(queryStr) {
2170+
var obj = {},
2171+
keyValuePairs = queryStr.split('&').
2172+
filter(angular.identity). // Ignore empty segments.
2173+
map(function(keyValue) { return keyValue.replace(/\+/g, '%20').split('='); });
2174+
2175+
angular.forEach(keyValuePairs, function(pair) {
2176+
var key = tryDecodeURIComponent(pair[0]);
2177+
if (angular.isDefined(key)) {
2178+
var val = angular.isDefined(pair[1]) ? tryDecodeURIComponent(pair[1]) : true;
2179+
if (!hasOwnProperty.call(obj, key)) {
2180+
obj[key] = val;
2181+
} else if (angular.isArray(obj[key])) {
2182+
obj[key].push(val);
2183+
} else {
2184+
obj[key] = [obj[key], val];
21862185
}
21872186
}
21882187
});
2188+
21892189
return obj;
21902190
}
2191+
21912192
function tryDecodeURIComponent(value) {
21922193
try {
21932194
return decodeURIComponent(value);

0 commit comments

Comments
 (0)