]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Ensure that when we check typeof x == 'number' we also check instanceof Number (...
authorEvert Timberg <evert.timberg+github@gmail.com>
Mon, 22 Oct 2018 07:52:05 +0000 (03:52 -0400)
committerSimon Brunel <simonbrunel@users.noreply.github.com>
Mon, 22 Oct 2018 07:52:05 +0000 (09:52 +0200)
src/core/core.element.js
src/core/core.scale.js
src/helpers/helpers.core.js
src/plugins/plugin.filler.js
test/specs/helpers.core.tests.js

index 2ca60692b973d6e2063ecbae4399013299bc7bda..665f20c4302c005fdd9258b26bba851f1d02d666 100644 (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;
                        }
index 78ede6985f619c45acea468c9612adf66ecd1f55..363815e63b60edb623c78c191d8a209db15b3bfc 100644 (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
index 2a4b0098ccf7459acc2a30950bfaa706f7f94fa8..c22dff651b9f1d9101abeea02f89c2b4da94d83c 100644 (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.
index eb8dad4c3b0cb2224a1167b79217c8ec1a0db217..e89bb0d8785c6ba4556def180cc445699e56f6c6 100644 (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,
index 80b0640b2ccdcf07d943222da83ccebd752b5574..e3993de635a124573a6a940702e39643161c4623 100644 (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();