Skip to content

Commit

Permalink
improve isTypedArray
Browse files Browse the repository at this point in the history
- by making sure it returns false on instances of DataView
  • Loading branch information
etpinard committed Feb 28, 2018
1 parent a2fb88b commit f0395b5
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"Int16Array": true,
"Int32Array": true,
"ArrayBuffer": true,
"DataView": true,
"SVGElement": false
},
"rules": {
Expand Down
13 changes: 10 additions & 3 deletions src/lib/is_array.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@

'use strict';

// IE9 fallback
// IE9 fallbacks

var ab = (typeof ArrayBuffer === 'undefined' || !ArrayBuffer.isView) ?
{isView: function() { return false; }} :
ArrayBuffer;

exports.isTypedArray = ab.isView;
var dv = (typeof DataView === 'undefined') ?
function() {} :
DataView;

exports.isTypedArray = function(a) {
return ab.isView(a) && !(a instanceof dv);
};

exports.isArrayOrTypedArray = function(a) {
return Array.isArray(a) || ab.isView(a);
return Array.isArray(a) || exports.isTypedArray(a);
};
1 change: 1 addition & 0 deletions test/jasmine/assets/ie9_mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ delete window.Float32Array;
delete window.Float64Array;
delete window.Int16Array;
delete window.Int32Array;
delete window.DataView;
5 changes: 3 additions & 2 deletions test/jasmine/bundle_tests/ie9_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');

describe('Bundle with IE9 supported trace types:', function() {

afterEach(destroyGraphDiv);

it(' check that ie9_mock.js did its job', function() {
it('check that ie9_mock.js did its job', function() {
expect(function() { return ArrayBuffer; })
.toThrow(new ReferenceError('ArrayBuffer is not defined'));
expect(function() { return DataView; })
.toThrow(new ReferenceError('DataView is not defined'));
expect(function() { return Uint8Array; })
.toThrow(new ReferenceError('Uint8Array is not defined'));
});
Expand Down
12 changes: 10 additions & 2 deletions test/jasmine/tests/is_array_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ var Lib = require('@src/lib');
describe('isArrayOrTypedArray', function() {
function A() {}

var buffer = new ArrayBuffer(2);
var dv = new DataView(buffer);

var shouldPass = [
[],
new Array(10),
Expand All @@ -26,7 +29,8 @@ describe('isArrayOrTypedArray', function() {
'\n',
new Date(),
new RegExp('foo'),
new String('string')
new String('string'),
dv
];

shouldPass.forEach(function(obj) {
Expand All @@ -45,6 +49,9 @@ describe('isArrayOrTypedArray', function() {
describe('isTypedArray', function() {
function A() {}

var buffer = new ArrayBuffer(2);
var dv = new DataView(buffer);

var shouldPass = [
new Float32Array(1),
new Int32Array([1, 2, 3])
Expand All @@ -68,7 +75,8 @@ describe('isTypedArray', function() {
'\n',
new Date(),
new RegExp('foo'),
new String('string')
new String('string'),
dv
];

shouldPass.forEach(function(obj) {
Expand Down

0 comments on commit f0395b5

Please sign in to comment.