Skip to content

Commit b3b5c7d

Browse files
etimbergsimonbrunel
authored andcommitted
Ensure that when we check typeof x == 'number' we also check instanceof Number (chartjs#5752)
1 parent 81b4b87 commit b3b5c7d

File tree

5 files changed

+30
-3
lines changed

5 files changed

+30
-3
lines changed

src/core/core.element.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function interpolate(start, view, model, ease) {
4242
continue;
4343
}
4444
}
45-
} else if (type === 'number' && isFinite(origin) && isFinite(target)) {
45+
} else if (helpers.isFinite(origin) && helpers.isFinite(target)) {
4646
view[key] = origin + (target - origin) * ease;
4747
continue;
4848
}

src/core/core.scale.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ module.exports = Element.extend({
519519
return NaN;
520520
}
521521
// isNaN(object) returns true, so make sure NaN is checking for a number; Discard Infinite values
522-
if (typeof rawValue === 'number' && !isFinite(rawValue)) {
522+
if ((typeof rawValue === 'number' || rawValue instanceof Number) && !isFinite(rawValue)) {
523523
return NaN;
524524
}
525525
// If it is in fact an object, dive in one more level

src/helpers/helpers.core.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ var helpers = {
5151
return value !== null && Object.prototype.toString.call(value) === '[object Object]';
5252
},
5353

54+
/**
55+
* Returns true if `value` is a finite number, else returns false
56+
* @param {*} value - The value to test.
57+
* @returns {Boolean}
58+
*/
59+
isFinite: function(value) {
60+
return (typeof value === 'number' || value instanceof Number) && isFinite(value);
61+
},
62+
5463
/**
5564
* Returns `value` if defined, else returns `defaultValue`.
5665
* @param {*} value - The value to return if defined.

src/plugins/plugin.filler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ function computeBoundary(source) {
128128
return target;
129129
}
130130

131-
if (typeof target === 'number' && isFinite(target)) {
131+
if (helpers.isFinite(target)) {
132132
horizontal = scale.isHorizontal();
133133
return {
134134
x: horizontal ? target : null,

test/specs/helpers.core.tests.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,24 @@ describe('Chart.helpers.core', function() {
5656
});
5757
});
5858

59+
describe('isFinite', function() {
60+
it('should return true if value is a finite number', function() {
61+
expect(helpers.isFinite(0)).toBeTruthy();
62+
// eslint-disable-next-line no-new-wrappers
63+
expect(helpers.isFinite(new Number(10))).toBeTruthy();
64+
});
65+
66+
it('should return false if the value is infinite', function() {
67+
expect(helpers.isFinite(Number.POSITIVE_INFINITY)).toBeFalsy();
68+
expect(helpers.isFinite(Number.NEGATIVE_INFINITY)).toBeFalsy();
69+
});
70+
71+
it('should return false if the value is not a number', function() {
72+
expect(helpers.isFinite('a')).toBeFalsy();
73+
expect(helpers.isFinite({})).toBeFalsy();
74+
});
75+
});
76+
5977
describe('isNullOrUndef', function() {
6078
it('should return true if value is null/undefined', function() {
6179
expect(helpers.isNullOrUndef(null)).toBeTruthy();

0 commit comments

Comments
 (0)