Skip to content

Commit

Permalink
Input validation
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Jun 27, 2019
1 parent 9095a68 commit 3722785
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/helpers/helpers.math.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ var exports = {
var sqrt = Math.sqrt(value);
var i;

if (!Number.isInteger(value) || value <= 0) {
throw new Error('_factorize expects a positive integer, but received ' + value);
}

for (i = 1; i < sqrt; i++) {
if (value % i === 0) {
result.push(i);
Expand Down
6 changes: 6 additions & 0 deletions test/specs/helpers.math.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,11 @@ describe('Chart.helpers.math', function() {
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(function() {
factorize(-1);
}).toThrow(new Error('_factorize expects a positive integer, but received -1'));
expect(function() {
factorize(2.76);
}).toThrow(new Error('_factorize expects a positive integer, but received 2.76'));
});
});

0 comments on commit 3722785

Please sign in to comment.