refactor(util): use Object.assign polyfill

This commit is contained in:
Adam Bradley
2016-01-11 11:17:55 -06:00
parent 2f42a55b12
commit b79d6cc8ea
10 changed files with 2520 additions and 2619 deletions

View File

@@ -1,5 +1,5 @@
import {CSS, rafFrames} from '../util/dom';
import {extend} from '../util/util';
import {assign} from '../util/util';
const doc: any = document;
@@ -53,7 +53,7 @@ export class Animation {
constructor(ele, opts={}) {
this.reset();
this._opts = extend({
this._opts = assign({
renderDelay: 16
}, opts);
@@ -516,7 +516,7 @@ export class Animation {
function copy(dest, src) {
// undo what stage() may have already done
extend(dest, src);
assign(dest, src);
dest._isFinished = dest._isStaged = dest.isProgress = false;
dest._chld = [];

View File

@@ -1,12 +1,12 @@
import {Menu} from './menu';
import {SlideEdgeGesture} from '../../gestures/slide-edge-gesture';
import * as util from '../../util';
import {assign} from '../../util/util';
export class MenuContentGesture extends SlideEdgeGesture {
constructor(menu: Menu, targetEl: Element, options = {}) {
super(targetEl, util.extend({
super(targetEl, assign({
direction: (menu.side === 'left' || menu.side === 'right') ? 'x' : 'y',
edge: menu.side,
threshold: 0,

View File

@@ -7,7 +7,7 @@
*/
import {Platform} from '../platform/platform';
import {isObject, isDefined, isFunction, isArray, extend} from '../util/util';
import {isObject, isDefined, isFunction, isArray} from '../util/util';
/**
* @name Config

View File

@@ -30,7 +30,7 @@ export class DragGesture extends Gesture {
this.dragging = false;
});
this.hammertime.get('pan').set(this._options);
//this.hammertime.get('pan').set(this._options);
}
onDrag() {}

View File

@@ -1,4 +1,4 @@
import {defaults, extend} from '../util';
import {defaults, assign} from '../util';
import {Hammer} from './hammer';
/**
@@ -28,7 +28,7 @@ export class Gesture {
}
options(opts = {}) {
extend(this._options, opts);
assign(this._options, opts);
}
on(type, cb) {

View File

File diff suppressed because it is too large Load Diff

View File

@@ -1,4 +1,4 @@
import {getQuerystring, extend} from '../util/util';
import {getQuerystring, assign} from '../util/util';
import {ready, windowDimensions, flushDimensionCache} from '../util/dom';
@@ -513,8 +513,8 @@ export class Platform {
// add the engine to the first in the platform hierarchy
// the original rootPlatformNode now becomes a child
// of the engineNode, which is not the new root
engineNode.child(rootPlatformNode);
rootPlatformNode.parent(engineNode);
engineNode.child = rootPlatformNode;
rootPlatformNode.parent = engineNode;
rootPlatformNode = engineNode;
// add any events which the engine would provide
@@ -522,21 +522,21 @@ export class Platform {
let engineMethods = engineNode.methods();
engineMethods._engineReady = engineMethods.ready;
delete engineMethods.ready;
extend(this, engineMethods);
assign(this, engineMethods);
}
let platformNode = rootPlatformNode;
while (platformNode) {
insertSuperset(platformNode);
platformNode = platformNode.child();
platformNode = platformNode.child;
}
// make sure the root noot is actually the root
// incase a node was inserted before the root
platformNode = rootPlatformNode.parent();
platformNode = rootPlatformNode.parent;
while (platformNode) {
rootPlatformNode = platformNode;
platformNode = platformNode.parent();
platformNode = platformNode.parent;
}
platformNode = rootPlatformNode;
@@ -549,7 +549,7 @@ export class Platform {
this._versions[platformNode.name()] = platformNode.version(this);
// go to the next platform child
platformNode = platformNode.child();
platformNode = platformNode.child;
}
}
@@ -566,14 +566,14 @@ export class Platform {
// use it's getRoot method to build up its hierarchy
// depending on which platforms match
let platformNode = new PlatformNode(platformName);
let rootNode = platformNode.getRoot(this, 0);
let rootNode = platformNode.getRoot(this);
if (rootNode) {
rootNode.depth = 0;
let childPlatform = rootNode.child();
let childPlatform = rootNode.child;
while (childPlatform) {
rootNode.depth++;
childPlatform = childPlatform.child();
childPlatform = childPlatform.child;
}
}
return rootNode;
@@ -587,20 +587,20 @@ function insertSuperset(platformNode) {
// add a platform in between two exist platforms
// so we can build the correct hierarchy of active platforms
let supersetPlatform = new PlatformNode(supersetPlaformName);
supersetPlatform.parent(platformNode.parent());
supersetPlatform.child(platformNode);
if (supersetPlatform.parent()) {
supersetPlatform.parent().child(supersetPlatform);
supersetPlatform.parent = platformNode.parent;
supersetPlatform.child = platformNode;
if (supersetPlatform.parent) {
supersetPlatform.parent.child = supersetPlatform;
}
platformNode.parent(supersetPlatform);
platformNode.parent = supersetPlatform;
}
}
class PlatformNode {
private c: any;
private _parent: PlatformNode;
private _child: PlatformNode;
public parent: PlatformNode;
public child: PlatformNode;
public isEngine: boolean;
constructor(platformName) {
@@ -624,20 +624,6 @@ class PlatformNode {
return this.c.methods || {};
}
parent(val) {
if (arguments.length) {
this._parent = val;
}
return this._parent;
}
child(val) {
if (arguments.length) {
this._child = val;
}
return this._child;
}
isMatch(p): boolean {
if (p.platformOverride && !this.isEngine) {
return (p.platformOverride === this.c.name);
@@ -678,11 +664,11 @@ class PlatformNode {
for (let i = 0; i < parents.length; i++) {
platform = new PlatformNode(parents[i]);
platform.child(this);
platform.child = this;
rootPlatform = platform.getRoot(p);
if (rootPlatform) {
this.parent(platform);
this.parent = platform;
return rootPlatform;
}
}

View File

@@ -1,6 +1,6 @@
import {StorageEngine} from './storage';
import {defaults, extend} from '../../util';
import {defaults, assign} from '../../util/util';
const DB_NAME :string = '__ionicstorage';
const win :any = window;
@@ -54,7 +54,7 @@ export class SqlStorage extends StorageEngine {
if (win.sqlitePlugin) {
let location = this._getBackupLocation(dbOptions.backupFlag);
this._db = win.sqlitePlugin.openDatabase(extend({
this._db = win.sqlitePlugin.openDatabase(assign({
name: dbOptions.name,
location: location,
createFromLocation: dbOptions.existingDatabase ? 1 : 0

View File

@@ -5,23 +5,10 @@ export function run() {
it('should extend simple', () => {
var obj = { a: '0', c: '0' };
expect( util.extend(obj, { a: '1', b: '2' }) ).toBe(obj);
expect( util.assign(obj, { a: '1', b: '2' }) ).toBe(obj);
expect(obj).toEqual({ a: '1', b: '2', c: '0' });
});
it('should extend complex', () => {
expect(util.extend(
{ a: '0', b: '0' },
{ b: '1', c: '1' },
{ c: '2', d: '2' }
)).toEqual({
a: '0',
b: '1',
c: '2',
d: '2'
});
});
});
describe('defaults', function() {
@@ -29,7 +16,7 @@ export function run() {
it('should simple defaults', () => {
var obj = { a: '1' };
expect(util.defaults(obj, { a: '2', b: '2' })).toBe(obj);
expect(obj).toEqual({
expect(obj).toEqual({
a: '1', b: '2'
});
});

View File

@@ -12,13 +12,26 @@ 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;
}
/**
* Extend the destination with an arbitrary number of other objects.
* @param dst the destination
* @param ... the param objects
* 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
*/
export function extend(dst: any, ...args: any[]) {
return _baseExtend(dst, [].slice.call(arguments, 1), false);
export function assign(target: any, source: any): any {
return _assign(target, source);
}
/**
@@ -30,7 +43,7 @@ export function merge(dst: any, ...args: any[]) {
return _baseExtend(dst, [].slice.call(arguments, 1), true);
}
function _baseExtend(dst, objs, deep) {
function _baseExtend(dst, objs, deep = false) {
for (var i = 0, ii = objs.length; i < ii; ++i) {
var obj = objs[i];
if (!obj || !isObject(obj) && !isFunction(obj)) continue;