Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed Aug 13, 2021
1 parent 553e81b commit 3f829b2
Show file tree
Hide file tree
Showing 12 changed files with 408 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

# Files #
#########

package.json.copy

# Directories #
###############
Expand Down
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Makefile
**/examples/
reports/
support/
scripts/
**/tmp/
workshops/

Expand Down
2 changes: 1 addition & 1 deletion is-arraybuffer-view/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var isArrayBufferView = require( '@stdlib/assert/is-arraybuffer-view' );

#### isArrayBufferView( value )

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

```javascript
var Int8Array = require( '@stdlib/array/int8' );
Expand Down
27 changes: 25 additions & 2 deletions is-arraybuffer-view/benchmark/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var isArrayBufferView = require( './../lib' );

// MAIN //

bench( pkg, function benchmark( b ) {
bench( pkg+'::true', function benchmark( b ) {
var values;
var bool;
var i;
Expand All @@ -47,7 +47,30 @@ bench( pkg, function benchmark( b ) {
new Uint32Array( 10 ),
new Int16Array( 10 ),
new Uint16Array( 10 ),
new Int8Array( 10 ),
new Int8Array( 10 )
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
bool = isArrayBufferView( values[ i%values.length ] );
if ( !isBoolean( bool ) ) {
b.fail( 'should return a boolean' );
}
}
b.toc();
if ( !isBoolean( bool ) ) {
b.fail( 'should return a boolean' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::false', function benchmark( b ) {
var values;
var bool;
var i;

values = [
[],
{},
true,
Expand Down
13 changes: 10 additions & 3 deletions is-arraybuffer-view/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,24 @@
// MODULES //

var hasArrayBufferSupport = require( './../../has-arraybuffer-support' );
var main = require( './main.js' );
var isFunction = require( './../../is-function' );
var ArrayBuffer = require( '@stdlib/array/buffer' );
var noArraybuffer = require( './no_arraybuffer.js' );
var polyfill = require( './polyfill.js' );
var main = require( './main.js' );


// MAIN //

var isArrayBufferView;
if ( hasArrayBufferSupport() ) {
isArrayBufferView = main;
if ( isFunction( ArrayBuffer.isView ) ) {
isArrayBufferView = main;
} else {
isArrayBufferView = polyfill;
}
} else {
isArrayBufferView = polyfill;
isArrayBufferView = noArraybuffer;
}


Expand Down
5 changes: 5 additions & 0 deletions is-arraybuffer-view/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@

'use strict';

// MODULES //

var ArrayBuffer = require( '@stdlib/array/buffer' );


// MAIN //

/**
Expand Down
33 changes: 33 additions & 0 deletions is-arraybuffer-view/lib/no_arraybuffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2021 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var constantFunction = require( '@stdlib/utils/constant-function' );


// MAIN //

var isArrayBufferView = constantFunction( false );


// EXPORTS //

module.exports = isArrayBufferView;
27 changes: 25 additions & 2 deletions is-arraybuffer-view/lib/polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,35 @@

// MODULES //

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


// MAIN //

var isArrayBufferView = constantFunction( false );
/**
* Polyfill for determining whether an object is an array buffer view.
*
* ## Notes
*
* - This polyfill should only be used in environments which do not provide native `ArrayBuffer.isView` support.
* - The implementation checks whether a value is a data view or typed array.
*
* @param {*} value - value to test
* @returns {boolean} boolean indicating whether value is an array buffer view
*
* @example
* var Int8Array = require( '@stdlib/array/int8' );
* var bool = isArrayBufferView( new Int8Array( 10 ) );
* // returns true
*
* @example
* var bool = isArrayBufferView( [] );
* // returns false
*/
function isArrayBufferView( value ) {
return isTypedArray( value ) || isDataView( value );
}


// EXPORTS //
Expand Down
96 changes: 73 additions & 23 deletions is-arraybuffer-view/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@
// MODULES //

var tape = require( 'tape' );
var Float32Array = require( '@stdlib/array/float32' );
var Float64Array = require( '@stdlib/array/float64' );
var proxyquire = require( 'proxyquire' );
var Int8Array = require( '@stdlib/array/int8' );
var Int16Array = require( '@stdlib/array/int16' );
var Int32Array = require( '@stdlib/array/int32' );
var Uint8Array = require( '@stdlib/array/uint8' );
var Uint8ClampedArray = require( '@stdlib/array/uint8c' );
var Uint16Array = require( '@stdlib/array/uint16' );
var Int32Array = require( '@stdlib/array/int32' );
var Uint32Array = require( '@stdlib/array/uint32' );
var ArrayBuffer = require( '@stdlib/array/buffer' );
var Float32Array = require( '@stdlib/array/float32' );
var Float64Array = require( '@stdlib/array/float64' );
var isArrayBufferView = require( './../lib' );


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

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

isArrayBufferView = proxyquire( './../lib', {
'./../../has-arraybuffer-support': hasSupport
});

t.strictEqual( isArrayBufferView, require( './../lib/no_arraybuffer.js' ), 'exports a polyfill' );

values = [
new Float64Array( 10 ),
new Float32Array( 10 ),
new Uint32Array( 10 ),
new Int32Array( 10 ),
new Uint16Array( 10 ),
new Int16Array( 10 ),
new Uint8Array( 10 ),
new Int8Array( 10 ),
new Uint8ClampedArray( 10 )
'5',
5,
NaN,
null,
void 0,
false,
true,
[],
{},
function noop() {}
];

for ( i = 0; i < values.length; i++ ) {
t.strictEqual( isArrayBufferView( values[i] ), true, 'returns true when provided ' + values[i] );
t.strictEqual( isArrayBufferView( values[ i ] ), false, 'returns false when provided '+values[ i ] );
}
t.end();

function hasSupport() {
return false;
}
});

tape( 'the function returns `false` if not provided an `ArrayBuffer` view', function test( t ) {
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 ) {
var isArrayBufferView;
var values;
var i;

isArrayBufferView = proxyquire( './../lib', {
'@stdlib/array/buffer': {
'isView': null
}
});

t.strictEqual( isArrayBufferView, require( './../lib/polyfill.js' ), 'exports a polyfill' );

values = [
'5',
5,
NaN,
true,
null,
void 0,
false,
true,
[],
{},
function noop() {},
new Array( 10 ),
new ArrayBuffer( 10 )
function noop() {}
];

for ( i = 0; i < values.length; i++ ) {
t.strictEqual( isArrayBufferView( values[i] ), false, 'returns false when provided ' + values[i] );
t.strictEqual( isArrayBufferView( values[ i ] ), false, 'returns false when provided '+values[ i ] );
}

values = [
new Float64Array( 10 ),
new Float32Array( 10 ),
new Int32Array( 10 ),
new Uint32Array( 10 ),
new Int16Array( 10 ),
new Uint16Array( 10 ),
new Int8Array( 10 )
];
for ( i = 0; i < values.length; i++ ) {
t.strictEqual( isArrayBufferView( values[ i ] ), true, 'returns true when provided '+values[ i ] );
}

t.end();
});

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 ) {
var isArrayBufferView = proxyquire( './../lib', {
'./../../has-arraybuffer-support': hasSupport,
'./main.js': main
});

t.strictEqual( isArrayBufferView, isArrayBufferView, 'exports expected function' );
t.end();

function hasSupport() {
return true;
}

function main() {
return false;
}
});
Loading

0 comments on commit 3f829b2

Please sign in to comment.