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

Commit 2bbc7c4

Browse files
committed
refactor(ngMocks): simplify routeToRegExp by assuming path has query/hash stripped off
Closes #16672
1 parent e85f91d commit 2bbc7c4

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

src/ngMock/angular-mocks.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,8 +1771,8 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
17711771
* See {@link ngMock.$httpBackend#when `when`} for more info.
17721772
*/
17731773
$httpBackend.whenRoute = function(method, url) {
1774-
var pathObj = routeToRegExp(url, {caseInsensitiveMatch: true, ignoreTrailingSlashes: true, isUrl: true});
1775-
return $httpBackend.when(method, pathObj.regexp, undefined, undefined, pathObj.keys);
1774+
var parsed = parseRouteUrl(url);
1775+
return $httpBackend.when(method, parsed.regexp, undefined, undefined, parsed.keys);
17761776
};
17771777

17781778
/**
@@ -1955,8 +1955,8 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
19551955
* See {@link ngMock.$httpBackend#expect `expect`} for more info.
19561956
*/
19571957
$httpBackend.expectRoute = function(method, url) {
1958-
var pathObj = routeToRegExp(url, {caseInsensitiveMatch: true, ignoreTrailingSlashes: true, isUrl: true});
1959-
return $httpBackend.expect(method, pathObj.regexp, undefined, undefined, pathObj.keys);
1958+
var parsed = parseRouteUrl(url);
1959+
return $httpBackend.expect(method, parsed.regexp, undefined, undefined, parsed.keys);
19601960
};
19611961

19621962

@@ -2084,6 +2084,12 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
20842084
};
20852085
});
20862086
}
2087+
2088+
function parseRouteUrl(url) {
2089+
var strippedUrl = stripQueryAndHash(url);
2090+
var parseOptions = {caseInsensitiveMatch: true, ignoreTrailingSlashes: true};
2091+
return routeToRegExp(strippedUrl, parseOptions);
2092+
}
20872093
}
20882094

20892095
function assertArgDefined(args, index, name) {

src/routeToRegExp.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,30 @@
33
/* global routeToRegExp: true */
44

55
/**
6-
* @param pathOrUrl {string} path or url
7-
* @param opts {Object} options
8-
* @return {?Object}
6+
* @param {string} path - The path to parse. (It is assumed to have query and hash stripped off.)
7+
* @param {Object} opts - Options.
8+
* @return {Object} - An object containing an array of path parameter names (`keys`) and a regular
9+
* expression (`regexp`) that can be used to identify a matching URL and extract the path
10+
* parameter values.
911
*
1012
* @description
11-
* Normalizes the given path, returning a regular expression
12-
* and the original path.
13+
* Parses the given path, extracting path parameter names and a regular expression to match URLs.
1314
*
14-
* Inspired by pathRexp in visionmedia/express/lib/utils.js.
15+
* Originally inspired by `pathRexp` in `visionmedia/express/lib/utils.js`.
1516
*/
16-
function routeToRegExp(pathOrUrl, opts) {
17+
function routeToRegExp(path, opts) {
1718
var keys = [];
1819

19-
var pattern = pathOrUrl
20+
var pattern = path
2021
.replace(/([().])/g, '\\$1')
2122
.replace(/(\/)?:(\w+)(\*\?|[?*])?/g, function(_, slash, key, option) {
2223
var optional = option === '?' || option === '*?';
2324
var star = option === '*' || option === '*?';
24-
keys.push({ name: key, optional: optional });
25+
keys.push({name: key, optional: optional});
2526
slash = slash || '';
2627
return (
2728
(optional ? '(?:' + slash : slash + '(?:') +
28-
(opts.isUrl ? (star ? '([^?#]+?)' : '([^/?#]+)') : (star ? '(.+?)' : '([^/]+)')) +
29+
(star ? '(.+?)' : '([^/]+)') +
2930
(optional ? '?)?' : ')')
3031
);
3132
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy