refactor(router): code cleanup

This commit is contained in:
Victor Berchet 2016-12-02 15:34:28 -08:00 committed by Alex Rickabaugh
parent 349ad75de3
commit 307c4693dc
2 changed files with 14 additions and 29 deletions

View File

@ -96,7 +96,7 @@ export class Location {
* used, or the `APP_BASE_HREF` if the `PathLocationStrategy` is in use. * used, or the `APP_BASE_HREF` if the `PathLocationStrategy` is in use.
*/ */
prepareExternalUrl(url: string): string { prepareExternalUrl(url: string): string {
if (url.length > 0 && !url.startsWith('/')) { if (url && url[0] !== '/') {
url = '/' + url; url = '/' + url;
} }
return this._platformStrategy.prepareExternalUrl(url); return this._platformStrategy.prepareExternalUrl(url);
@ -143,7 +143,7 @@ export class Location {
* is. * is.
*/ */
public static normalizeQueryParams(params: string): string { public static normalizeQueryParams(params: string): string {
return (params.length > 0 && params.substring(0, 1) != '?') ? ('?' + params) : params; return params && params[0] !== '?' ? '?' + params : params;
} }
/** /**
@ -175,25 +175,13 @@ export class Location {
/** /**
* If url has a trailing slash, remove it, otherwise return url as is. * If url has a trailing slash, remove it, otherwise return url as is.
*/ */
public static stripTrailingSlash(url: string): string { public static stripTrailingSlash(url: string): string { return url.replace(/\/$/, ''); }
if (/\/$/g.test(url)) {
url = url.substring(0, url.length - 1);
}
return url;
}
} }
function _stripBaseHref(baseHref: string, url: string): string { function _stripBaseHref(baseHref: string, url: string): string {
if (baseHref.length > 0 && url.startsWith(baseHref)) { return baseHref && url.startsWith(baseHref) ? url.substring(baseHref.length) : url;
return url.substring(baseHref.length);
}
return url;
} }
function _stripIndexHtml(url: string): string { function _stripIndexHtml(url: string): string {
if (/\/index.html$/g.test(url)) { return url.replace(/\/index.html$/, '');
// '/index.html'.length == 11
return url.substring(0, url.length - 11);
}
return url;
} }

View File

@ -23,7 +23,7 @@ import {mergeMap} from 'rxjs/operator/mergeMap';
import {reduce} from 'rxjs/operator/reduce'; import {reduce} from 'rxjs/operator/reduce';
import {applyRedirects} from './apply_redirects'; import {applyRedirects} from './apply_redirects';
import {Data, ResolveData, Routes, validateConfig} from './config'; import {ResolveData, Routes, validateConfig} from './config';
import {createRouterState} from './create_router_state'; import {createRouterState} from './create_router_state';
import {createUrlTree} from './create_url_tree'; import {createUrlTree} from './create_url_tree';
import {RouterOutlet} from './directives/router_outlet'; import {RouterOutlet} from './directives/router_outlet';
@ -38,7 +38,7 @@ import {UrlSerializer, UrlTree, containsTree, createEmptyUrlTree} from './url_tr
import {andObservables, forEach, merge, waitForMap, wrapIntoObservable} from './utils/collection'; import {andObservables, forEach, merge, waitForMap, wrapIntoObservable} from './utils/collection';
import {TreeNode} from './utils/tree'; import {TreeNode} from './utils/tree';
declare var Zone: any; declare let Zone: any;
/** /**
* @whatItDoes Represents the extra options used during navigation. * @whatItDoes Represents the extra options used during navigation.
@ -55,16 +55,13 @@ export interface NavigationExtras {
* [{ * [{
* path: 'parent', * path: 'parent',
* component: ParentComponent, * component: ParentComponent,
* children: [ * children: [{
* { * path: 'list',
* path: 'list', * component: ListComponent
* component: ListComponent * },{
* }, * path: 'child',
* { * component: ChildComponent
* path: 'child', * }]
* component: ChildComponent
* }
* ]
* }] * }]
* ``` * ```
* *