Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move log10 from core.helpers to helpers.math
- Loading branch information
Showing
5 changed files
with
48 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
}); | ||
}); |