Skip to content

Commit

Permalink
Ensure that when we check typeof x == 'number' we also check instance…
Browse files Browse the repository at this point in the history
…of Number (#5752)
  • Loading branch information
etimberg authored and simonbrunel committed Oct 22, 2018
1 parent 81b4b87 commit b3b5c7d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/core/core.element.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function interpolate(start, view, model, ease) {
continue;
}
}
} else if (type === 'number' && isFinite(origin) && isFinite(target)) {
} else if (helpers.isFinite(origin) && helpers.isFinite(target)) {
view[key] = origin + (target - origin) * ease;
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/core.scale.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ module.exports = Element.extend({
return NaN;
}
// isNaN(object) returns true, so make sure NaN is checking for a number; Discard Infinite values
if (typeof rawValue === 'number' && !isFinite(rawValue)) {
if ((typeof rawValue === 'number' || rawValue instanceof Number) && !isFinite(rawValue)) {
return NaN;
}
// If it is in fact an object, dive in one more level
Expand Down
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
2 changes: 1 addition & 1 deletion src/plugins/plugin.filler.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function computeBoundary(source) {
return target;
}

if (typeof target === 'number' && isFinite(target)) {
if (helpers.isFinite(target)) {
horizontal = scale.isHorizontal();
return {
x: horizontal ? target : null,
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 b3b5c7d

Please sign in to comment.