README.md (4897B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 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/utils/every-by' ); 41 ``` 42 43 #### everyBy( collection, predicate\[, thisArg ] ) 44 45 Tests whether all elements in a `collection` pass a test implemented by a `predicate` function. 46 47 ```javascript 48 function isPositive( value ) { 49 return ( value > 0 ); 50 } 51 52 var arr = [ 1, 2, 3, 4 ]; 53 54 var bool = everyBy( arr, 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 arr = [ 1, -2, 3, 4 ]; 66 67 var bool = everyBy( arr, isPositive ); 68 // returns false 69 ``` 70 71 The invoked `function` is provided three arguments: 72 73 - `value`: collection element 74 - `index`: collection index 75 - `collection`: input collection 76 77 To set the 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 arr = [ 1, 2, 3, 4 ]; 90 91 var context = { 92 'sum': 0, 93 'count': 0 94 }; 95 96 var bool = everyBy( arr, 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 - A `collection` may be either an [`Array`][mdn-array], [`Typed Array`][mdn-typed-array], or an array-like [`Object`][mdn-object] (excluding `strings` and `functions`). 114 115 - If provided an empty collection, the function returns `true`. 116 117 ```javascript 118 function untrue() { 119 return false; 120 } 121 var bool = everyBy( [], untrue ); 122 // returns true 123 ``` 124 125 - The function differs from [`Array.prototype.every`][mdn-array-every] in the following ways: 126 127 - The function does **not** skip `undefined` elements. 128 129 <!-- eslint-disable no-sparse-arrays, stdlib/doctest-marker --> 130 131 ```javascript 132 function log( value, index ) { 133 console.log( '%s: %s', index, value ); 134 return true; 135 } 136 137 var arr = [ 1, , , 4 ]; 138 139 var bool = everyBy( arr, log ); 140 /* => 141 0: 1 142 1: undefined 143 2: undefined 144 3: 4 145 */ 146 ``` 147 148 - The function provides limited support for dynamic collections (i.e., collections whose `length` changes during execution). 149 150 </section> 151 152 <!-- /.notes --> 153 154 <!-- Package usage examples. --> 155 156 <section class="examples"> 157 158 ## Examples 159 160 <!-- eslint no-undef: "error" --> 161 162 ```javascript 163 var randu = require( '@stdlib/random/base/randu' ); 164 var everyBy = require( '@stdlib/utils/every-by' ); 165 166 function isPositive( value ) { 167 return ( value > 0 ); 168 } 169 170 var bool; 171 var arr; 172 var i; 173 174 arr = new Array( 100 ); 175 for ( i = 0; i < arr.length; i++ ) { 176 arr[ i ] = randu(); 177 } 178 179 bool = everyBy( arr, isPositive ); 180 // returns <boolean> 181 ``` 182 183 </section> 184 185 <!-- /.examples --> 186 187 <!-- 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. --> 188 189 <section class="references"> 190 191 </section> 192 193 <!-- /.references --> 194 195 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 196 197 <section class="links"> 198 199 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array 200 201 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray 202 203 [mdn-object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object 204 205 [mdn-array-every]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every 206 207 </section> 208 209 <!-- /.links -->