From fc01eaea678acc430c664eb374555fbe3d403bdd Mon Sep 17 00:00:00 2001 From: Chris Thoburn Date: Wed, 23 Dec 2015 09:48:34 -0600 Subject: [PATCH] Assign: Adds Assign method deprecates merge and extend Assign is a polyfill of Object.assign --- src/expose.js | 3 +- src/manager.js | 5 +- src/recognizer.js | 8 +-- src/utils.js | 66 +++++++++++++++++-- ...pagation.js => test_propagation_bubble.js} | 0 tests/unit/test_utils.js | 23 +------ 6 files changed, 70 insertions(+), 35 deletions(-) rename tests/unit/{test_propagation.js => test_propagation_bubble.js} (100%) diff --git a/src/expose.js b/src/expose.js index ebba4ec4b..1fe14db25 100644 --- a/src/expose.js +++ b/src/expose.js @@ -1,4 +1,4 @@ -extend(Hammer, { +assign(Hammer, { INPUT_START: INPUT_START, INPUT_MOVE: INPUT_MOVE, INPUT_END: INPUT_END, @@ -45,6 +45,7 @@ extend(Hammer, { each: each, merge: merge, extend: extend, + assign: assign, inherit: inherit, bindFn: bindFn, prefixed: prefixed diff --git a/src/manager.js b/src/manager.js index 522c3682f..175b8947b 100644 --- a/src/manager.js +++ b/src/manager.js @@ -8,8 +8,7 @@ var FORCED_STOP = 2; * @constructor */ function Manager(element, options) { - var newOptions = options ? extend({}, options) : {}; - this.options = merge(newOptions, Hammer.defaults); + this.options = assign({}, Hammer.defaults, options || {}); this.options.inputTarget = this.options.inputTarget || element; @@ -37,7 +36,7 @@ Manager.prototype = { * @returns {Manager} */ set: function(options) { - extend(this.options, options); + assign(this.options, options); // Options that need a little more setup if (options.touchAction) { diff --git a/src/recognizer.js b/src/recognizer.js index 7c159c91e..b0dd9fa04 100644 --- a/src/recognizer.js +++ b/src/recognizer.js @@ -40,13 +40,11 @@ var STATE_FAILED = 32; * @param {Object} options */ function Recognizer(options) { - // make sure, options are copied over to a new object to prevent leaking it outside - options = extend({}, options || {}); + this.options = assign({}, this.defaults, options || {}); this.id = uniqueId(); this.manager = null; - this.options = merge(options, this.defaults); // default is enable true this.options.enable = ifUndefined(this.options.enable, true); @@ -70,7 +68,7 @@ Recognizer.prototype = { * @return {Recognizer} */ set: function(options) { - extend(this.options, options); + assign(this.options, options); // also update the touchAction, in case something changed about the directions/enabled state this.manager && this.manager.touchAction.update(); @@ -231,7 +229,7 @@ Recognizer.prototype = { recognize: function(inputData) { // make a new copy of the inputData // so we can change the inputData without messing up the other recognizers - var inputDataClone = extend({}, inputData); + var inputDataClone = assign({}, inputData); // is is enabled and allow recognizing? if (!boolOrFn(this.options.enable, [this, inputDataClone])) { diff --git a/src/utils.js b/src/utils.js index 8697d0b9f..94d57cf40 100644 --- a/src/utils.js +++ b/src/utils.js @@ -63,15 +63,69 @@ function each(obj, iterator, context) { } } +/** + * wrap a method with a deprecation warning and stack trace + * @param {Function} method + * @param {String} name + * @param {String} message + * @returns {Function} A new function wrapping the supplied method. + */ +function deprecate(method, name, message) { + var deprecationMessage = 'DEPRECATED METHOD: ' + name + '\n' + message + ' AT \n'; + return function() { + var e = new Error('get-stack-trace'); + var stack = e && e.stack ? e.stack.replace(/^[^\(]+?[\n$]/gm, '') + .replace(/^\s+at\s+/gm, '') + .replace(/^Object.\s*\(/gm, '{anonymous}()@') : 'Unknown Stack Trace'; + + var log = window.console && (window.console.warn || window.console.log); + if (log) { + log.call(window.console, deprecationMessage, stack); + } + return method.apply(this, arguments); + }; +} + +/** + * extend object. + * means that properties in dest will be overwritten by the ones in src. + * @param {Object} target + * @param {...Object} objects_to_assign + * @returns {Object} target + */ +var assign; +if (typeof Object.assign !== 'function') { + assign = function assign(target) { + if (target === undefined || target === null) { + throw new TypeError('Cannot convert undefined or null to object'); + } + + var output = Object(target); + for (var index = 1; index < arguments.length; index++) { + var source = arguments[index]; + if (source !== undefined && source !== null) { + for (var nextKey in source) { + if (source.hasOwnProperty(nextKey)) { + output[nextKey] = source[nextKey]; + } + } + } + } + return output; + }; +} else { + assign = Object.assign; +} + /** * extend object. * means that properties in dest will be overwritten by the ones in src. * @param {Object} dest * @param {Object} src - * @param {Boolean} [merge] + * @param {Boolean=false} [merge] * @returns {Object} dest */ -function extend(dest, src, merge) { +var extend = deprecate(function extend(dest, src, merge) { var keys = Object.keys(src); var i = 0; while (i < keys.length) { @@ -81,7 +135,7 @@ function extend(dest, src, merge) { i++; } return dest; -} +}, 'extend', 'Use `assign`.'); /** * merge the values from src in the dest. @@ -90,9 +144,9 @@ function extend(dest, src, merge) { * @param {Object} src * @returns {Object} dest */ -function merge(dest, src) { +var merge = deprecate(function merge(dest, src) { return extend(dest, src, true); -} +}, 'merge', 'Use `assign`.'); /** * simple class inheritance @@ -109,7 +163,7 @@ function inherit(child, base, properties) { childP._super = baseP; if (properties) { - extend(childP, properties); + assign(childP, properties); } } diff --git a/tests/unit/test_propagation.js b/tests/unit/test_propagation_bubble.js similarity index 100% rename from tests/unit/test_propagation.js rename to tests/unit/test_propagation_bubble.js diff --git a/tests/unit/test_utils.js b/tests/unit/test_utils.js index 21be58c0e..f9d5f80b3 100644 --- a/tests/unit/test_utils.js +++ b/tests/unit/test_utils.js @@ -130,10 +130,10 @@ test('each', function() { ok(loop == 3, 'array loop without Array.forEach'); }); -test('extend', function() { +test('assign', function() { expect(2); deepEqual( - $H.extend( + $H.assign( {a: 1, b: 3}, {b: 2, c: 3} ), @@ -142,24 +142,7 @@ test('extend', function() { ); var src = { foo: true }; - var dest = $H.extend({}, src); - src.foo = false; - deepEqual(dest, {foo: true}, 'Clone reference'); -}); - -test('merge', function() { - expect(2); - deepEqual( - $H.merge( - {a: 1, b: 3}, - {b: 2, c: 3} - ), - {a: 1, b: 3, c: 3}, - 'Simple extend' - ); - - var src = { foo: true }; - var dest = $H.merge({ foo: true }, src); + var dest = $H.assign({}, src); src.foo = false; deepEqual(dest, {foo: true}, 'Clone reference'); });