Skip to content

Commit

Permalink
Helper function + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
etimberg committed Oct 21, 2018
1 parent edd207f commit bd27842
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/helpers/helpers.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ var helpers = {
return value !== null && Object.prototype.toString.call(value) === '[object Object]';
},

/**
* Returns true if `value` is a finite number, else returns false
* @param {*} value - The value to test.
* @returns {Boolean}
*/
isFinite: function(value) {
return (typeof value === 'number' || value instanceof Number) && isFinite(value);
},

/**
* Returns `value` if defined, else returns `defaultValue`.
* @param {*} value - The value to return if defined.
Expand Down
18 changes: 18 additions & 0 deletions test/specs/helpers.core.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@ describe('Chart.helpers.core', function() {
});
});

describe('isFinite', function() {
it('should return true if value is a finite number', function() {
expect(helpers.isFinite(0)).toBeTruthy();
// eslint-disable-next-line no-new-wrappers
expect(helpers.isFinite(new Number(10))).toBeTruthy();
});

it('should return false if the value is infinite', function() {
expect(helpers.isFinite(Number.POSITIVE_INFINITY)).toBeFalsy();
expect(helpers.isFinite(Number.NEGATIVE_INFINITY)).toBeFalsy();
});

it('should return false if the value is not a number', function() {
expect(helpers.isFinite('a')).toBeFalsy();
expect(helpers.isFinite({})).toBeFalsy();
});
});

describe('isNullOrUndef', function() {
it('should return true if value is null/undefined', function() {
expect(helpers.isNullOrUndef(null)).toBeTruthy();
Expand Down

0 comments on commit bd27842

Please sign in to comment.