Skip to content

Commit 0d8a44c

Browse files
kurklesimonbrunel
authored andcommitted
Add support for typed arrays (chartjs#5905)
1 parent a3c3cec commit 0d8a44c

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

src/helpers/helpers.core.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,20 @@ var helpers = {
3232
},
3333

3434
/**
35-
* Returns true if `value` is an array, else returns false.
35+
* Returns true if `value` is an array (including typed arrays), else returns false.
3636
* @param {*} value - The value to test.
3737
* @returns {Boolean}
3838
* @function
3939
*/
40-
isArray: Array.isArray ? Array.isArray : function(value) {
41-
return Object.prototype.toString.call(value) === '[object Array]';
40+
isArray: function(value) {
41+
if (Array.isArray && Array.isArray(value)) {
42+
return true;
43+
}
44+
var type = Object.prototype.toString.call(value);
45+
if (type.substr(0, 7) === '[object' && type.substr(-6) === 'Array]') {
46+
return true;
47+
}
48+
return false;
4249
},
4350

4451
/**

test/.eslintrc.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
parserOptions:
2+
ecmaVersion: 5 # don't rely on default, since its changed by env: es6
3+
14
env:
5+
es6: true # also changes default ecmaVersion to 6
26
jasmine: true
37

48
globals:

test/specs/helpers.core.tests.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ describe('Chart.helpers.core', function() {
2121
expect(helpers.isArray([42])).toBeTruthy();
2222
expect(helpers.isArray(new Array())).toBeTruthy();
2323
expect(helpers.isArray(Array.prototype)).toBeTruthy();
24+
expect(helpers.isArray(new Int8Array(2))).toBeTruthy();
25+
expect(helpers.isArray(new Uint8Array())).toBeTruthy();
26+
expect(helpers.isArray(new Uint8ClampedArray([128, 244]))).toBeTruthy();
27+
expect(helpers.isArray(new Int16Array())).toBeTruthy();
28+
expect(helpers.isArray(new Uint16Array())).toBeTruthy();
29+
expect(helpers.isArray(new Int32Array())).toBeTruthy();
30+
expect(helpers.isArray(new Uint32Array())).toBeTruthy();
31+
expect(helpers.isArray(new Float32Array([1.2]))).toBeTruthy();
32+
expect(helpers.isArray(new Float64Array([]))).toBeTruthy();
2433
});
2534
it('should return false if value is not an array', function() {
2635
expect(helpers.isArray()).toBeFalsy();

0 commit comments

Comments
 (0)