test(core): matcher.factory test cases
DELETE
packages/core/src/operators/matchPath/matchPath.factory.ts
-const removeTrailingSlash = (path: string): string =>
- path.replace(/\/$/, '');
-
-const removeQueryParams = (path: string): string =>
- path.split('?')[0];
-
-export const pathFactory = (matchers: string[], path: string, suffix?: string): string =>
- removeTrailingSlash(
- matchers.reduce((prev, cur) => prev + cur, '')
- + path + (suffix || '')
- );
-
-export const urlFactory = (path: string): string =>
- removeQueryParams(path);

MODIFY
packages/core/src/operators/matchPath/matchPath.operator.ts
packages/core/src/operators/matchPath/matchPath.operator.ts
@@ -2,7 +2,7 @@ import * as pathToRegexp from 'path-to-regexp';
import { Observable } from 'rxjs';
import { filter, tap } from 'rxjs/operators';
import { HttpRequest } from '../../http.interface';
-import { pathFactory, urlFactory } from './matchPath.factory';
+import { matcherFactory, removeQueryParams } from './matcher.factory';
import { queryParamsFactory } from './queryParams.factory';
import { urlParamsFactory } from './urlParams.factory';
@@ -19,9 +19,9 @@ export const matchPath = (path: string, opts: MatcherOpts = {}) => (
tap(req => (req.params = req.params || {})),
tap(req => (req.matchers = req.matchers || [])),
filter(req => {
- const match = pathFactory(req.matchers!, path, opts.suffix);
- const url = urlFactory(req.url!);
- return pathToRegexp(match).test(url);
+ const matcher = matcherFactory(req.matchers!, path, opts.suffix);
+ const url = removeQueryParams(req.url!);
+ return pathToRegexp(matcher).test(url);
}),
tap(req => (req.params = urlParamsFactory(req, path))),
tap(req => (req.query = queryParamsFactory(req.url!))),

ADD
packages/core/src/operators/matchPath/matcher.factory.spec.ts
+import { matcherFactory, removeQueryParams, removeTrailingSlash } from './matcher.factory';
+
+describe('matcher factory', () => {
+
+ it('#removeQueryParams removes query parameters from url', () => {
+ // given
+ const input = 'api/v1/foo?testParam';
+ const result = 'api/v1/foo';
+
+ // when
+ const urlWithoutQuery = removeQueryParams(input);
+
+ // then
+ expect(urlWithoutQuery).toEqual(result);
+ });
+
+ it('#removeTrailingSlash removes trailing slash from url', () => {
+ // given
+ const input = 'api/v1/foo/';
+ const result = 'api/v1/foo';
+
+ // when
+ const urlWithoutTrailingSlash = removeTrailingSlash(input);
+
+ // then
+ expect(urlWithoutTrailingSlash).toEqual(result);
+ });
+
+ it('#matcherFactory factorizes matcher url based on matching history', () => {
+ // given
+ const matchingHistory = ['/api/v1', '/user'];
+ const pathToMatch = '/foo/';
+
+ // when
+ const matcher = matcherFactory(matchingHistory, pathToMatch);
+
+ // then
+ expect(matcher).toEqual('/api/v1/user/foo');
+ });
+
+ it('#matcherFactory factorizes matcher url based on matching history', () => {
+ // given
+ const matchingHistory = ['/api/v1', '/user'];
+ const pathToMatch = '/foo/';
+
+ // when
+ const matcher = matcherFactory(matchingHistory, pathToMatch);
+
+ // then
+ expect(matcher).toEqual('/api/v1/user/foo');
+ });
+
+ it('#matcherFactory factorizes matcher with additional suffix', () => {
+ // given
+ const matchingHistory = ['/api/:version', '/user'];
+ const pathToMatch = '/foo/';
+ const suffix = '/:foo*';
+
+ // when
+ const matcher = matcherFactory(matchingHistory, pathToMatch, suffix);
+
+ // then
+ expect(matcher).toEqual('/api/:version/user/foo/:foo*');
+ });
+
+});

ADD
packages/core/src/operators/matchPath/matcher.factory.ts
+export const removeTrailingSlash = (path: string): string =>
+ path.replace(/\/$/, '');
+
+export const removeQueryParams = (path: string): string => path.split('?')[0];
+
+export const matcherFactory = (
+ matchingHistory: string[],
+ pathToMatch: string,
+ suffix?: string
+): string =>
+ removeTrailingSlash(
+ matchingHistory.reduce((prev, cur) => prev + cur, '') +
+ removeTrailingSlash(pathToMatch) +
+ (suffix || '')
+ );

MODIFY
packages/core/src/operators/matchPath/urlParams.factory.ts
packages/core/src/operators/matchPath/urlParams.factory.ts
import * as pathToRegexp from 'path-to-regexp';
import { HttpRequest, RouteParameters } from '../../http.interface';
-import { pathFactory, urlFactory } from './matchPath.factory';
+import { matcherFactory, removeQueryParams } from './matcher.factory';
-export const urlParamsFactory = (req: HttpRequest, path: string): RouteParameters => {
- const match = pathFactory(req.matchers!, path);
- const url = urlFactory(req.url!);
+export const urlParamsFactory = (
+ req: HttpRequest,
+ path: string
+): RouteParameters => {
+ const match = matcherFactory(req.matchers!, path);
+ const url = removeQueryParams(req.url!);
const params = pathToRegexp(match).exec(url);
const routes = pathToRegexp.parse(match);
- if (!params || !routes) { return {}; }
+ if (!params || !routes) {
+ return {};
+ }
- return routes
- .filter(route => route.hasOwnProperty('name'))
- .reduce((obj, route, index) => ({
+ return routes.filter(route => route.hasOwnProperty('name')).reduce(
+ (obj, route, index) => ({
...obj,
- [route['name']]: params[++index],
- }), {});
+ [route['name']]: params[++index]
+ }),
+ {}
+ );
};

test(core): matcher.factory test cases