|
| 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 --> |
0 commit comments