Move extend and inherits helpers in helpers.core.js (#4878)

Fix Rollup issue caused by early access of the `extend` and `inherits` helpers not yet part of the `helpers/index` import. Also added (basic) unit tests for whose methods.
This commit is contained in:
Simon Brunel
2017-10-24 19:11:40 +02:00
committed by GitHub
parent c81a55fed1
commit 26c1936dee
4 changed files with 95 additions and 52 deletions

View File

@@ -10,16 +10,6 @@ module.exports = function(Chart) {
// -- Basic js utility methods
helpers.extend = function(base) {
var setFn = function(value, key) {
base[key] = value;
};
for (var i = 1, ilen = arguments.length; i < ilen; i++) {
helpers.each(arguments[i], setFn);
}
return base;
};
helpers.configMerge = function(/* objects ... */) {
return helpers.merge(helpers.clone(arguments[0]), [].slice.call(arguments, 1), {
merger: function(key, target, source, options) {
@@ -125,29 +115,7 @@ module.exports = function(Chart) {
}
}
};
helpers.inherits = function(extensions) {
// Basic javascript inheritance based on the model created in Backbone.js
var me = this;
var ChartElement = (extensions && extensions.hasOwnProperty('constructor')) ? extensions.constructor : function() {
return me.apply(this, arguments);
};
var Surrogate = function() {
this.constructor = ChartElement;
};
Surrogate.prototype = me.prototype;
ChartElement.prototype = new Surrogate();
ChartElement.extend = helpers.inherits;
if (extensions) {
helpers.extend(ChartElement.prototype, extensions);
}
ChartElement.__super__ = me.prototype;
return ChartElement;
};
// -- Math methods
helpers.isNumber = function(n) {
return !isNaN(parseFloat(n)) && isFinite(n);

View File

@@ -250,6 +250,48 @@ var helpers = {
*/
mergeIf: function(target, source) {
return helpers.merge(target, source, {merger: helpers._mergerIf});
},
/**
* Applies the contents of two or more objects together into the first object.
* @param {Object} target - The target object in which all objects are merged into.
* @param {Object} arg1 - Object containing additional properties to merge in target.
* @param {Object} argN - Additional objects containing properties to merge in target.
* @returns {Object} The `target` object.
*/
extend: function(target) {
var setFn = function(value, key) {
target[key] = value;
};
for (var i = 1, ilen = arguments.length; i < ilen; ++i) {
helpers.each(arguments[i], setFn);
}
return target;
},
/**
* Basic javascript inheritance based on the model created in Backbone.js
*/
inherits: function(extensions) {
var me = this;
var ChartElement = (extensions && extensions.hasOwnProperty('constructor')) ? extensions.constructor : function() {
return me.apply(this, arguments);
};
var Surrogate = function() {
this.constructor = ChartElement;
};
Surrogate.prototype = me.prototype;
ChartElement.prototype = new Surrogate();
ChartElement.extend = helpers.inherits;
if (extensions) {
helpers.extend(ChartElement.prototype, extensions);
}
ChartElement.__super__ = me.prototype;
return ChartElement;
}
};