Skip to content

Commit b80305d

Browse files
committed
feat: add array/base/every
1 parent 87d2120 commit b80305d

File tree

10 files changed

+895
-0
lines changed

10 files changed

+895
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
# every
22+
23+
> Test whether all elements in a collection are truthy.
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 every = require( '@stdlib/array/base/every' );
41+
```
42+
43+
#### every( x )
44+
45+
Tests whether all elements in an array are truthy.
46+
47+
```javascript
48+
var x = [ 1, 2, 3, 4 ];
49+
50+
var bool = every( x );
51+
// returns true
52+
```
53+
54+
</section>
55+
56+
<!-- /.usage -->
57+
58+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
59+
60+
<section class="notes">
61+
62+
## Notes
63+
64+
- If provided an empty array, the function returns `true`.
65+
- The function does **not** skip `undefined` elements and is thus not optimized for sparse arrays.
66+
67+
</section>
68+
69+
<!-- /.notes -->
70+
71+
<!-- Package usage examples. -->
72+
73+
<section class="examples">
74+
75+
## Examples
76+
77+
<!-- eslint no-undef: "error" -->
78+
79+
```javascript
80+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
81+
var every = require( '@stdlib/array/base/every' );
82+
83+
var x = discreteUniform( 10, 0, 10, {
84+
'dtype': 'int32'
85+
} );
86+
// returns <Int32Array>
87+
88+
var out = every( x );
89+
// returns <boolean>
90+
```
91+
92+
</section>
93+
94+
<!-- /.examples -->
95+
96+
<!-- 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. -->
97+
98+
<section class="references">
99+
100+
</section>
101+
102+
<!-- /.references -->
103+
104+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
105+
106+
<section class="related">
107+
108+
</section>
109+
110+
<!-- /.related -->
111+
112+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
113+
114+
<section class="links">
115+
116+
</section>
117+
118+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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 oneTo = require( '@stdlib/array/base/one-to' );
27+
var pkg = require( './../package.json' ).name;
28+
var every = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
var x = oneTo( len );
42+
return benchmark;
43+
44+
/**
45+
* Benchmark function.
46+
*
47+
* @private
48+
* @param {Benchmark} b - benchmark instance
49+
*/
50+
function benchmark( b ) {
51+
var out;
52+
var i;
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
out = every( x );
57+
if ( typeof out !== 'boolean' ) {
58+
b.fail( 'should return a boolean' );
59+
}
60+
}
61+
b.toc();
62+
if ( !isBoolean( out ) ) {
63+
b.fail( 'should return a boolean' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
}
68+
}
69+
70+
71+
// MAIN //
72+
73+
/**
74+
* Main execution sequence.
75+
*
76+
* @private
77+
*/
78+
function main() {
79+
var len;
80+
var min;
81+
var max;
82+
var f;
83+
var i;
84+
85+
min = 1; // 10^min
86+
max = 6; // 10^max
87+
88+
for ( i = min; i <= max; i++ ) {
89+
len = pow( 10, i );
90+
91+
f = createBenchmark( len );
92+
bench( pkg+':dtype=generic,len='+len, f );
93+
}
94+
}
95+
96+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
{{alias}}( x )
3+
Tests whether all elements in an array are truthy.
4+
5+
The function returns immediately upon encountering a non-truthy value.
6+
7+
If provided an empty array, the function returns `true`.
8+
9+
Parameters
10+
----------
11+
x: Array|TypedArray|Object
12+
Input array.
13+
14+
Returns
15+
-------
16+
bool: boolean
17+
The function returns `true` if all elements are truthy; otherwise, the
18+
function returns `false`.
19+
20+
Examples
21+
--------
22+
> var x = [ 1, 2, 3, 4 ];
23+
> var out = {{alias}}( x )
24+
true
25+
26+
See Also
27+
--------
28+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 { Collection, AccessorArrayLike } from '@stdlib/types/array';
24+
25+
/**
26+
* Tests whether all elements in an array are truthy.
27+
*
28+
* ## Notes
29+
*
30+
* - The function immediately returns upon encountering a non-truthy value.
31+
* - If provided an empty collection, the function returns `true`.
32+
*
33+
* @param x - input array
34+
* @returns boolean indicating whether all elements are truthy
35+
*
36+
* @example
37+
* var x = [ 1, 2, 3, 4 ];
38+
*
39+
* var out = every( x );
40+
* // returns true
41+
*/
42+
declare function every<T = unknown>( x: Collection<T> | AccessorArrayLike<T> ): boolean;
43+
44+
45+
// EXPORTS //
46+
47+
export = every;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
20+
import every = require( './index' );
21+
22+
23+
// TESTS //
24+
25+
// The function returns a boolean...
26+
{
27+
every( [ 1, 2, 3 ] ); // $ExpectType boolean
28+
every( new Float64Array( [ 1, 2, 3 ] ) ); // $ExpectType boolean
29+
every( new Float32Array( [ 1, 2, 3 ] ) ); // $ExpectType boolean
30+
every( new Int32Array( [ 1, 2, 3 ] ) ); // $ExpectType boolean
31+
every( new Int16Array( [ 1, 2, 3 ] ) ); // $ExpectType boolean
32+
every( new Int8Array( [ 1, 2, 3 ] ) ); // $ExpectType boolean
33+
every( new Uint32Array( [ 1, 2, 3 ] ) ); // $ExpectType boolean
34+
every( new Uint16Array( [ 1, 2, 3 ] ) ); // $ExpectType boolean
35+
every( new Uint8Array( [ 1, 2, 3 ] ) ); // $ExpectType boolean
36+
every( new Uint8ClampedArray( [ 1, 2, 3 ] ) ); // $ExpectType boolean
37+
every( toAccessorArray( [ 1, 2, 3 ] ) ); // $ExpectType boolean
38+
}
39+
40+
// The compiler throws an error if the function is provided a first argument which is not a collection...
41+
{
42+
every( 2 ); // $ExpectError
43+
every( false ); // $ExpectError
44+
every( true ); // $ExpectError
45+
every( {} ); // $ExpectError
46+
}
47+
48+
// The compiler throws an error if the function is provided an unsupported number of arguments...
49+
{
50+
every(); // $ExpectError
51+
every( [ 1, 2, 3 ], {}, 3 ); // $ExpectError
52+
}
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+
'use strict';
20+
21+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
22+
var every = require( './../lib' );
23+
24+
var x = discreteUniform( 10, 0, 10, {
25+
'dtype': 'int32'
26+
} );
27+
// returns <Int32Array>
28+
29+
var out = every( x );
30+
// returns <boolean>
31+
32+
console.log( x );
33+
console.log( out );

0 commit comments

Comments
 (0)