feat(router): add `ParamMap.keys` to get a list of parameters

This commit is contained in:
Victor Berchet 2017-03-20 09:19:25 -07:00
parent a755b715ed
commit d3eda7a5b5
4 changed files with 20 additions and 1 deletions

View File

@ -20,7 +20,7 @@ export {ExtraOptions, ROUTER_CONFIGURATION, ROUTER_INITIALIZER, RouterModule, pr
export {RouterOutletMap} from './router_outlet_map';
export {NoPreloading, PreloadAllModules, PreloadingStrategy, RouterPreloader} from './router_preloader';
export {ActivatedRoute, ActivatedRouteSnapshot, RouterState, RouterStateSnapshot} from './router_state';
export {PRIMARY_OUTLET, ParamMap, Params} from './shared';
export {PRIMARY_OUTLET, ParamMap, Params, convertToParamMap} from './shared';
export {UrlHandlingStrategy} from './url_handling_strategy';
export {DefaultUrlSerializer, UrlSegment, UrlSegmentGroup, UrlSerializer, UrlTree} from './url_tree';
export {VERSION} from './version';

View File

@ -54,6 +54,9 @@ export interface ParamMap {
* If there is no such parameter, an empty array is returned.
*/
getAll(name: string): string[];
/** Name of the parameters */
readonly keys: string[];
}
class ParamsAsMap implements ParamMap {
@ -80,8 +83,15 @@ class ParamsAsMap implements ParamMap {
return [];
}
get keys(): string[] { return Object.keys(this.params); }
}
/**
* Convert a {@link Params} instance to a {@link ParamMap}.
*
* @stable
*/
export function convertToParamMap(params: Params): ParamMap {
return new ParamsAsMap(params);
}

View File

@ -16,6 +16,11 @@ describe('ParamsMap', () => {
expect(map.has('not here')).toEqual(false);
});
it('should returns the name of the parameters', () => {
const map = convertToParamMap({single: 's', multiple: ['m1', 'm2']});
expect(map.keys).toEqual(['single', 'multiple']);
});
it('should support single valued parameters', () => {
const map = convertToParamMap({single: 's', multiple: ['m1', 'm2']});
expect(map.get('single')).toEqual('s');

View File

@ -59,6 +59,9 @@ export interface CanLoad {
canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean;
}
/** @stable */
export declare function convertToParamMap(params: Params): ParamMap;
/** @stable */
export declare type Data = {
[name: string]: any;
@ -156,6 +159,7 @@ export declare class NoPreloading implements PreloadingStrategy {
/** @stable */
export interface ParamMap {
readonly keys: string[];
get(name: string): string | null;
getAll(name: string): string[];
has(name: string): boolean;