forked from chartjs/Chart.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add helpers.math._factorize (chartjs#6360)
* Add helpers.math._factorize * Remove duplicate test statement
- Loading branch information
Showing
4 changed files
with
60 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict'; | ||
|
||
/** | ||
* @alias Chart.helpers.math | ||
* @namespace | ||
*/ | ||
var exports = { | ||
/** | ||
* Returns an array of factors sorted from 1 to sqrt(value) | ||
* @private | ||
*/ | ||
_factorize: function(value) { | ||
var result = []; | ||
var sqrt = Math.sqrt(value); | ||
var i; | ||
|
||
for (i = 1; i < sqrt; i++) { | ||
if (value % i === 0) { | ||
result.push(i); | ||
result.push(value / i); | ||
} | ||
} | ||
if (sqrt === (sqrt | 0)) { // if value is a square number | ||
result.push(sqrt); | ||
} | ||
|
||
result.sort(function(a, b) { | ||
return a - b; | ||
}).pop(); | ||
return result; | ||
} | ||
}; | ||
|
||
module.exports = exports; |
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,16 @@ | ||
'use strict'; | ||
|
||
describe('Chart.helpers.math', function() { | ||
var factorize = Chart.helpers.math._factorize; | ||
|
||
it('should factorize', function() { | ||
expect(factorize(1000)).toEqual([1, 2, 4, 5, 8, 10, 20, 25, 40, 50, 100, 125, 200, 250, 500]); | ||
expect(factorize(60)).toEqual([1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30]); | ||
expect(factorize(30)).toEqual([1, 2, 3, 5, 6, 10, 15]); | ||
expect(factorize(24)).toEqual([1, 2, 3, 4, 6, 8, 12]); | ||
expect(factorize(12)).toEqual([1, 2, 3, 4, 6]); | ||
expect(factorize(4)).toEqual([1, 2]); | ||
expect(factorize(-1)).toEqual([]); | ||
expect(factorize(2.76)).toEqual([]); | ||
}); | ||
}); |