Skip to content

Commit f0395b5

Browse files
committedFeb 28, 2018
improve isTypedArray
- by making sure it returns false on instances of DataView
1 parent a2fb88b commit f0395b5

File tree

5 files changed

+25
-7
lines changed

5 files changed

+25
-7
lines changed
 

‎.eslintrc

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"Int16Array": true,
1818
"Int32Array": true,
1919
"ArrayBuffer": true,
20+
"DataView": true,
2021
"SVGElement": false
2122
},
2223
"rules": {

‎src/lib/is_array.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,20 @@
88

99
'use strict';
1010

11-
// IE9 fallback
11+
// IE9 fallbacks
12+
1213
var ab = (typeof ArrayBuffer === 'undefined' || !ArrayBuffer.isView) ?
1314
{isView: function() { return false; }} :
1415
ArrayBuffer;
1516

16-
exports.isTypedArray = ab.isView;
17+
var dv = (typeof DataView === 'undefined') ?
18+
function() {} :
19+
DataView;
20+
21+
exports.isTypedArray = function(a) {
22+
return ab.isView(a) && !(a instanceof dv);
23+
};
1724

1825
exports.isArrayOrTypedArray = function(a) {
19-
return Array.isArray(a) || ab.isView(a);
26+
return Array.isArray(a) || exports.isTypedArray(a);
2027
};

‎test/jasmine/assets/ie9_mock.js

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ delete window.Float32Array;
66
delete window.Float64Array;
77
delete window.Int16Array;
88
delete window.Int32Array;
9+
delete window.DataView;

‎test/jasmine/bundle_tests/ie9_test.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ var createGraphDiv = require('../assets/create_graph_div');
1818
var destroyGraphDiv = require('../assets/destroy_graph_div');
1919

2020
describe('Bundle with IE9 supported trace types:', function() {
21-
2221
afterEach(destroyGraphDiv);
2322

24-
it(' check that ie9_mock.js did its job', function() {
23+
it('check that ie9_mock.js did its job', function() {
2524
expect(function() { return ArrayBuffer; })
2625
.toThrow(new ReferenceError('ArrayBuffer is not defined'));
26+
expect(function() { return DataView; })
27+
.toThrow(new ReferenceError('DataView is not defined'));
2728
expect(function() { return Uint8Array; })
2829
.toThrow(new ReferenceError('Uint8Array is not defined'));
2930
});

‎test/jasmine/tests/is_array_test.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ var Lib = require('@src/lib');
33
describe('isArrayOrTypedArray', function() {
44
function A() {}
55

6+
var buffer = new ArrayBuffer(2);
7+
var dv = new DataView(buffer);
8+
69
var shouldPass = [
710
[],
811
new Array(10),
@@ -26,7 +29,8 @@ describe('isArrayOrTypedArray', function() {
2629
'\n',
2730
new Date(),
2831
new RegExp('foo'),
29-
new String('string')
32+
new String('string'),
33+
dv
3034
];
3135

3236
shouldPass.forEach(function(obj) {
@@ -45,6 +49,9 @@ describe('isArrayOrTypedArray', function() {
4549
describe('isTypedArray', function() {
4650
function A() {}
4751

52+
var buffer = new ArrayBuffer(2);
53+
var dv = new DataView(buffer);
54+
4855
var shouldPass = [
4956
new Float32Array(1),
5057
new Int32Array([1, 2, 3])
@@ -68,7 +75,8 @@ describe('isTypedArray', function() {
6875
'\n',
6976
new Date(),
7077
new RegExp('foo'),
71-
new String('string')
78+
new String('string'),
79+
dv
7280
];
7381

7482
shouldPass.forEach(function(obj) {

0 commit comments

Comments
 (0)