refactor(router): use object spread operator instead of merge fn

This commit is contained in:
Dzmitry Shylovich 2017-03-26 17:15:10 +03:00 committed by Victor Berchet
parent b7fa5dec21
commit 606b8fafb0
5 changed files with 12 additions and 30 deletions

View File

@ -22,7 +22,7 @@ import {Route, Routes} from './config';
import {LoadedRouterConfig, RouterConfigLoader} from './router_config_loader';
import {PRIMARY_OUTLET, Params, defaultUrlMatcher, navigationCancelingError} from './shared';
import {UrlSegment, UrlSegmentGroup, UrlSerializer, UrlTree} from './url_tree';
import {andObservables, forEach, merge, waitForMap, wrapIntoObservable} from './utils/collection';
import {andObservables, forEach, waitForMap, wrapIntoObservable} from './utils/collection';
class NoMatch {
constructor(public segmentGroup: UrlSegmentGroup = null) {}
@ -478,7 +478,7 @@ function addEmptySegmentsToChildrenIfNeeded(
res[getOutlet(r)] = new UrlSegmentGroup([], {});
}
}
return merge(children, res);
return {...children, ...res};
}
function createChildrenForEmptySegments(

View File

@ -15,7 +15,7 @@ import {Data, ResolveData, Route, Routes} from './config';
import {ActivatedRouteSnapshot, RouterStateSnapshot, inheritedParamsDataResolve} from './router_state';
import {PRIMARY_OUTLET, defaultUrlMatcher} from './shared';
import {UrlSegment, UrlSegmentGroup, UrlTree, mapChildrenIntoArray} from './url_tree';
import {forEach, last, merge} from './utils/collection';
import {forEach, last} from './utils/collection';
import {TreeNode} from './utils/tree';
class NoMatch {}
@ -180,7 +180,7 @@ function match(segmentGroup: UrlSegmentGroup, route: Route, segments: UrlSegment
const posParams: {[n: string]: string} = {};
forEach(res.posParams, (v: UrlSegment, k: string) => { posParams[k] = v.path; });
const parameters = merge(posParams, res.consumed[res.consumed.length - 1].parameters);
const parameters = {...posParams, ...res.consumed[res.consumed.length - 1].parameters};
return {consumedSegments: res.consumed, lastChild: res.consumed.length, parameters};
}
@ -259,7 +259,7 @@ function addEmptyPathsToChildrenIfNeeded(
res[getOutlet(r)] = s;
}
}
return merge(children, res);
return {...children, ...res};
}
function createChildrenForEmptyPaths(

View File

@ -35,7 +35,7 @@ import {ActivatedRoute, ActivatedRouteSnapshot, RouterState, RouterStateSnapshot
import {PRIMARY_OUTLET, Params, isNavigationCancelingError} from './shared';
import {DefaultUrlHandlingStrategy, UrlHandlingStrategy} from './url_handling_strategy';
import {UrlSerializer, UrlTree, containsTree, createEmptyUrlTree} from './url_tree';
import {andObservables, forEach, merge, shallowEqual, waitForMap, wrapIntoObservable} from './utils/collection';
import {andObservables, forEach, shallowEqual, waitForMap, wrapIntoObservable} from './utils/collection';
import {TreeNode} from './utils/tree';
declare let Zone: any;
@ -411,7 +411,7 @@ export class Router {
if (queryParamsHandling) {
switch (queryParamsHandling) {
case 'merge':
q = merge(this.currentUrlTree.queryParams, queryParams);
q = {...this.currentUrlTree.queryParams, ...queryParams};
break;
case 'preserve':
q = this.currentUrlTree.queryParams;
@ -981,7 +981,7 @@ export class PreActivation {
const resolve = future._resolve;
return map.call(this.resolveNode(resolve, future), (resolvedData: any): any => {
future._resolvedData = resolvedData;
future.data = merge(future.data, inheritedParamsDataResolve(future).resolve);
future.data = {...future.data, ...inheritedParamsDataResolve(future).resolve};
return null;
});
}

View File

@ -14,7 +14,7 @@ import {map} from 'rxjs/operator/map';
import {Data, ResolveData, Route} from './config';
import {PRIMARY_OUTLET, ParamMap, Params, convertToParamMap} from './shared';
import {UrlSegment, UrlSegmentGroup, UrlTree, equalSegments} from './url_tree';
import {merge, shallowEqual, shallowEqualArrays} from './utils/collection';
import {shallowEqual, shallowEqualArrays} from './utils/collection';
import {Tree, TreeNode} from './utils/tree';
@ -205,9 +205,9 @@ export function inheritedParamsDataResolve(route: ActivatedRouteSnapshot): Inher
}
return pathToRoot.slice(inhertingStartingFrom).reduce((res, curr) => {
const params = merge(res.params, curr.params);
const data = merge(res.data, curr.data);
const resolve = merge(res.resolve, curr._resolvedData);
const params = {...res.params, ...curr.params};
const data = {...res.data, ...curr.data};
const resolve = {...res.resolve, ...curr._resolvedData};
return {params, data, resolve};
}, <any>{params: {}, data: {}, resolve: {}});
}

View File

@ -63,24 +63,6 @@ export function and(bools: boolean[]): boolean {
return !bools.some(v => !v);
}
export function merge<V>(m1: {[key: string]: V}, m2: {[key: string]: V}): {[key: string]: V} {
const m: {[key: string]: V} = {};
for (const attr in m1) {
if (m1.hasOwnProperty(attr)) {
m[attr] = m1[attr];
}
}
for (const attr in m2) {
if (m2.hasOwnProperty(attr)) {
m[attr] = m2[attr];
}
}
return m;
}
export function forEach<K, V>(map: {[key: string]: V}, callback: (v: V, k: string) => void): void {
for (const prop in map) {
if (map.hasOwnProperty(prop)) {