Skip to content

Commit 42c67e7

Browse files
Jaysukh-409kgryte
andauthored
feat: add every and some methods to array/bool
PR-URL: #2421 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 32b9ebf commit 42c67e7

File tree

10 files changed

+1018
-1
lines changed

10 files changed

+1018
-1
lines changed

lib/node_modules/@stdlib/array/bool/README.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,58 @@ var len = arr.length;
334334
// returns 4
335335
```
336336

337+
<a name="method-every"></a>
338+
339+
#### BooleanArray.prototype.every( predicate\[, thisArg] )
340+
341+
Returns a boolean indicating whether all elements pass a test.
342+
343+
```javascript
344+
function predicate( v ) {
345+
return v === true;
346+
}
347+
348+
var arr = new BooleanArray( 3 );
349+
350+
arr.set( true, 0 );
351+
arr.set( true, 1 );
352+
arr.set( true, 2 );
353+
354+
var bool = arr.every( predicate );
355+
// returns true
356+
```
357+
358+
The `predicate` function is provided three arguments:
359+
360+
- **value**: current array element.
361+
- **index**: current array element index.
362+
- **arr**: the array on which this method was called.
363+
364+
To set the function execution context, provide a `thisArg`.
365+
366+
```javascript
367+
function predicate( v ) {
368+
this.count += 1;
369+
return v === true;
370+
}
371+
372+
var arr = new BooleanArray( 3 );
373+
374+
var context = {
375+
'count': 0
376+
};
377+
378+
arr.set( true, 0 );
379+
arr.set( true, 1 );
380+
arr.set( true, 2 );
381+
382+
var bool = arr.every( predicate, context );
383+
// returns true
384+
385+
var count = context.count;
386+
// returns 3
387+
```
388+
337389
<a name="method-find"></a>
338390

339391
#### BooleanArray.prototype.find( predicate\[, thisArg] )
@@ -715,6 +767,58 @@ A few notes:
715767
- If a target array cannot accommodate all values (i.e., the length of source array plus `i` exceeds the target array length), the method throws an error.
716768
- If provided a [typed array][@stdlib/array/typed] which shares an [`ArrayBuffer`][@stdlib/array/buffer] with the target array, the method will intelligently copy the source range to the destination range.
717769

770+
<a name="method-some"></a>
771+
772+
#### BooleanArray.prototype.some( predicate\[, thisArg] )
773+
774+
Returns a boolean indicating whether at least one element passes a test.
775+
776+
```javascript
777+
function predicate( v ) {
778+
return v === true;
779+
}
780+
781+
var arr = new BooleanArray( 3 );
782+
783+
arr.set( false, 0 );
784+
arr.set( true, 1 );
785+
arr.set( false, 2 );
786+
787+
var bool = arr.some( predicate );
788+
// returns true
789+
```
790+
791+
The `predicate` function is provided three arguments:
792+
793+
- **value**: current array element.
794+
- **index**: current array element index.
795+
- **arr**: the array on which this method was called.
796+
797+
To set the function execution context, provide a `thisArg`.
798+
799+
```javascript
800+
function predicate( v ) {
801+
this.count += 1;
802+
return v === true;
803+
}
804+
805+
var arr = new BooleanArray( 3 );
806+
807+
var context = {
808+
'count': 0
809+
};
810+
811+
arr.set( false, 0 );
812+
arr.set( true, 1 );
813+
arr.set( false, 2 );
814+
815+
var bool = arr.some( predicate, context );
816+
// returns true
817+
818+
var count = context.count;
819+
// returns 2
820+
```
821+
718822
<a name="method-sort"></a>
719823

720824
#### BooleanArray.prototype.sort( \[compareFcn] )
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 pkg = require( './../package.json' ).name;
26+
var BooleanArray = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg+':every', function benchmark( b ) {
32+
var bool;
33+
var arr;
34+
var i;
35+
36+
arr = new BooleanArray( [ true, false, false, true ] );
37+
38+
b.tic();
39+
for ( i = 0; i < b.iterations; i++ ) {
40+
bool = arr.every( predicate );
41+
if ( typeof bool !== 'boolean' ) {
42+
b.fail( 'should return a boolean' );
43+
}
44+
}
45+
b.toc();
46+
if ( !isBoolean( bool ) ) {
47+
b.fail( 'should return a boolean' );
48+
}
49+
b.pass( 'benchmark finished' );
50+
b.end();
51+
52+
function predicate( v ) {
53+
return v === true;
54+
}
55+
});
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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 pkg = require( './../package.json' ).name;
28+
var BooleanArray = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Predicate function.
35+
*
36+
* @private
37+
* @param {boolean} value - array element
38+
* @param {NonNegativeInteger} idx - array element index
39+
* @param {BooleanArray} arr - array instance
40+
* @returns {boolean} boolean indicating whether a value passes a test
41+
*/
42+
function predicate( value ) {
43+
return value === true;
44+
}
45+
46+
/**
47+
* Creates a benchmark function.
48+
*
49+
* @private
50+
* @param {PositiveInteger} len - array length
51+
* @returns {Function} benchmark function
52+
*/
53+
function createBenchmark( len ) {
54+
var arr;
55+
var i;
56+
57+
arr = [];
58+
for ( i = 0; i < len; i++ ) {
59+
arr.push( Boolean( 1 ) );
60+
}
61+
arr = new BooleanArray( arr );
62+
63+
return benchmark;
64+
65+
/**
66+
* Benchmark function.
67+
*
68+
* @private
69+
* @param {Benchmark} b - benchmark instance
70+
*/
71+
function benchmark( b ) {
72+
var bool;
73+
var i;
74+
75+
b.tic();
76+
for ( i = 0; i < b.iterations; i++ ) {
77+
bool = arr.every( predicate );
78+
if ( typeof bool !== 'boolean' ) {
79+
b.fail( 'should return a boolean' );
80+
}
81+
}
82+
b.toc();
83+
if ( !isBoolean( bool ) ) {
84+
b.fail( 'should return a boolean' );
85+
}
86+
b.pass( 'benchmark finished' );
87+
b.end();
88+
}
89+
}
90+
91+
92+
// MAIN //
93+
94+
/**
95+
* Main execution sequence.
96+
*
97+
* @private
98+
*/
99+
function main() {
100+
var len;
101+
var min;
102+
var max;
103+
var f;
104+
var i;
105+
106+
min = 1; // 10^min
107+
max = 6; // 10^max
108+
109+
for ( i = min; i <= max; i++ ) {
110+
len = pow( 10, i );
111+
f = createBenchmark( len );
112+
bench( pkg+':every:len='+len, f );
113+
}
114+
}
115+
116+
main();
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 pkg = require( './../package.json' ).name;
26+
var BooleanArray = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg+':every', function benchmark( b ) {
32+
var bool;
33+
var arr;
34+
var i;
35+
36+
arr = new BooleanArray( [ true, false, false, true ] );
37+
38+
b.tic();
39+
for ( i = 0; i < b.iterations; i++ ) {
40+
bool = arr.some( predicate );
41+
if ( typeof bool !== 'boolean' ) {
42+
b.fail( 'should return a boolean' );
43+
}
44+
}
45+
b.toc();
46+
if ( !isBoolean( bool ) ) {
47+
b.fail( 'should return a boolean' );
48+
}
49+
b.pass( 'benchmark finished' );
50+
b.end();
51+
52+
function predicate( v ) {
53+
return v === true;
54+
}
55+
});

0 commit comments

Comments
 (0)