Skip to content

Commit 7c36ea3

Browse files
Jaysukh-409kgryte
andauthored
feat: add assert/is-booleanarray
PR-URL: #2299 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 96e896a commit 7c36ea3

File tree

10 files changed

+728
-0
lines changed

10 files changed

+728
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# isBooleanArray
22+
23+
> Test if a value is a [BooleanArray][@stdlib/array/bool].
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var isBooleanArray = require( '@stdlib/assert/is-booleanarray' );
31+
```
32+
33+
#### isBooleanArray( value )
34+
35+
Tests if a value is a [`BooleanArray`][@stdlib/array/bool].
36+
37+
```javascript
38+
var BooleanArray = require( '@stdlib/array/bool' );
39+
40+
var bool = isBooleanArray( new BooleanArray( 10 ) );
41+
// returns true
42+
43+
bool = isBooleanArray( [] );
44+
// returns false
45+
```
46+
47+
</section>
48+
49+
<!-- /.usage -->
50+
51+
<section class="examples">
52+
53+
## Examples
54+
55+
<!-- eslint no-undef: "error" -->
56+
57+
```javascript
58+
var Int8Array = require( '@stdlib/array/int8' );
59+
var Uint8Array = require( '@stdlib/array/uint8' );
60+
var Uint8ClampedArray = require( '@stdlib/array/uint8c' );
61+
var Int16Array = require( '@stdlib/array/int16' );
62+
var Uint16Array = require( '@stdlib/array/uint16' );
63+
var Int32Array = require( '@stdlib/array/int32' );
64+
var Uint32Array = require( '@stdlib/array/uint32' );
65+
var Float32Array = require( '@stdlib/array/float32' );
66+
var Float64Array = require( '@stdlib/array/float64' );
67+
var Complex128Array = require( '@stdlib/array/complex128' );
68+
var Complex64Array = require( '@stdlib/array/complex64' );
69+
var BooleanArray = require( '@stdlib/array/bool' );
70+
var isBooleanArray = require( '@stdlib/assert/is-booleanarray' );
71+
72+
var bool = isBooleanArray( new BooleanArray( 10 ) );
73+
// returns true
74+
75+
bool = isBooleanArray( new Complex64Array( 10 ) );
76+
// returns false
77+
78+
bool = isBooleanArray( new Complex128Array( 10 ) );
79+
// returns false
80+
81+
bool = isBooleanArray( new Float64Array( 10 ) );
82+
// returns false
83+
84+
bool = isBooleanArray( new Int8Array( 10 ) );
85+
// returns false
86+
87+
bool = isBooleanArray( new Uint8Array( 10 ) );
88+
// returns false
89+
90+
bool = isBooleanArray( new Uint8ClampedArray( 10 ) );
91+
// returns false
92+
93+
bool = isBooleanArray( new Int16Array( 10 ) );
94+
// returns false
95+
96+
bool = isBooleanArray( new Uint16Array( 10 ) );
97+
// returns false
98+
99+
bool = isBooleanArray( new Int32Array( 10 ) );
100+
// returns false
101+
102+
bool = isBooleanArray( new Uint32Array( 10 ) );
103+
// returns false
104+
105+
bool = isBooleanArray( new Float32Array( 10 ) );
106+
// returns false
107+
108+
bool = isBooleanArray( new Array( 10 ) );
109+
// returns false
110+
111+
bool = isBooleanArray( {} );
112+
// returns false
113+
114+
bool = isBooleanArray( null );
115+
// returns false
116+
```
117+
118+
</section>
119+
120+
<!-- /.examples -->
121+
122+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
123+
124+
<section class="related">
125+
126+
</section>
127+
128+
<!-- /.related -->
129+
130+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
131+
132+
<section class="links">
133+
134+
[@stdlib/array/bool]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/bool
135+
136+
</section>
137+
138+
<!-- /.links -->
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 bench = require( '@stdlib/bench' );
24+
var Int8Array = require( '@stdlib/array/int8' );
25+
var Uint8Array = require( '@stdlib/array/uint8' );
26+
var Uint8ClampedArray = require( '@stdlib/array/uint8c' );
27+
var Int16Array = require( '@stdlib/array/int16' );
28+
var Uint16Array = require( '@stdlib/array/uint16' );
29+
var Int32Array = require( '@stdlib/array/int32' );
30+
var Uint32Array = require( '@stdlib/array/uint32' );
31+
var Float32Array = require( '@stdlib/array/float32' );
32+
var Float64Array = require( '@stdlib/array/float64' );
33+
var Complex64Array = require( '@stdlib/array/complex64' );
34+
var Complex128Array = require( '@stdlib/array/complex128' );
35+
var BooleanArray = require( '@stdlib/array/bool' );
36+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
37+
var pkg = require( './../package.json' ).name;
38+
var isBooleanArray = require( './../lib' );
39+
40+
41+
// MAIN //
42+
43+
bench( pkg, function benchmark( b ) {
44+
var values;
45+
var bool;
46+
var i;
47+
48+
values = [
49+
new Float64Array( 10 ),
50+
new Float32Array( 10 ),
51+
new Int32Array( 10 ),
52+
new Uint32Array( 10 ),
53+
new Int16Array( 10 ),
54+
new Uint16Array( 10 ),
55+
new Int8Array( 10 ),
56+
new Uint8Array( 10 ),
57+
new Uint8ClampedArray( 10 ),
58+
new Complex64Array( 10 ),
59+
new Complex128Array( 10 ),
60+
new BooleanArray( 10 )
61+
];
62+
63+
b.tic();
64+
for ( i = 0; i < b.iterations; i++ ) {
65+
bool = isBooleanArray( values[ i%values.length ] );
66+
if ( typeof bool !== 'boolean' ) {
67+
b.fail( 'should return a boolean' );
68+
}
69+
}
70+
b.toc();
71+
if ( !isBoolean( bool ) ) {
72+
b.fail( 'should return a boolean' );
73+
}
74+
b.pass( 'benchmark finished' );
75+
b.end();
76+
});
77+
78+
bench( pkg+'::true', function benchmark( b ) {
79+
var values;
80+
var bool;
81+
var i;
82+
83+
values = [
84+
new BooleanArray( 10 ),
85+
new BooleanArray( 10 )
86+
];
87+
88+
b.tic();
89+
for ( i = 0; i < b.iterations; i++ ) {
90+
bool = isBooleanArray( values[ i%values.length ] );
91+
if ( typeof bool !== 'boolean' ) {
92+
b.fail( 'should return a boolean' );
93+
}
94+
}
95+
b.toc();
96+
if ( !isBoolean( bool ) ) {
97+
b.fail( 'should return a boolean' );
98+
}
99+
b.pass( 'benchmark finished' );
100+
b.end();
101+
});
102+
103+
bench( pkg+'::false', function benchmark( b ) {
104+
var values;
105+
var bool;
106+
var i;
107+
108+
values = [
109+
new Complex128Array( 10 ),
110+
new Complex64Array( 10 ),
111+
new Float64Array( 10 ),
112+
new Float32Array( 10 ),
113+
new Int32Array( 10 ),
114+
new Uint32Array( 10 ),
115+
new Int16Array( 10 ),
116+
new Uint16Array( 10 ),
117+
new Int8Array( 10 ),
118+
new Uint8Array( 10 ),
119+
new Uint8ClampedArray( 10 )
120+
];
121+
122+
b.tic();
123+
for ( i = 0; i < b.iterations; i++ ) {
124+
bool = isBooleanArray( values[ i%values.length ] );
125+
if ( typeof bool !== 'boolean' ) {
126+
b.fail( 'should return a boolean' );
127+
}
128+
}
129+
b.toc();
130+
if ( !isBoolean( bool ) ) {
131+
b.fail( 'should return a boolean' );
132+
}
133+
b.pass( 'benchmark finished' );
134+
b.end();
135+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
{{alias}}( value )
3+
Tests if a value is a BooleanArray.
4+
5+
Parameters
6+
----------
7+
value: any
8+
Value to test.
9+
10+
Returns
11+
-------
12+
bool: boolean
13+
Boolean indicating whether a value is a BooleanArray.
14+
15+
Examples
16+
--------
17+
> var bool = {{alias}}( new {{alias:@stdlib/array/bool}}( 10 ) )
18+
true
19+
> bool = {{alias}}( [] )
20+
false
21+
22+
See Also
23+
--------
24+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { BooleanArray } from '@stdlib/types/array';
24+
25+
/**
26+
* Tests if a value is a BooleanArray.
27+
*
28+
* @param value - value to test
29+
* @returns boolean indicating whether a value is a BooleanArray
30+
*
31+
* @example
32+
* var BooleanArray = require( '@stdlib/array/bool' );
33+
*
34+
* var bool = isBooleanArray( new BooleanArray( 10 ) );
35+
* // returns true
36+
*
37+
* @example
38+
* var bool = isBooleanArray( [] );
39+
* // returns false
40+
*/
41+
declare function isBooleanArray( value: any ): value is BooleanArray;
42+
43+
44+
// EXPORTS //
45+
46+
export = isBooleanArray;
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) 2024 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+
import isBooleanArray = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a boolean...
25+
{
26+
isBooleanArray( [] ); // $ExpectType boolean
27+
}
28+
29+
// The compiler throws an error if the function is provided an unsupported number of arguments...
30+
{
31+
isBooleanArray(); // $ExpectError
32+
isBooleanArray( [], 123 ); // $ExpectError
33+
}

0 commit comments

Comments
 (0)