Skip to content

Commit 3f829b2

Browse files
committed
Auto-generated commit
1 parent 553e81b commit 3f829b2

File tree

12 files changed

+408
-32
lines changed

12 files changed

+408
-32
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
# Files #
2020
#########
21-
21+
package.json.copy
2222

2323
# Directories #
2424
###############

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Makefile
3636
**/examples/
3737
reports/
3838
support/
39+
scripts/
3940
**/tmp/
4041
workshops/
4142

is-arraybuffer-view/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var isArrayBufferView = require( '@stdlib/assert/is-arraybuffer-view' );
3232

3333
#### isArrayBufferView( value )
3434

35-
Tests if a value is an [`ArrayBuffer`][mdn-arraybuffer] view such as a [`DataView`][mdn-dataview] or[`TypedArray`][mdn-typed-array].
35+
Tests if a value is an [`ArrayBuffer`][mdn-arraybuffer] view such as a [`DataView`][mdn-dataview] or [`TypedArray`][mdn-typed-array].
3636

3737
```javascript
3838
var Int8Array = require( '@stdlib/array/int8' );

is-arraybuffer-view/benchmark/benchmark.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var isArrayBufferView = require( './../lib' );
3535

3636
// MAIN //
3737

38-
bench( pkg, function benchmark( b ) {
38+
bench( pkg+'::true', function benchmark( b ) {
3939
var values;
4040
var bool;
4141
var i;
@@ -47,7 +47,30 @@ bench( pkg, function benchmark( b ) {
4747
new Uint32Array( 10 ),
4848
new Int16Array( 10 ),
4949
new Uint16Array( 10 ),
50-
new Int8Array( 10 ),
50+
new Int8Array( 10 )
51+
];
52+
53+
b.tic();
54+
for ( i = 0; i < b.iterations; i++ ) {
55+
bool = isArrayBufferView( values[ i%values.length ] );
56+
if ( !isBoolean( bool ) ) {
57+
b.fail( 'should return a boolean' );
58+
}
59+
}
60+
b.toc();
61+
if ( !isBoolean( bool ) ) {
62+
b.fail( 'should return a boolean' );
63+
}
64+
b.pass( 'benchmark finished' );
65+
b.end();
66+
});
67+
68+
bench( pkg+'::false', function benchmark( b ) {
69+
var values;
70+
var bool;
71+
var i;
72+
73+
values = [
5174
[],
5275
{},
5376
true,

is-arraybuffer-view/lib/index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,24 @@
3737
// MODULES //
3838

3939
var hasArrayBufferSupport = require( './../../has-arraybuffer-support' );
40-
var main = require( './main.js' );
40+
var isFunction = require( './../../is-function' );
41+
var ArrayBuffer = require( '@stdlib/array/buffer' );
42+
var noArraybuffer = require( './no_arraybuffer.js' );
4143
var polyfill = require( './polyfill.js' );
44+
var main = require( './main.js' );
4245

4346

4447
// MAIN //
4548

4649
var isArrayBufferView;
4750
if ( hasArrayBufferSupport() ) {
48-
isArrayBufferView = main;
51+
if ( isFunction( ArrayBuffer.isView ) ) {
52+
isArrayBufferView = main;
53+
} else {
54+
isArrayBufferView = polyfill;
55+
}
4956
} else {
50-
isArrayBufferView = polyfill;
57+
isArrayBufferView = noArraybuffer;
5158
}
5259

5360

is-arraybuffer-view/lib/main.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818

1919
'use strict';
2020

21+
// MODULES //
22+
23+
var ArrayBuffer = require( '@stdlib/array/buffer' );
24+
25+
2126
// MAIN //
2227

2328
/**
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var constantFunction = require( '@stdlib/utils/constant-function' );
24+
25+
26+
// MAIN //
27+
28+
var isArrayBufferView = constantFunction( false );
29+
30+
31+
// EXPORTS //
32+
33+
module.exports = isArrayBufferView;

is-arraybuffer-view/lib/polyfill.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,35 @@
2020

2121
// MODULES //
2222

23-
var constantFunction = require( '@stdlib/utils/constant-function' );
23+
var isTypedArray = require( './../../is-typed-array' );
24+
var isDataView = require( './../../is-dataview' );
2425

2526

2627
// MAIN //
2728

28-
var isArrayBufferView = constantFunction( false );
29+
/**
30+
* Polyfill for determining whether an object is an array buffer view.
31+
*
32+
* ## Notes
33+
*
34+
* - This polyfill should only be used in environments which do not provide native `ArrayBuffer.isView` support.
35+
* - The implementation checks whether a value is a data view or typed array.
36+
*
37+
* @param {*} value - value to test
38+
* @returns {boolean} boolean indicating whether value is an array buffer view
39+
*
40+
* @example
41+
* var Int8Array = require( '@stdlib/array/int8' );
42+
* var bool = isArrayBufferView( new Int8Array( 10 ) );
43+
* // returns true
44+
*
45+
* @example
46+
* var bool = isArrayBufferView( [] );
47+
* // returns false
48+
*/
49+
function isArrayBufferView( value ) {
50+
return isTypedArray( value ) || isDataView( value );
51+
}
2952

3053

3154
// EXPORTS //

is-arraybuffer-view/test/test.js

Lines changed: 73 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,14 @@
2121
// MODULES //
2222

2323
var tape = require( 'tape' );
24-
var Float32Array = require( '@stdlib/array/float32' );
25-
var Float64Array = require( '@stdlib/array/float64' );
24+
var proxyquire = require( 'proxyquire' );
2625
var Int8Array = require( '@stdlib/array/int8' );
2726
var Int16Array = require( '@stdlib/array/int16' );
28-
var Int32Array = require( '@stdlib/array/int32' );
29-
var Uint8Array = require( '@stdlib/array/uint8' );
30-
var Uint8ClampedArray = require( '@stdlib/array/uint8c' );
3127
var Uint16Array = require( '@stdlib/array/uint16' );
28+
var Int32Array = require( '@stdlib/array/int32' );
3229
var Uint32Array = require( '@stdlib/array/uint32' );
33-
var ArrayBuffer = require( '@stdlib/array/buffer' );
30+
var Float32Array = require( '@stdlib/array/float32' );
31+
var Float64Array = require( '@stdlib/array/float64' );
3432
var isArrayBufferView = require( './../lib' );
3533

3634

@@ -42,48 +40,100 @@ tape( 'main export is a function', function test( t ) {
4240
t.end();
4341
});
4442

45-
tape( 'the function returns `true` if provided an `ArrayBuffer` view', function test( t ) {
43+
tape( 'if an environment does not support array buffers, the main export is a polyfill which always returns `false`', function test( t ) {
44+
var isArrayBufferView;
4645
var values;
4746
var i;
4847

48+
isArrayBufferView = proxyquire( './../lib', {
49+
'./../../has-arraybuffer-support': hasSupport
50+
});
51+
52+
t.strictEqual( isArrayBufferView, require( './../lib/no_arraybuffer.js' ), 'exports a polyfill' );
53+
4954
values = [
50-
new Float64Array( 10 ),
51-
new Float32Array( 10 ),
52-
new Uint32Array( 10 ),
53-
new Int32Array( 10 ),
54-
new Uint16Array( 10 ),
55-
new Int16Array( 10 ),
56-
new Uint8Array( 10 ),
57-
new Int8Array( 10 ),
58-
new Uint8ClampedArray( 10 )
55+
'5',
56+
5,
57+
NaN,
58+
null,
59+
void 0,
60+
false,
61+
true,
62+
[],
63+
{},
64+
function noop() {}
5965
];
6066

6167
for ( i = 0; i < values.length; i++ ) {
62-
t.strictEqual( isArrayBufferView( values[i] ), true, 'returns true when provided ' + values[i] );
68+
t.strictEqual( isArrayBufferView( values[ i ] ), false, 'returns false when provided '+values[ i ] );
6369
}
6470
t.end();
71+
72+
function hasSupport() {
73+
return false;
74+
}
6575
});
6676

67-
tape( 'the function returns `false` if not provided an `ArrayBuffer` view', function test( t ) {
77+
tape( 'if an environment does support array buffers but not the `ArrayBuffer.isView` method, the main export is a polyfill checking for data views or typed arrays', function test( t ) {
78+
var isArrayBufferView;
6879
var values;
6980
var i;
7081

82+
isArrayBufferView = proxyquire( './../lib', {
83+
'@stdlib/array/buffer': {
84+
'isView': null
85+
}
86+
});
87+
88+
t.strictEqual( isArrayBufferView, require( './../lib/polyfill.js' ), 'exports a polyfill' );
89+
7190
values = [
7291
'5',
7392
5,
7493
NaN,
75-
true,
7694
null,
7795
void 0,
96+
false,
97+
true,
7898
[],
7999
{},
80-
function noop() {},
81-
new Array( 10 ),
82-
new ArrayBuffer( 10 )
100+
function noop() {}
83101
];
84102

85103
for ( i = 0; i < values.length; i++ ) {
86-
t.strictEqual( isArrayBufferView( values[i] ), false, 'returns false when provided ' + values[i] );
104+
t.strictEqual( isArrayBufferView( values[ i ] ), false, 'returns false when provided '+values[ i ] );
105+
}
106+
107+
values = [
108+
new Float64Array( 10 ),
109+
new Float32Array( 10 ),
110+
new Int32Array( 10 ),
111+
new Uint32Array( 10 ),
112+
new Int16Array( 10 ),
113+
new Uint16Array( 10 ),
114+
new Int8Array( 10 )
115+
];
116+
for ( i = 0; i < values.length; i++ ) {
117+
t.strictEqual( isArrayBufferView( values[ i ] ), true, 'returns true when provided '+values[ i ] );
87118
}
119+
120+
t.end();
121+
});
122+
123+
tape( 'if an environment does support array buffers and the `ArrayBuffer.isView` method, the main export is not any of the polyfills', function test( t ) {
124+
var isArrayBufferView = proxyquire( './../lib', {
125+
'./../../has-arraybuffer-support': hasSupport,
126+
'./main.js': main
127+
});
128+
129+
t.strictEqual( isArrayBufferView, isArrayBufferView, 'exports expected function' );
88130
t.end();
131+
132+
function hasSupport() {
133+
return true;
134+
}
135+
136+
function main() {
137+
return false;
138+
}
89139
});

0 commit comments

Comments
 (0)