Skip to content

Commit a5c9e56

Browse files
Jaysukh-409kgryte
andauthored
feat: add assert/is-same-booleanarray
PR-URL: #2401 Ref: #2304 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Athan Reines <kgryte@gmail.com>
1 parent 4d54abb commit a5c9e56

File tree

10 files changed

+657
-0
lines changed

10 files changed

+657
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
# isSameBooleanArray
22+
23+
> Test if two arguments are both [BooleanArrays][@stdlib/array/bool] and have the [same values][@stdlib/assert/is-same-value].
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var isSameBooleanArray = require( '@stdlib/assert/is-same-booleanarray' );
31+
```
32+
33+
#### isSameBooleanArray( v1, v2 )
34+
35+
Tests if two arguments are both [BooleanArrays][@stdlib/array/bool] and have the [same values][@stdlib/assert/is-same-value].
36+
37+
```javascript
38+
var BooleanArray = require( '@stdlib/array/bool' );
39+
40+
var x = new BooleanArray( [ true, false ] );
41+
var y = new BooleanArray( [ true, false ] );
42+
var bool = isSameBooleanArray( x, y );
43+
// returns true
44+
45+
bool = isSameBooleanArray( x, [ true, false ] );
46+
// returns false
47+
```
48+
49+
</section>
50+
51+
<!-- /.usage -->
52+
53+
<section class="notes">
54+
55+
</section>
56+
57+
<!-- /.notes -->
58+
59+
<section class="examples">
60+
61+
## Examples
62+
63+
<!-- eslint no-undef: "error" -->
64+
65+
```javascript
66+
var BooleanArray = require( '@stdlib/array/bool' );
67+
var isSameBooleanArray = require( '@stdlib/assert/is-same-booleanarray' );
68+
69+
var x = new BooleanArray( [ true, false, false, true ] );
70+
var y = new BooleanArray( [ true, false, false, true ] );
71+
var out = isSameBooleanArray( x, y );
72+
// returns true
73+
74+
x = new BooleanArray( [ true, false, false, true ] );
75+
y = new BooleanArray( [ true, true, false, false ] );
76+
out = isSameBooleanArray( x, y );
77+
// returns false
78+
```
79+
80+
</section>
81+
82+
<!-- /.examples -->
83+
84+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
85+
86+
<section class="related">
87+
88+
</section>
89+
90+
<!-- /.related -->
91+
92+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
93+
94+
<section class="links">
95+
96+
[@stdlib/array/bool]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/bool
97+
98+
[@stdlib/assert/is-same-value]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/assert/is-same-value
99+
100+
</section>
101+
102+
<!-- /.links -->
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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 isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var Boolean = require( '@stdlib/boolean/ctor' );
27+
var BooleanArray = require( '@stdlib/array/bool' );
28+
var pkg = require( './../package.json' ).name;
29+
var isSameBooleanArray = require( './../lib' );
30+
31+
32+
// FUNCTIONS //
33+
34+
/**
35+
* Creates a benchmark function.
36+
*
37+
* @private
38+
* @param {PositiveInteger} len - array length
39+
* @returns {Function} benchmark function
40+
*/
41+
function createBenchmark( len ) {
42+
var x;
43+
var y;
44+
var i;
45+
46+
x = [];
47+
for ( i = 0; i < len; i++ ) {
48+
x.push( Boolean( i%2 ) );
49+
}
50+
x = new BooleanArray( x );
51+
y = new BooleanArray( x );
52+
53+
return benchmark;
54+
55+
/**
56+
* Benchmark function.
57+
*
58+
* @private
59+
* @param {Benchmark} b - benchmark instance
60+
*/
61+
function benchmark( b ) {
62+
var bool;
63+
var i;
64+
65+
b.tic();
66+
for ( i = 0; i < b.iterations; i++ ) {
67+
bool = isSameBooleanArray( x, y );
68+
if ( typeof bool !== 'boolean' ) {
69+
b.fail( 'should return a boolean' );
70+
}
71+
}
72+
b.toc();
73+
if ( !isBoolean( bool ) ) {
74+
b.fail( 'should return a boolean' );
75+
}
76+
b.pass( 'benchmark finished' );
77+
b.end();
78+
}
79+
}
80+
81+
82+
// MAIN //
83+
84+
/**
85+
* Main execution sequence.
86+
*
87+
* @private
88+
*/
89+
function main() {
90+
var len;
91+
var min;
92+
var max;
93+
var f;
94+
var i;
95+
96+
min = 1; // 10^min
97+
max = 6; // 10^max
98+
99+
for ( i = min; i <= max; i++ ) {
100+
len = pow( 10, i );
101+
f = createBenchmark( len );
102+
bench( pkg+':len='+len, f );
103+
}
104+
}
105+
106+
main();
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
{{alias}}( v1, v2 )
3+
Tests if two arguments are both BooleanArrays and have the same values.
4+
5+
Parameters
6+
----------
7+
v1: any
8+
First input value.
9+
10+
v2: any
11+
Second input value.
12+
13+
Returns
14+
-------
15+
bool: boolean
16+
Boolean indicating whether two arguments are the same.
17+
18+
Examples
19+
--------
20+
> var x = new {{alias:@stdlib/array/bool}}( [ true, false, false, true ] );
21+
> var y = new {{alias:@stdlib/array/bool}}( [ true, false, false, true ] );
22+
> var bool = {{alias}}( x, y )
23+
true
24+
25+
> x = new {{alias:@stdlib/array/bool}}( [ true, false, false, true ] );
26+
> y = new {{alias:@stdlib/array/bool}}( [ true, true, false, false ] );
27+
> bool = {{alias}}( x, y )
28+
false
29+
30+
See Also
31+
--------
32+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
/**
22+
* Tests if two arguments are both BooleanArrays and have the same values.
23+
*
24+
* @param v1 - first input value
25+
* @param v2 - second input value
26+
* @returns boolean indicating whether two arguments are the same
27+
*
28+
* @example
29+
* var BooleanArray = require( '@stdlib/array/bool' );
30+
*
31+
* var x = new BooleanArray( [ true, false, false, true ] );
32+
* var y = new BooleanArray( [ true, false, false, true ] );
33+
*
34+
* var out = isSameBooleanArray( x, y );
35+
* // returns true
36+
*
37+
* @example
38+
* var BooleanArray = require( '@stdlib/array/bool' );
39+
*
40+
* var x = new BooleanArray( [ true, false, false, true ] );
41+
* var y = new BooleanArray( [ true, true, false, false ] );
42+
*
43+
* var out = isSameBooleanArray( x, y );
44+
* // returns false
45+
*/
46+
declare function isSameBooleanArray( v1: any, v2: any ): boolean;
47+
48+
49+
// EXPORTS //
50+
51+
export = isSameBooleanArray;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 isSameBooleanArray = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a boolean...
25+
{
26+
isSameBooleanArray( true, true ); // $ExpectType boolean
27+
isSameBooleanArray( null, null ); // $ExpectType boolean
28+
isSameBooleanArray( 'beep', 'boop' ); // $ExpectType boolean
29+
}
30+
31+
// The compiler throws an error if the function is provided an unsupported number of arguments...
32+
{
33+
isSameBooleanArray(); // $ExpectError
34+
isSameBooleanArray( 3.14 ); // $ExpectError
35+
isSameBooleanArray( 'beep', 'beep', 3.14 ); // $ExpectError
36+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
var BooleanArray = require( '@stdlib/array/bool' );
22+
var isSameBooleanArray = require( './../lib' );
23+
24+
var x = new BooleanArray( [ true, false, false, true ] );
25+
var y = new BooleanArray( [ true, false, false, true ] );
26+
var out = isSameBooleanArray( x, y );
27+
console.log( out );
28+
// => true
29+
30+
x = new BooleanArray( [ true, false, false, true ] );
31+
y = new BooleanArray( [ true, true, false, false ] );
32+
out = isSameBooleanArray( x, y );
33+
console.log( out );
34+
// => false

0 commit comments

Comments
 (0)