diff --git a/src/core/core.helpers.js b/src/core/core.helpers.js index 52c8aea68a8..6a4e2134b6a 100644 --- a/src/core/core.helpers.js +++ b/src/core/core.helpers.js @@ -100,19 +100,6 @@ module.exports = function() { } return x > 0 ? 1 : -1; }; - helpers.log10 = Math.log10 ? - function(x) { - return Math.log10(x); - } : - function(x) { - var exponent = Math.log(x) * Math.LOG10E; // Math.LOG10E = 1 / Math.LN10. - // Check for whole powers of 10, - // which due to floating point rounding error should be corrected. - var powerOf10 = Math.round(exponent); - var isPowerOf10 = x === Math.pow(10, powerOf10); - - return isPowerOf10 ? powerOf10 : exponent; - }; helpers.toRadians = function(degrees) { return degrees * (Math.PI / 180); }; diff --git a/src/helpers/helpers.math.js b/src/helpers/helpers.math.js new file mode 100644 index 00000000000..3d5d6ded389 --- /dev/null +++ b/src/helpers/helpers.math.js @@ -0,0 +1,32 @@ +'use strict'; + +var helpers = require('./helpers.core'); + +/** + * @alias Chart.helpers.math + * @namespace + */ +var exports = { + log10: Math.log10 || function(x) { + var exponent = Math.log(x) * Math.LOG10E; // Math.LOG10E = 1 / Math.LN10. + // Check for whole powers of 10, + // which due to floating point rounding error should be corrected. + var powerOf10 = Math.round(exponent); + var isPowerOf10 = x === Math.pow(10, powerOf10); + + return isPowerOf10 ? powerOf10 : exponent; + } +}; + +module.exports = exports; + +// DEPRECATIONS + +/** + * Provided for backward compatibility, use Chart.helpers.math.log10 instead. + * @namespace Chart.helpers.log10 + * @deprecated since version 2.9.0 + * @todo remove at version 3 + * @private + */ +helpers.log10 = exports.log10; diff --git a/src/helpers/index.js b/src/helpers/index.js index 60c199cad55..ef510076090 100644 --- a/src/helpers/index.js +++ b/src/helpers/index.js @@ -4,3 +4,4 @@ module.exports = require('./helpers.core'); module.exports.easing = require('./helpers.easing'); module.exports.canvas = require('./helpers.canvas'); module.exports.options = require('./helpers.options'); +module.exports.math = require('./helpers.math'); diff --git a/test/specs/core.helpers.tests.js b/test/specs/core.helpers.tests.js index 1f2bc29d5a1..e6ade2bd76a 100644 --- a/test/specs/core.helpers.tests.js +++ b/test/specs/core.helpers.tests.js @@ -26,16 +26,6 @@ describe('Core helper tests', function() { expect(helpers.sign(-5)).toBe(-1); }); - it('should do a log10 operation', function() { - expect(helpers.log10(0)).toBe(-Infinity); - - // Check all allowed powers of 10, which should return integer values - var maxPowerOf10 = Math.floor(helpers.log10(Number.MAX_VALUE)); - for (var i = 0; i < maxPowerOf10; i += 1) { - expect(helpers.log10(Math.pow(10, i))).toBe(i); - } - }); - it('should correctly determine if two numbers are essentially equal', function() { expect(helpers.almostEquals(0, Number.EPSILON, 2 * Number.EPSILON)).toBe(true); expect(helpers.almostEquals(1, 1.1, 0.0001)).toBe(false); diff --git a/test/specs/helpers.math.tests.js b/test/specs/helpers.math.tests.js new file mode 100644 index 00000000000..a2e17241d7f --- /dev/null +++ b/test/specs/helpers.math.tests.js @@ -0,0 +1,15 @@ +'use strict'; + +describe('Chart.helpers.math', function() { + var math = Chart.helpers.math; + + it('should do a log10 operation', function() { + expect(math.log10(0)).toBe(-Infinity); + + // Check all allowed powers of 10, which should return integer values + var maxPowerOf10 = Math.floor(math.log10(Number.MAX_VALUE)); + for (var i = 0; i < maxPowerOf10; i += 1) { + expect(math.log10(Math.pow(10, i))).toBe(i); + } + }); +});