Skip to content

Commit 52e5cb5

Browse files
authored
chore: don't use Object.assign (#1791)
1 parent f525db1 commit 52e5cb5

File tree

4 files changed

+86
-10
lines changed

4 files changed

+86
-10
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import {extendObject} from './object-extend';
2+
3+
4+
describe('extendObject', () => {
5+
it('should extend an object', () => {
6+
let extended = extendObject({}, {x: 123});
7+
8+
expect(extended).toEqual({x: 123});
9+
});
10+
11+
it('should overwrite existing properties', () => {
12+
let extended = extendObject({x: 456}, {x: 123});
13+
14+
expect(extended).toEqual({x: 123});
15+
});
16+
17+
it('should add additional properties', () => {
18+
let extended = extendObject({x: 456}, {y: 123});
19+
20+
expect(extended).toEqual({x: 456, y: 123});
21+
});
22+
23+
it('should extend from multiple sources', () => {
24+
let extended = extendObject({}, {x: 123}, {y: 456});
25+
26+
expect(extended).toEqual({x: 123, y: 456});
27+
});
28+
29+
it('should overwrite properties from the later source', () => {
30+
let extended = extendObject({}, {x: 123}, {x: 456});
31+
32+
expect(extended).toEqual({x: 456});
33+
});
34+
35+
it('should treat null and undefined sources as empty objects', () => {
36+
let extended = extendObject({}, null, {x: 123}, undefined, {y: 456});
37+
38+
expect(extended).toEqual({x: 123, y: 456});
39+
});
40+
41+
it('should throw an error when the dest object is null', () => {
42+
expect(() => extendObject(null, {x: 123}))
43+
.toThrowError('Cannot convert undefined or null to object');
44+
});
45+
46+
it('should throw an error when the dest object is undefined', () => {
47+
expect(() => extendObject(undefined, {x: 123}))
48+
.toThrowError('Cannot convert undefined or null to object');
49+
});
50+
});

src/lib/core/util/object-extend.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Extends an object with the *enumerable* and *own* properties of one or more source objects,
3+
* similar to Object.assign.
4+
*
5+
* @param dest The object which will have properties copied to it.
6+
* @param sources The source objects from which properties will be copied.
7+
*/
8+
export function extendObject(dest: any, ...sources: any[]): any {
9+
if (dest == null) {
10+
throw TypeError('Cannot convert undefined or null to object');
11+
}
12+
13+
for (let source of sources) {
14+
if (source != null) {
15+
for (let key in source) {
16+
if (source.hasOwnProperty(key)) {
17+
dest[key] = source[key];
18+
}
19+
}
20+
}
21+
}
22+
23+
return dest;
24+
}

src/lib/dialog/dialog.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {MdDialogRef} from './dialog-ref';
1414
import {DialogInjector} from './dialog-injector';
1515
import {MdDialogContainer} from './dialog-container';
1616
import {A11yModule, InteractivityChecker} from '../core';
17+
import {extendObject} from '../core/util/object-extend';
1718

1819
export {MdDialogConfig} from './dialog-config';
1920
export {MdDialogRef} from './dialog-ref';
@@ -41,7 +42,7 @@ export class MdDialog {
4142
* @param config
4243
*/
4344
open<T>(component: ComponentType<T>, config?: MdDialogConfig): MdDialogRef<T> {
44-
config = this._applyConfigDefaults(config);
45+
config = _applyConfigDefaults(config);
4546

4647
let overlayRef = this._createOverlay(config);
4748
let dialogContainer = this._attachDialogContainer(overlayRef, config);
@@ -127,15 +128,15 @@ export class MdDialog {
127128

128129
return state;
129130
}
131+
}
130132

131-
/**
132-
* Applies default options to the dialog config.
133-
* @param dialogConfig Config to be modified.
134-
* @returns The new configuration object.
135-
*/
136-
private _applyConfigDefaults(dialogConfig: MdDialogConfig): MdDialogConfig {
137-
return Object.assign(new MdDialogConfig(), dialogConfig);
138-
}
133+
/**
134+
* Applies default options to the dialog config.
135+
* @param dialogConfig Config to be modified.
136+
* @returns The new configuration object.
137+
*/
138+
function _applyConfigDefaults(dialogConfig: MdDialogConfig): MdDialogConfig {
139+
return extendObject(new MdDialogConfig(), dialogConfig);
139140
}
140141

141142

src/lib/snack-bar/snack-bar.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {MdSnackBarConfig} from './snack-bar-config';
2020
import {MdSnackBarRef} from './snack-bar-ref';
2121
import {MdSnackBarContainer} from './snack-bar-container';
2222
import {SimpleSnackBar} from './simple-snack-bar';
23+
import {extendObject} from '../core/util/object-extend';
2324

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

@@ -124,7 +125,7 @@ export class MdSnackBar {
124125
* @returns The new configuration object with defaults applied.
125126
*/
126127
function _applyConfigDefaults(config: MdSnackBarConfig): MdSnackBarConfig {
127-
return Object.assign(new MdSnackBarConfig(), config);
128+
return extendObject(new MdSnackBarConfig(), config);
128129
}
129130

130131

0 commit comments

Comments
 (0)