Skip to content

Commit

Permalink
feat: add array/base/assert/is-booleanarray
Browse files Browse the repository at this point in the history
PR-URL: #2357
Ref: #2304
Reviewed-by: Athan Reines <kgryte@gmail.com>
  • Loading branch information
Jaysukh-409 authored Jun 12, 2024
1 parent a96a408 commit ce961d9
Show file tree
Hide file tree
Showing 10 changed files with 770 additions and 0 deletions.
157 changes: 157 additions & 0 deletions lib/node_modules/@stdlib/array/base/assert/is-booleanarray/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<!--
@license Apache-2.0
Copyright (c) 2024 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.
-->

# isBooleanArray

> Test if a value is a [`BooleanArray`][@stdlib/array/bool].
<section class="intro">

</section>

<!-- ./intro -->

<section class="usage">

## Usage

```javascript
var isBooleanArray = require( '@stdlib/array/base/assert/is-booleanarray' );
```

#### isBooleanArray( value )

Tests if a value is a [`BooleanArray`][@stdlib/array/bool].

```javascript
var BooleanArray = require( '@stdlib/array/bool' );

var arr = new BooleanArray( 10 );
var bool = isBooleanArray( arr );
// returns true
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- This function is not robust and that is intentional. This function provides a lower cost way to reasonably determine whether an input value is a [`BooleanArray`][@stdlib/array/bool] in order to avoid walking the prototype chain and resolving constructors, which is necessary for robust identification of cross-realm instances. For more robust validation, see [`@stdlib/assert/is-booleanarray`][@stdlib/assert/is-booleanarray].

</section>

<!-- /.notes -->

<section class="examples">

## Examples

<!-- eslint-disable object-curly-newline -->

<!-- eslint no-undef: "error" -->

```javascript
var Int8Array = require( '@stdlib/array/int8' );
var Uint8Array = require( '@stdlib/array/uint8' );
var Uint8ClampedArray = require( '@stdlib/array/uint8c' );
var Int16Array = require( '@stdlib/array/int16' );
var Uint16Array = require( '@stdlib/array/uint16' );
var Int32Array = require( '@stdlib/array/int32' );
var Uint32Array = require( '@stdlib/array/uint32' );
var Float32Array = require( '@stdlib/array/float32' );
var Float64Array = require( '@stdlib/array/float64' );
var Complex128Array = require( '@stdlib/array/complex128' );
var Complex64Array = require( '@stdlib/array/complex64' );
var BooleanArray = require( '@stdlib/array/bool' );
var isBooleanArray = require( '@stdlib/array/base/assert/is-booleanarray' );

var bool = isBooleanArray( new BooleanArray( 10 ) );
// returns true

bool = isBooleanArray( new Complex64Array( 10 ) );
// returns false

bool = isBooleanArray( new Complex128Array( 10 ) );
// returns false

bool = isBooleanArray( [] );
// returns false

bool = isBooleanArray( new Float64Array( 10 ) );
// returns false

bool = isBooleanArray( new Float32Array( 10 ) );
// returns false

bool = isBooleanArray( new Int32Array( 10 ) );
// returns false

bool = isBooleanArray( new Uint32Array( 10 ) );
// returns false

bool = isBooleanArray( new Int16Array( 10 ) );
// returns false

bool = isBooleanArray( new Uint16Array( 10 ) );
// returns false

bool = isBooleanArray( new Int8Array( 10 ) );
// returns false

bool = isBooleanArray( new Uint8Array( 10 ) );
// returns false

bool = isBooleanArray( new Uint8ClampedArray( 10 ) );
// returns false

bool = isBooleanArray( { 'length': 0 } );
// returns false
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[@stdlib/array/bool]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/bool

[@stdlib/assert/is-booleanarray]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/assert/is-booleanarray

<!-- <related-links> -->

<!-- </related-links> -->

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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 bench = require( '@stdlib/bench' );
var Float32Array = require( '@stdlib/array/float32' );
var Complex64Array = require( '@stdlib/array/complex64' );
var BooleanArray = require( '@stdlib/array/bool' );
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
var pkg = require( './../package.json' ).name;
var isBooleanArray = require( './../lib' );


// MAIN //

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

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
obj = [ i, i+1 ];
bool = isBooleanArray( obj );
if ( typeof bool !== 'boolean' ) {
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+'::boolean_array', function benchmark( b ) {
var values;
var bool;
var obj;
var N;
var i;

values = [
new BooleanArray( [ true, false ] ),
new BooleanArray( [ false, true ] )
];
N = values.length;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
obj = values[ i%N ];
bool = isBooleanArray( obj );
if ( typeof bool !== 'boolean' ) {
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+'::real_typed_array', function benchmark( b ) {
var values;
var bool;
var obj;
var N;
var i;

values = [
new Float32Array( [ 1.0, 2.0 ] ),
new Float32Array( [ 3.0, 4.0 ] )
];
N = values.length;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
obj = values[ i%N ];
bool = isBooleanArray( obj );
if ( typeof bool !== 'boolean' ) {
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+'::complex_typed_array', function benchmark( b ) {
var values;
var bool;
var obj;
var N;
var i;

values = [
new Complex64Array( [ 1.0, 2.0 ] ),
new Complex64Array( [ 3.0, 4.0 ] )
];
N = values.length;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
obj = values[ i%N ];
bool = isBooleanArray( obj );
if ( typeof bool !== 'boolean' ) {
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+'::array_like_object', function benchmark( b ) {
var bool;
var obj;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
obj = {
'length': 2,
'0': i,
'1': i + 1
};
bool = isBooleanArray( obj );
if ( typeof bool !== 'boolean' ) {
b.fail( 'should return a boolean' );
}
}
b.toc();
if ( !isBoolean( bool ) ) {
b.fail( 'should return a boolean' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

{{alias}}( value )
Tests if a value is a BooleanArray.

Parameters
----------
value: ArrayLikeObject
Value to test.

Returns
-------
bool: boolean
Boolean indicating whether a value is a BooleanArray.

Examples
--------
> var bool = {{alias}}( new {{alias:@stdlib/array/bool}}( 10 ) )
true
> bool = {{alias}}( [] )
false
> bool = {{alias}}( { 'length': 0 } )
false

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2024 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.
*/

// TypeScript Version: 4.1

/// <reference types="@stdlib/types"/>

import { Collection, BooleanArray } from '@stdlib/types/array';

/**
* Tests if a value is a `BooleanArray`.
*
* @param value - value to test
* @returns boolean indicating whether a value is a `BooleanArray`
*
* @example
* var BooleanArray = require( '@stdlib/array/bool' );
*
* var arr = new BooleanArray( 10 );
* var bool = isBooleanArray( arr );
* // returns true
*
* @example
* var bool = isBooleanArray( [] );
* // returns false
*/
declare function isBooleanArray( value: Collection | BooleanArray ): value is BooleanArray;


// EXPORTS //

export = isBooleanArray;
Loading

1 comment on commit ce961d9

@stdlib-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage Report

Package Statements Branches Functions Lines
array/base/assert/is-booleanarray $\color{green}100/100$
$\color{green}+100.00\%$
$\color{green}6/6$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}100/100$
$\color{green}+100.00\%$

The above coverage report was generated for the changes in this push.

Please sign in to comment.