Skip to content

Commit

Permalink
Move log10 from core.helpers to helpers.math
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed Jun 18, 2019
1 parent 70b32ff commit b2aa8c7
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 23 deletions.
13 changes: 0 additions & 13 deletions src/core/core.helpers.js
Expand Up @@ -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);
};
Expand Down
32 changes: 32 additions & 0 deletions 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;
1 change: 1 addition & 0 deletions src/helpers/index.js
Expand Up @@ -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');
10 changes: 0 additions & 10 deletions test/specs/core.helpers.tests.js
Expand Up @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions 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);
}
});
});

0 comments on commit b2aa8c7

Please sign in to comment.