Skip to content

Commit 2214349

Browse files
committed
feat: add array/base/every-by
1 parent d68f17c commit 2214349

File tree

10 files changed

+1073
-0
lines changed

10 files changed

+1073
-0
lines changed
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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+
# everyBy
22+
23+
> Test whether all elements in a collection pass a test implemented by a predicate function.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var everyBy = require( '@stdlib/array/base/every-by' );
41+
```
42+
43+
#### everyBy( x, predicate\[, thisArg] )
44+
45+
Tests whether all elements in an array pass a test implemented by a `predicate` function.
46+
47+
```javascript
48+
function isPositive( value ) {
49+
return ( value > 0 );
50+
}
51+
52+
var x = [ 1, 2, 3, 4 ];
53+
54+
var bool = everyBy( x, isPositive );
55+
// returns true
56+
```
57+
58+
If a `predicate` function returns a non-truthy value, the function **immediately** returns `false`.
59+
60+
```javascript
61+
function isPositive( value ) {
62+
return ( value > 0 );
63+
}
64+
65+
var x = [ 1, -2, 3, 4 ];
66+
67+
var bool = everyBy( x, isPositive );
68+
// returns false
69+
```
70+
71+
The `predicate` function is provided three arguments:
72+
73+
- **value**: current array element.
74+
- **index**: current array element index.
75+
- **arr**: input array.
76+
77+
To set the `predicate` function execution context, provide a `thisArg`.
78+
79+
```javascript
80+
function sum( value ) {
81+
if ( value < 0 ) {
82+
return false;
83+
}
84+
this.sum += value;
85+
this.count += 1;
86+
return true;
87+
}
88+
89+
var x = [ 1, 2, 3, 4 ];
90+
91+
var context = {
92+
'sum': 0,
93+
'count': 0
94+
};
95+
96+
var bool = everyBy( x, sum, context );
97+
// returns true
98+
99+
var mean = context.sum / context.count;
100+
// returns 2.5
101+
```
102+
103+
</section>
104+
105+
<!-- /.usage -->
106+
107+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
108+
109+
<section class="notes">
110+
111+
## Notes
112+
113+
- If provided an array-like object having an `every` method, the function defers execution to that method and assumes that the method API has the following signature:
114+
115+
```text
116+
x.every( predicate, thisArg )
117+
```
118+
119+
- If provided an array-like object without an `every` method, the function performs a linear scan and returns immediately upon encountering a non-truthy return value. Unlike [`Array.prototype.every`][mdn-array-every], when performing a linear scan, the function does **not** skip `undefined` elements.
120+
121+
- If provided an empty array, the function returns `true`.
122+
123+
</section>
124+
125+
<!-- /.notes -->
126+
127+
<!-- Package usage examples. -->
128+
129+
<section class="examples">
130+
131+
## Examples
132+
133+
<!-- eslint no-undef: "error" -->
134+
135+
```javascript
136+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
137+
var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
138+
var naryFunction = require( '@stdlib/utils/nary-function' );
139+
var everyBy = require( '@stdlib/array/base/every-by' );
140+
141+
var x = discreteUniform( 10, 0, 10, {
142+
'dtype': 'int32'
143+
} );
144+
// returns <Int32Array>
145+
146+
var out = everyBy( x, naryFunction( isPositiveInteger, 1 ) );
147+
// returns <boolean>
148+
```
149+
150+
</section>
151+
152+
<!-- /.examples -->
153+
154+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
155+
156+
<section class="references">
157+
158+
</section>
159+
160+
<!-- /.references -->
161+
162+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
163+
164+
<section class="related">
165+
166+
</section>
167+
168+
<!-- /.related -->
169+
170+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
171+
172+
<section class="links">
173+
174+
[mdn-array-every]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every
175+
176+
</section>
177+
178+
<!-- /.links -->
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 pow = require( '@stdlib/math/base/special/pow' );
25+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
26+
var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
27+
var zeroTo = require( '@stdlib/array/base/zero-to' );
28+
var pkg = require( './../package.json' ).name;
29+
var everyBy = 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 = zeroTo( len );
43+
return benchmark;
44+
45+
/**
46+
* Benchmark function.
47+
*
48+
* @private
49+
* @param {Benchmark} b - benchmark instance
50+
*/
51+
function benchmark( b ) {
52+
var out;
53+
var i;
54+
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
out = everyBy( x, isNonNegativeInteger );
58+
if ( typeof out !== 'boolean' ) {
59+
b.fail( 'should return a boolean' );
60+
}
61+
}
62+
b.toc();
63+
if ( !isBoolean( out ) ) {
64+
b.fail( 'should return a boolean' );
65+
}
66+
b.pass( 'benchmark finished' );
67+
b.end();
68+
}
69+
}
70+
71+
72+
// MAIN //
73+
74+
/**
75+
* Main execution sequence.
76+
*
77+
* @private
78+
*/
79+
function main() {
80+
var len;
81+
var min;
82+
var max;
83+
var f;
84+
var i;
85+
86+
min = 1; // 10^min
87+
max = 6; // 10^max
88+
89+
for ( i = min; i <= max; i++ ) {
90+
len = pow( 10, i );
91+
92+
f = createBenchmark( len );
93+
bench( pkg+':dtype=generic,len='+len, f );
94+
}
95+
}
96+
97+
main();
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
{{alias}}( x, predicate[, thisArg] )
3+
Tests whether all elements in an array pass a test implemented by a
4+
predicate function.
5+
6+
The predicate function is provided three arguments:
7+
8+
- value: current array element.
9+
- index: current array element index.
10+
- arr: the input array.
11+
12+
If provided an array-like object having an `every` method , the function
13+
defers execution to that method and assumes that the method has the
14+
following signature:
15+
16+
x.every( predicate, thisArg )
17+
18+
If provided an array-like object without an `every` method, the function
19+
performs a linear scan and returns immediately upon encountering a
20+
non-truthy return value.
21+
22+
If provided an empty array, the function returns `true`.
23+
24+
Parameters
25+
----------
26+
x: Array|TypedArray|Object
27+
Input array.
28+
29+
predicate: Function
30+
Predicate function.
31+
32+
thisArg: any (optional)
33+
Execution context.
34+
35+
Returns
36+
-------
37+
bool: boolean
38+
The function returns `true` if the predicate function returns a truthy
39+
value for all elements; otherwise, the function returns `false`.
40+
41+
Examples
42+
--------
43+
> function f( v ) { return ( v > 0 ); };
44+
> var x = [ 1, 2, 3, 4 ];
45+
> var out = {{alias}}( x, f )
46+
true
47+
48+
See Also
49+
--------
50+

0 commit comments

Comments
 (0)