style(core): refactor of reqMatched in matchPath operator
MODIFY
packages/core/src/operators/matchPath/matchPath.operator.spec.ts
packages/core/src/operators/matchPath/matchPath.operator.spec.ts
@@ -3,11 +3,13 @@ import { Marbles } from '../../util/marbles.spec-util';
import { matchPath } from './matchPath.operator';
const req = (url: string) => ({ url } as HttpRequest);
-const reqMatched = (url: string, query = '', matchers: string[] = [], params: RouteParameters = {}) => ({
- url: !!query ? `${url}?${query}` : url,
- matchers,
- params,
-} as any as HttpRequest);
+
+const reqMatched = (
+ url: string,
+ matchers: string[] = [],
+ params: RouteParameters = {},
+ query: object = {}
+) => (({ url, matchers, params, query } as any) as HttpRequest);
describe('matchPath operator', () => {
let operators;
@@ -20,13 +22,13 @@ describe('matchPath operator', () => {
operators = [matchPath('/')];
Marbles.assert(operators, [
['-a-b---', { a: req('/'), b: req('/test') }],
- ['-a-----', { a: reqMatched('/') }],
+ ['-a-----', { a: reqMatched('/') }]
]);
operators = [matchPath('/test')];
Marbles.assert(operators, [
['-a-b---', { a: req('/'), b: req('/test') }],
- ['---b---', { b: reqMatched('/test') }],
+ ['---b---', { b: reqMatched('/test') }]
]);
});
@@ -34,49 +36,69 @@ describe('matchPath operator', () => {
operators = [matchPath('/test/:foo/bar')];
Marbles.assert(operators, [
['-a-b---', { a: req('/test/bar'), b: req('/test/url/bar') }],
- ['---b---', { b: reqMatched('/test/url/bar', null, [], { foo: 'url' }) }],
+ ['---b---', { b: reqMatched('/test/url/bar', [], { foo: 'url' }) }]
]);
});
it('matches path with query parameter', () => {
operators = [matchPath('/test')];
Marbles.assert(operators, [
- ['-a-b-c-', {
+ [
+ '-a-b-c-',
+ {
a: req('/test/bar'),
b: req('/test?foo=bar'),
- c: req('/test/?foo=bar'),
- }],
- ['---b-c-', {
- b: reqMatched('/test', 'foo=bar'),
- c: reqMatched('/test/', 'foo=bar'),
- }],
+ c: req('/test/?foo=bar')
+ }
+ ],
+ [
+ '---b-c-',
+ {
+ b: reqMatched('/test?foo=bar'),
+ c: reqMatched('/test/?foo=bar')
+ }
+ ]
]);
});
it('matches path with url and query parameter', () => {
operators = [matchPath('/test/:foo/bar')];
Marbles.assert(operators, [
- ['-a-b--', {
+ [
+ '-a-b--',
+ {
a: req('/test/bar'),
- b: req('/test/url/bar?foo=bar') }],
- ['---b--', {
- b: reqMatched('/test/url/bar', 'foo=bar', [], { foo: 'url' }) }],
+ b: req('/test/url/bar?foo=bar')
+ }
+ ],
+ [
+ '---b--',
+ {
+ b: reqMatched('/test/url/bar?foo=bar', [], { foo: 'url' })
+ }
+ ]
]);
});
it('matches path with additional suffix parameter', () => {
operators = [matchPath('/test', { suffix: '/:foo*' })];
Marbles.assert(operators, [
- ['-a-b-c-', {
+ [
+ '-a-b-c-',
+ {
a: req('/test'),
b: req('/test/bar'),
- c: req('/test/url/bar'),
- }],
- ['-a-b-c-', {
+ c: req('/test/url/bar')
+ }
+ ],
+ [
+ '-a-b-c-',
+ {
a: reqMatched('/test'),
b: reqMatched('/test/bar'),
- c: reqMatched('/test/url/bar'),
- }],
+ c: reqMatched('/test/url/bar')
+ }
+ ]
]);
});
@@ -84,14 +106,15 @@ describe('matchPath operator', () => {
operators = [matchPath('/test', { combiner: true })];
Marbles.assert(operators, [
['-a--', { a: req('/test') }],
- ['-a--', { a: reqMatched('/test', null, ['/test']) }],
+ ['-a--', { a: reqMatched('/test', ['/test']) }]
]);
- operators = [matchPath('/test/:id/foo', { combiner: true, suffix: '/:foo*' })];
+ operators = [
+ matchPath('/test/:id/foo', { combiner: true, suffix: '/:foo*' })
+ ];
Marbles.assert(operators, [
['-a--', { a: req('/test/2/foo') }],
- ['-a--', { a: reqMatched('/test/2/foo', null, ['/test/:id/foo'], { id: '2' }) }],
+ ['-a--', { a: reqMatched('/test/2/foo', ['/test/:id/foo'], { id: '2' }) }]
]);
});
-
});

MODIFY
packages/core/src/operators/matchPath/matchPath.operator.ts
packages/core/src/operators/matchPath/matchPath.operator.ts
@@ -6,18 +6,22 @@ import { pathFactory, urlFactory } from './matchPath.factory';
import { urlParamsFactory } from './urlParams.factory';
type MatcherOpts = {
- suffix?: string,
- combiner?: boolean,
+ suffix?: string;
+ combiner?: boolean;
};
-export const matchPath = (path: string, opts: MatcherOpts = {}) => (source$: Observable<HttpRequest>) =>
+export const matchPath = (path: string, opts: MatcherOpts = {}) => (
+ source$: Observable<HttpRequest>
+) =>
source$.pipe(
- tap(req => req.matchers = req.matchers || []),
+ tap(req => (req.query = req.query || {})),
+ 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);
}),
- tap(req => req.params = urlParamsFactory(req, path)),
+ tap(req => (req.params = urlParamsFactory(req, path))),
tap(req => opts.combiner && req.matchers!.push(path))
);

style(core): refactor of reqMatched in matchPath operator