From 201c2dde67b3e4c01ca1201d929be73d8d6bf4f0 Mon Sep 17 00:00:00 2001 From: Adam Bradley Date: Mon, 11 Jan 2016 11:44:28 -0600 Subject: [PATCH] fix(util): fix assign polyfill --- ionic/util/test/util.spec.ts | 13 +++++++++++++ ionic/util/util.ts | 24 ++++++++++-------------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/ionic/util/test/util.spec.ts b/ionic/util/test/util.spec.ts index c2a8d5a837..c3df1109b2 100644 --- a/ionic/util/test/util.spec.ts +++ b/ionic/util/test/util.spec.ts @@ -9,6 +9,19 @@ export function run() { expect(obj).toEqual({ a: '1', b: '2', c: '0' }); }); + it('should extend complex', () => { + expect(util.assign( + { a: '0', b: '0' }, + { b: '1', c: '1' }, + { c: '2', d: '2' } + )).toEqual({ + a: '0', + b: '1', + c: '2', + d: '2' + }); + }); + }); describe('defaults', function() { diff --git a/ionic/util/util.ts b/ionic/util/util.ts index 978a7b7e01..df512a6d8b 100644 --- a/ionic/util/util.ts +++ b/ionic/util/util.ts @@ -12,26 +12,22 @@ export function clamp(min, n, max) { return Math.max(min, Math.min(n, max)); } -// polyfill for Object.assign -var _assign: any; -if (typeof Object.assign !== 'function') { - // use the old-school shallow extend method - _assign = _baseExtend; -} else { - // use the built in ES6 Object.assign method - _assign = Object.assign; -} - /** * The assign() method is used to copy the values of all enumerable own * properties from one or more source objects to a target object. It will * return the target object. When available, this method will use * `Object.assign()` under-the-hood. * @param target The target object - * @param source The source object + * @param source(s) The source object */ -export function assign(target: any, source: any): any { - return _assign(target, source); +export function assign(...args: any[]): any { + if (typeof Object.assign !== 'function') { + // use the old-school shallow extend method + return _baseExtend(args[0], [].slice.call(args, 1), false); + } + + // use the built in ES6 Object.assign method + return Object.assign.apply(null, args); } /** @@ -43,7 +39,7 @@ export function merge(dst: any, ...args: any[]) { return _baseExtend(dst, [].slice.call(arguments, 1), true); } -function _baseExtend(dst, objs, deep = false) { +function _baseExtend(dst, objs, deep) { for (var i = 0, ii = objs.length; i < ii; ++i) { var obj = objs[i]; if (!obj || !isObject(obj) && !isFunction(obj)) continue;