Skip to content

Commit

Permalink
feat(facade/collections): add own Object.assign ponyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
Hotell committed Jan 7, 2016
1 parent 93d1d25 commit 2b07b23
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/facade/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,37 @@ export class StringMapWrapper {
return true;
}

static assign<T,S>( target: T, ...sources: S[] ): T&S {

if ( !isPresent( target ) ) {
throw new TypeError( 'Object.assign cannot be called with null or undefined' );
}

const hasOwnProperty = Object.prototype.hasOwnProperty;

if ( (Object as any).assign ) {
return (Object as any).assign( target, ...sources );
}

let from;
const to = Object( target );

for ( var s = 0; s < sources.length; s++ ) {

from = Object( sources[ s ] );

for ( var key in from ) {
if ( hasOwnProperty.call( from, key ) ) {
to[ key ] = from[ key ];
}
}

}

return to;

}

}


Expand Down
39 changes: 39 additions & 0 deletions test/facade/collections.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,45 @@ describe( `facade/collections`, ()=> {

} );

describe( `#assign`, ()=> {

it( `should combine objects by extend`, ()=> {

const mutatedObj = {one:1};
const sourceOne = {two:2};
let actual = StringMapWrapper.assign( mutatedObj, sourceOne, { one: '111' } as any );
let expected = { one: '111', two: 2 };

expect(actual).to.deep.equal(expected);
expect( mutatedObj ).to.deep.equal( expected );
expect( sourceOne ).to.deep.equal( sourceOne );

const actual2 = StringMapWrapper.assign( {}, { foo: 'fooo', bar: 'baz' }, { twoo: 'phar' } as any );
const expected2 = { foo: 'fooo', bar: 'baz', twoo: 'phar' };
expect( actual2 ).to.deep.equal( expected2 );

} );

it( `should work as expected if Object.assign is not available`, ()=> {

const envAssign = (Object as any).assign;
(Object as any).assign = undefined;

const mutatedObj = {one:1};
const sourceOne = {two:2};
let actual = StringMapWrapper.assign( mutatedObj, sourceOne, { one: '111' } as any );
let expected = { one: '111', two: 2 };

expect(actual).to.deep.equal(expected);
expect( mutatedObj ).to.deep.equal( expected );
expect( sourceOne ).to.deep.equal( sourceOne );

(Object as any).assign = envAssign;

} );

} );

} );

describe( `ListWrapper`, ()=> {
Expand Down

0 comments on commit 2b07b23

Please sign in to comment.