Skip to content

Commit

Permalink
Assign: Adds Assign method deprecates merge and extend
Browse files Browse the repository at this point in the history
Assign is a polyfill of Object.assign
  • Loading branch information
runspired authored and arschmitz committed Dec 24, 2015
1 parent b2b491e commit fc01eae
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 35 deletions.
3 changes: 2 additions & 1 deletion src/expose.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
extend(Hammer, {
assign(Hammer, {
INPUT_START: INPUT_START,
INPUT_MOVE: INPUT_MOVE,
INPUT_END: INPUT_END,
Expand Down Expand Up @@ -45,6 +45,7 @@ extend(Hammer, {
each: each,
merge: merge,
extend: extend,
assign: assign,
inherit: inherit,
bindFn: bindFn,
prefixed: prefixed
Expand Down
5 changes: 2 additions & 3 deletions src/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 3 additions & 5 deletions src/recognizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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])) {
Expand Down
66 changes: 60 additions & 6 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.<anonymous>\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) {
Expand All @@ -81,7 +135,7 @@ function extend(dest, src, merge) {
i++;
}
return dest;
}
}, 'extend', 'Use `assign`.');

/**
* merge the values from src in the dest.
Expand All @@ -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
Expand All @@ -109,7 +163,7 @@ function inherit(child, base, properties) {
childP._super = baseP;

if (properties) {
extend(childP, properties);
assign(childP, properties);
}
}

Expand Down
File renamed without changes.
23 changes: 3 additions & 20 deletions tests/unit/test_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
),
Expand All @@ -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');
});
Expand Down

0 comments on commit fc01eae

Please sign in to comment.