Skip to content
This repository was archived by the owner on Dec 8, 2022. It is now read-only.

Update skyAppLink to use optional excludeDefaults to getAll. #402

Merged
merged 6 commits into from
May 7, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion runtime/directives/sky-app-link.directive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('SkyAppLink Directive', () => {
let component: SkyAppLinkTestComponent;
let fixture: ComponentFixture<SkyAppLinkTestComponent>;
let debugElement: DebugElement;
let getAllParam: boolean;

function setup(params: any, useQueryParams: boolean) {
let componentToUse = useQueryParams ?
Expand All @@ -46,7 +47,10 @@ describe('SkyAppLink Directive', () => {
useValue: {
runtime: {
params: {
getAll: () => params
getAll: (p?: boolean) => {
getAllParam = p;
return params;
}
}
},
skyux: {}
Expand Down Expand Up @@ -95,4 +99,10 @@ describe('SkyAppLink Directive', () => {
expect(directive.attributes['skyAppLink']).toEqual('test');
expect(directive.properties['href']).toEqual('/test?qp1=1&qp2=false&asdf=123&jkl=mno');
});

it('should call getAll with excludeDefaults set to true', () => {
setup({}, true);
debugElement.query(By.directive(SkyAppLinkDirective));
expect(getAllParam).toBe(true);
});
});
4 changes: 2 additions & 2 deletions runtime/directives/sky-app-link.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ export class SkyAppLinkDirective extends RouterLinkWithHref {

@Input()
set queryParams(params: { [k: string]: any }) {
this._queryParams = Object.assign(params, this.skyAppConfig.runtime.params.getAll());
this._queryParams = Object.assign(params, this.skyAppConfig.runtime.params.getAll(true));
}

get queryParams() {
if (!this._queryParams) {
this._queryParams = Object.assign({}, this.skyAppConfig.runtime.params.getAll());
this._queryParams = Object.assign({}, this.skyAppConfig.runtime.params.getAll(true));
}
return this._queryParams;
}
Expand Down
21 changes: 21 additions & 0 deletions runtime/params.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,27 @@ describe('SkyAppRuntimeConfigParams', () => {
expect(params.get('a2')).toBe('c');
});

it('should support excluding default values in getAll() if specified', () => {
const params: SkyAppRuntimeConfigParams = new SkyAppRuntimeConfigParams(
'?a2=a2Value&a3=a3CustomValue',
{
// Allowed with simple boolean flag
a1: {
value: 'a1DefaultValue'
},
a2: true,
a3: {
value: 'a3DefaultValue'
}
}
);

expect(params.getAll(true)).toEqual({
a2: 'a2Value',
a3: 'a3CustomValue'
});
});

it('should allow values to be decoded when retrieved from the query string', () => {
const params = new SkyAppRuntimeConfigParams(
'?a=%2F',
Expand Down
15 changes: 13 additions & 2 deletions runtime/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function getUrlSearchParams(url: string): URLSearchParams {

export class SkyAppRuntimeConfigParams {
private params: { [key: string]: string } = {};
private defaultParamValues: { [key: string]: string } = {};
private requiredParams: string[] = [];
private encodedParams: string[] = [];

Expand Down Expand Up @@ -60,6 +61,7 @@ export class SkyAppRuntimeConfigParams {

if (paramValue) {
this.params[paramName] = paramValue;
this.defaultParamValues[paramName] = paramValue;
}
}
}
Expand Down Expand Up @@ -140,10 +142,19 @@ export class SkyAppRuntimeConfigParams {
/**
* Returns the params object
* @name getAll
* @param {boolean} excludeDefaults Exclude params that have default values
* @returns {Object}
*/
public getAll(): Object {
return this.params;
public getAll(excludeDefaults?: boolean): Object {
const filteredParams: { [key: string]: string} = {};

this.getAllKeys().forEach(key => {
if (!excludeDefaults || this.params[key] !== this.defaultParamValues[key]) {
filteredParams[key] = this.params[key];
}
});

return filteredParams;
}

/**
Expand Down