]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Add support for typed arrays (#5905)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Tue, 18 Dec 2018 13:49:39 +0000 (15:49 +0200)
committerSimon Brunel <simonbrunel@users.noreply.github.com>
Tue, 18 Dec 2018 13:49:39 +0000 (14:49 +0100)
src/helpers/helpers.core.js
test/.eslintrc.yml
test/specs/helpers.core.tests.js

index c2bd132727cabf791153b167ce8c6b372616428b..4464eb7baba7e735a187b658cec847dd49ecdfd0 100644 (file)
@@ -32,13 +32,20 @@ var helpers = {
        },
 
        /**
-        * Returns true if `value` is an array, else returns false.
+        * Returns true if `value` is an array (including typed arrays), else returns false.
         * @param {*} value - The value to test.
         * @returns {Boolean}
         * @function
         */
-       isArray: Array.isArray ? Array.isArray : function(value) {
-               return Object.prototype.toString.call(value) === '[object Array]';
+       isArray: function(value) {
+               if (Array.isArray && Array.isArray(value)) {
+                       return true;
+               }
+               var type = Object.prototype.toString.call(value);
+               if (type.substr(0, 7) === '[object' && type.substr(-6) === 'Array]') {
+                       return true;
+               }
+               return false;
        },
 
        /**
index 5fa01f891613bef2433365ad99db4772bd7f0e71..fc54c25facb5c731bb9212af0a808edd10b39e80 100644 (file)
@@ -1,4 +1,8 @@
+parserOptions:
+  ecmaVersion: 5 # don't rely on default, since its changed by env: es6
+
 env:
+  es6: true # also changes default ecmaVersion to 6
   jasmine: true
 
 globals:
index e3993de635a124573a6a940702e39643161c4623..cdda01b967413cc1423451a2a1d5d60661ab9e50 100644 (file)
@@ -21,6 +21,15 @@ describe('Chart.helpers.core', function() {
                        expect(helpers.isArray([42])).toBeTruthy();
                        expect(helpers.isArray(new Array())).toBeTruthy();
                        expect(helpers.isArray(Array.prototype)).toBeTruthy();
+                       expect(helpers.isArray(new Int8Array(2))).toBeTruthy();
+                       expect(helpers.isArray(new Uint8Array())).toBeTruthy();
+                       expect(helpers.isArray(new Uint8ClampedArray([128, 244]))).toBeTruthy();
+                       expect(helpers.isArray(new Int16Array())).toBeTruthy();
+                       expect(helpers.isArray(new Uint16Array())).toBeTruthy();
+                       expect(helpers.isArray(new Int32Array())).toBeTruthy();
+                       expect(helpers.isArray(new Uint32Array())).toBeTruthy();
+                       expect(helpers.isArray(new Float32Array([1.2]))).toBeTruthy();
+                       expect(helpers.isArray(new Float64Array([]))).toBeTruthy();
                });
                it('should return false if value is not an array', function() {
                        expect(helpers.isArray()).toBeFalsy();