feat(core): removed ribbish after rebase
DELETE
packages/core/src/operators/matchPath.operator.ts
-import * as pathToRegexp from 'path-to-regexp';
-import { Observable } from 'rxjs';
-import { filter, tap } from 'rxjs/operators';
-import { HttpRequest, HttpRoute, RouteParameters } from '../http.interface';
-import { queryParamsFactory } from "./queryParamsFactory/queryParams.factory";
-import { urlParamsFactory } from "./urlParamsFactory/urlParams.factory";
-
-type MatcherOpts = {
- suffix?: string,
- combiner?: boolean,
-};
-
-const removeTrailingSlash = (path: string): string =>
- path.replace(/\/$/, '');
-
-const removeQueryParams = (path: string): string =>
- path.split('?')[0];
-
-const pathFactory = (matchers: string[], path: string, suffix?: string): string =>
- removeTrailingSlash(
- matchers.reduce((prev, cur) => prev + cur, '')
- + path + (suffix || '')
- );
-
-const urlFactory = (path: string): string =>
- removeQueryParams(path);
-
-const routeFactory = (req: HttpRequest, path: string): HttpRoute => {
- const match = pathFactory(req.matchers!, path);
- const url = urlFactory(req.url!);
- const params = pathToRegexp(match).exec(url);
- const routes = pathToRegexp.parse(match);
-
- return {
- url,
- params: urlParamsFactory(params, routes),
- query: queryParamsFactory(req.url!)
- };
-};
-
-export const matchPath = (path: string, opts: MatcherOpts = {}) => (source$: Observable<HttpRequest>) =>
- source$.pipe(
- 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.route = {
- ...req.route,
- ...routeFactory(req, path),
- }),
- tap(req => opts.combiner && req.matchers!.push(path))
- );

RENAME
packages/core/src/operators/matchPath/queryParamsFactory.spec.ts
packages/core/src/operators/matchPath/queryParams.factory.spec.ts
MODIFY
packages/core/src/operators/matchPath/queryParams.factory.ts
packages/core/src/operators/matchPath/queryParams.factory.ts
@@ -15,6 +15,7 @@ const getNestedQueryParam = (
.replace(/\[/g, '')
.split(']')
.filter(v => v.length > 0);
+
const nestedQueryObject = extractedKeys.reduceRight(
(value, key) => ({ [key]: value }),
paramValue
@@ -37,7 +38,6 @@ export const queryParamsFactory = (path: string): QueryParameters => {
}
const queryParamsString = path.split('?').pop();
-
const extractedQueryParams = querystring.parse(queryParamsString!);
return queryParamsArrayToObject(extractedQueryParams);

DELETE
packages/core/src/operators/queryParamsFactory/queryParams.factory.ts
-import * as querystring from 'querystring';
-import { QueryParameters } from '../../http.interface';
-
-const getNestedQueryParam = (key: string, paramValue: string | Object): string | Object => {
- const nestedKeys = /\[.*\]/.exec(key);
-
- if(!nestedKeys) return ({ [key]: paramValue });
-
- const extractedKeys = nestedKeys[0].replace(/\[/g, '').split(']').filter(v => v.length > 0);
- const nestedQueryObject = extractedKeys.reduceRight((value, key) => ({ [key]: value }), paramValue);
-
- return { [key.replace(nestedKeys[0], '')]: nestedQueryObject };
-}
-
-const queryParamsArrayToObject = (queryParams: Object): QueryParameters => {
- const queryParamsKey = Object.keys(queryParams);
- return Object.assign({}, ...queryParamsKey.map(key => getNestedQueryParam(key, queryParams[key])));
-}
-
-export const queryParamsFactory = (path: string): QueryParameters => {
- if (!path || !path.includes('?')) return {};
- const queryParamsString = path.split('?').pop();
-
-
- const extractedQueryParams = querystring.parse(queryParamsString!);
-
- return queryParamsArrayToObject(extractedQueryParams);
-}

DELETE
packages/core/src/operators/queryParamsFactory/queryParamsFactory.spec.ts
-import { queryParamsFactory } from "./queryParams.factory";
-
-
-describe("queryParamsFactory", () => {
- test("should return empty object when passed URL with no valid params", () => {
- // given
- const invalidURLS = ["http://test.com", "http://test.com?", null];
-
- invalidURLS.forEach(url => {
- // when
- const params = queryParamsFactory(url);
-
- // then
- expect(params).toEqual({});
- });
- });
-
- test("should return empty string when no value for provided key", () => {
- // given
- const exampleURL = "http://test.com?testParam";
- const expectedParamsObj = { testParam: "" };
-
- // when
- const params = queryParamsFactory(exampleURL);
-
- // then
- expect(params).toEqual(expectedParamsObj);
- });
-
- test("should return object with multiple keys", () => {
- // given
- const exampleURL = "http://test.com?testParam=test&testParam2=test2";
- const expectedParamsObj = { testParam: "test", testParam2: "test2" };
-
- // when
- const params = queryParamsFactory(exampleURL);
-
- // then
- expect(params).toEqual(expectedParamsObj);
- });
-
- test("should return array when passed two times same parameter", () => {
- // given
- const exampleURL = "http://test.com?testParam=test&testParam=123";
- const expectedParamsObj = { testParam: ["test", "123"] };
-
- // when
- const params = queryParamsFactory(exampleURL);
-
- // then
- expect(params).toEqual(expectedParamsObj);
- });
-
- test("should return nested object", () => {
- // given
- const exampleURL = "http://test.com?testParam[testNested]=test";
- const expectedParamsObj = { testParam: { testNested: "test" } };
-
- // when
- const params = queryParamsFactory(exampleURL);
-
- // then
- expect(params).toEqual(expectedParamsObj);
- });
-});

DELETE
packages/core/src/operators/urlParamsFactory/urlParams.factory.ts
-import * as pathToRegexp from 'path-to-regexp';
-import { RouteParameters } from '../../http.interface';
-
-export const urlParamsFactory = (params: RegExpMatchArray | null, routes: pathToRegexp.Token[]): RouteParameters => {
- if (!params || !routes) { return {}; }
-
- return routes
- .filter(route => route.hasOwnProperty('name'))
- .reduce((obj, route, index) => ({
- ...obj,
- [route['name']]: params[++index],
- }), {});
-};

feat(core): removed ribbish after rebase