Skip to content

Commit fc01eae

Browse files
runspiredarschmitz
authored andcommitted
Assign: Adds Assign method deprecates merge and extend
Assign is a polyfill of Object.assign
1 parent b2b491e commit fc01eae

File tree

6 files changed

+70
-35
lines changed

6 files changed

+70
-35
lines changed

src/expose.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
extend(Hammer, {
1+
assign(Hammer, {
22
INPUT_START: INPUT_START,
33
INPUT_MOVE: INPUT_MOVE,
44
INPUT_END: INPUT_END,
@@ -45,6 +45,7 @@ extend(Hammer, {
4545
each: each,
4646
merge: merge,
4747
extend: extend,
48+
assign: assign,
4849
inherit: inherit,
4950
bindFn: bindFn,
5051
prefixed: prefixed

src/manager.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ var FORCED_STOP = 2;
88
* @constructor
99
*/
1010
function Manager(element, options) {
11-
var newOptions = options ? extend({}, options) : {};
12-
this.options = merge(newOptions, Hammer.defaults);
11+
this.options = assign({}, Hammer.defaults, options || {});
1312

1413
this.options.inputTarget = this.options.inputTarget || element;
1514

@@ -37,7 +36,7 @@ Manager.prototype = {
3736
* @returns {Manager}
3837
*/
3938
set: function(options) {
40-
extend(this.options, options);
39+
assign(this.options, options);
4140

4241
// Options that need a little more setup
4342
if (options.touchAction) {

src/recognizer.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,11 @@ var STATE_FAILED = 32;
4040
* @param {Object} options
4141
*/
4242
function Recognizer(options) {
43-
// make sure, options are copied over to a new object to prevent leaking it outside
44-
options = extend({}, options || {});
43+
this.options = assign({}, this.defaults, options || {});
4544

4645
this.id = uniqueId();
4746

4847
this.manager = null;
49-
this.options = merge(options, this.defaults);
5048

5149
// default is enable true
5250
this.options.enable = ifUndefined(this.options.enable, true);
@@ -70,7 +68,7 @@ Recognizer.prototype = {
7068
* @return {Recognizer}
7169
*/
7270
set: function(options) {
73-
extend(this.options, options);
71+
assign(this.options, options);
7472

7573
// also update the touchAction, in case something changed about the directions/enabled state
7674
this.manager && this.manager.touchAction.update();
@@ -231,7 +229,7 @@ Recognizer.prototype = {
231229
recognize: function(inputData) {
232230
// make a new copy of the inputData
233231
// so we can change the inputData without messing up the other recognizers
234-
var inputDataClone = extend({}, inputData);
232+
var inputDataClone = assign({}, inputData);
235233

236234
// is is enabled and allow recognizing?
237235
if (!boolOrFn(this.options.enable, [this, inputDataClone])) {

src/utils.js

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,69 @@ function each(obj, iterator, context) {
6363
}
6464
}
6565

66+
/**
67+
* wrap a method with a deprecation warning and stack trace
68+
* @param {Function} method
69+
* @param {String} name
70+
* @param {String} message
71+
* @returns {Function} A new function wrapping the supplied method.
72+
*/
73+
function deprecate(method, name, message) {
74+
var deprecationMessage = 'DEPRECATED METHOD: ' + name + '\n' + message + ' AT \n';
75+
return function() {
76+
var e = new Error('get-stack-trace');
77+
var stack = e && e.stack ? e.stack.replace(/^[^\(]+?[\n$]/gm, '')
78+
.replace(/^\s+at\s+/gm, '')
79+
.replace(/^Object.<anonymous>\s*\(/gm, '{anonymous}()@') : 'Unknown Stack Trace';
80+
81+
var log = window.console && (window.console.warn || window.console.log);
82+
if (log) {
83+
log.call(window.console, deprecationMessage, stack);
84+
}
85+
return method.apply(this, arguments);
86+
};
87+
}
88+
89+
/**
90+
* extend object.
91+
* means that properties in dest will be overwritten by the ones in src.
92+
* @param {Object} target
93+
* @param {...Object} objects_to_assign
94+
* @returns {Object} target
95+
*/
96+
var assign;
97+
if (typeof Object.assign !== 'function') {
98+
assign = function assign(target) {
99+
if (target === undefined || target === null) {
100+
throw new TypeError('Cannot convert undefined or null to object');
101+
}
102+
103+
var output = Object(target);
104+
for (var index = 1; index < arguments.length; index++) {
105+
var source = arguments[index];
106+
if (source !== undefined && source !== null) {
107+
for (var nextKey in source) {
108+
if (source.hasOwnProperty(nextKey)) {
109+
output[nextKey] = source[nextKey];
110+
}
111+
}
112+
}
113+
}
114+
return output;
115+
};
116+
} else {
117+
assign = Object.assign;
118+
}
119+
66120
/**
67121
* extend object.
68122
* means that properties in dest will be overwritten by the ones in src.
69123
* @param {Object} dest
70124
* @param {Object} src
71-
* @param {Boolean} [merge]
125+
* @param {Boolean=false} [merge]
72126
* @returns {Object} dest
73127
*/
74-
function extend(dest, src, merge) {
128+
var extend = deprecate(function extend(dest, src, merge) {
75129
var keys = Object.keys(src);
76130
var i = 0;
77131
while (i < keys.length) {
@@ -81,7 +135,7 @@ function extend(dest, src, merge) {
81135
i++;
82136
}
83137
return dest;
84-
}
138+
}, 'extend', 'Use `assign`.');
85139

86140
/**
87141
* merge the values from src in the dest.
@@ -90,9 +144,9 @@ function extend(dest, src, merge) {
90144
* @param {Object} src
91145
* @returns {Object} dest
92146
*/
93-
function merge(dest, src) {
147+
var merge = deprecate(function merge(dest, src) {
94148
return extend(dest, src, true);
95-
}
149+
}, 'merge', 'Use `assign`.');
96150

97151
/**
98152
* simple class inheritance
@@ -109,7 +163,7 @@ function inherit(child, base, properties) {
109163
childP._super = baseP;
110164

111165
if (properties) {
112-
extend(childP, properties);
166+
assign(childP, properties);
113167
}
114168
}
115169

tests/unit/test_utils.js

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,10 @@ test('each', function() {
130130
ok(loop == 3, 'array loop without Array.forEach');
131131
});
132132

133-
test('extend', function() {
133+
test('assign', function() {
134134
expect(2);
135135
deepEqual(
136-
$H.extend(
136+
$H.assign(
137137
{a: 1, b: 3},
138138
{b: 2, c: 3}
139139
),
@@ -142,24 +142,7 @@ test('extend', function() {
142142
);
143143

144144
var src = { foo: true };
145-
var dest = $H.extend({}, src);
146-
src.foo = false;
147-
deepEqual(dest, {foo: true}, 'Clone reference');
148-
});
149-
150-
test('merge', function() {
151-
expect(2);
152-
deepEqual(
153-
$H.merge(
154-
{a: 1, b: 3},
155-
{b: 2, c: 3}
156-
),
157-
{a: 1, b: 3, c: 3},
158-
'Simple extend'
159-
);
160-
161-
var src = { foo: true };
162-
var dest = $H.merge({ foo: true }, src);
145+
var dest = $H.assign({}, src);
163146
src.foo = false;
164147
deepEqual(dest, {foo: true}, 'Clone reference');
165148
});

0 commit comments

Comments
 (0)