Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
50 changes: 50 additions & 0 deletions src/lib/core/util/object-extend.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {extendObject} from './object-extend';


describe('extendObject', () => {
it('should extend an object', () => {
let extended = extendObject({}, {x: 123});

expect(extended).toEqual({x: 123});
});

it('should overwrite existing properties', () => {
let extended = extendObject({x: 456}, {x: 123});

expect(extended).toEqual({x: 123});
});

it('should add additional properties', () => {
let extended = extendObject({x: 456}, {y: 123});

expect(extended).toEqual({x: 456, y: 123});
});

it('should extend from multiple sources', () => {
let extended = extendObject({}, {x: 123}, {y: 456});

expect(extended).toEqual({x: 123, y: 456});
});

it('should overwrite properties from the later source', () => {
let extended = extendObject({}, {x: 123}, {x: 456});

expect(extended).toEqual({x: 456});
});

it('should treat null and undefined sources as empty objects', () => {
let extended = extendObject({}, null, {x: 123}, undefined, {y: 456});

expect(extended).toEqual({x: 123, y: 456});
});

it('should throw an error when the dest object is null', () => {
expect(() => extendObject(null, {x: 123}))
.toThrowError('Cannot convert undefined or null to object');
});

it('should throw an error when the dest object is undefined', () => {
expect(() => extendObject(undefined, {x: 123}))
.toThrowError('Cannot convert undefined or null to object');
});
});
24 changes: 24 additions & 0 deletions src/lib/core/util/object-extend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Extends an object with the *enumerable* and *own* properties of one or more source objects,
* similar to Object.assign.
*
* @param dest The object which will have properties copied to it.
* @param sources The source objects from which properties will be copied.
*/
export function extendObject(dest: any, ...sources: any[]): any {
if (dest == null) {
throw TypeError('Cannot convert undefined or null to object');
}

for (let source of sources) {
if (source != null) {
for (let key in source) {
if (source.hasOwnProperty(key)) {
dest[key] = source[key];
}
}
}
}

return dest;
}
19 changes: 10 additions & 9 deletions src/lib/dialog/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {MdDialogRef} from './dialog-ref';
import {DialogInjector} from './dialog-injector';
import {MdDialogContainer} from './dialog-container';
import {A11yModule, InteractivityChecker} from '../core';
import {extendObject} from '../core/util/object-extend';

export {MdDialogConfig} from './dialog-config';
export {MdDialogRef} from './dialog-ref';
Expand Down Expand Up @@ -41,7 +42,7 @@ export class MdDialog {
* @param config
*/
open<T>(component: ComponentType<T>, config?: MdDialogConfig): MdDialogRef<T> {
config = this._applyConfigDefaults(config);
config = _applyConfigDefaults(config);

let overlayRef = this._createOverlay(config);
let dialogContainer = this._attachDialogContainer(overlayRef, config);
Expand Down Expand Up @@ -127,15 +128,15 @@ export class MdDialog {

return state;
}
}

/**
* Applies default options to the dialog config.
* @param dialogConfig Config to be modified.
* @returns The new configuration object.
*/
private _applyConfigDefaults(dialogConfig: MdDialogConfig): MdDialogConfig {
return Object.assign(new MdDialogConfig(), dialogConfig);
}
/**
* Applies default options to the dialog config.
* @param dialogConfig Config to be modified.
* @returns The new configuration object.
*/
function _applyConfigDefaults(dialogConfig: MdDialogConfig): MdDialogConfig {
return extendObject(new MdDialogConfig(), dialogConfig);
}


Expand Down
3 changes: 2 additions & 1 deletion src/lib/snack-bar/snack-bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {MdSnackBarConfig} from './snack-bar-config';
import {MdSnackBarRef} from './snack-bar-ref';
import {MdSnackBarContainer} from './snack-bar-container';
import {SimpleSnackBar} from './simple-snack-bar';
import {extendObject} from '../core/util/object-extend';

// TODO(josephperrott): Automate dismiss after timeout.

Expand Down Expand Up @@ -124,7 +125,7 @@ export class MdSnackBar {
* @returns The new configuration object with defaults applied.
*/
function _applyConfigDefaults(config: MdSnackBarConfig): MdSnackBarConfig {
return Object.assign(new MdSnackBarConfig(), config);
return extendObject(new MdSnackBarConfig(), config);
}


Expand Down