Ensure that when we check typeof x == 'number' we also check instanceof Number (#5752)

This commit is contained in:
Evert Timberg 2018-10-22 03:52:05 -04:00 committed by Simon Brunel
parent 81b4b87666
commit b3b5c7de4f
5 changed files with 30 additions and 3 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -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.

View File

@ -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,

View File

@ -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();