Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core/core.element.js
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
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
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
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
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