README.md (4757B)
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 # someBy 22 23 > Test whether a collection contains at least `n` elements which 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 someBy = require( '@stdlib/utils/some-by' ); 41 ``` 42 43 #### someBy( collection, n, predicate\[, thisArg ] ) 44 45 Tests whether a `collection` contains at least `n` elements which pass a test implemented by a `predicate` function. 46 47 ```javascript 48 function isNegative( value ) { 49 return ( value < 0 ); 50 } 51 52 var arr = [ 1, -2, 3, 4, -1 ]; 53 54 var bool = someBy( arr, 2, isNegative ); 55 // returns true 56 ``` 57 58 Once the function finds `n` successful elements, the function **immediately** returns `true`. 59 60 ```javascript 61 function isPositive( value ) { 62 if ( value < 0 ) { 63 throw new Error( 'should never reach this line' ); 64 } 65 return ( value > 0 ); 66 } 67 68 var arr = [ 1, 2, -3, 4 ]; 69 70 var bool = someBy( arr, 2, isPositive ); 71 // returns true 72 ``` 73 74 The invoked `function` is provided three arguments: 75 76 - `value`: collection element 77 - `index`: collection index 78 - `collection`: input collection 79 80 To set the function execution context, provide a `thisArg`. 81 82 ```javascript 83 function sum( value ) { 84 this.sum += value; 85 this.count += 1; 86 return ( value < 0 ); 87 } 88 89 var arr = [ 1, 2, 3, 4, -5 ]; 90 91 var context = { 92 'sum': 0, 93 'count': 0 94 }; 95 96 var bool = someBy( arr, 1, sum, context ); 97 // returns true 98 99 var mean = context.sum / context.count; 100 // returns 1.0 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 `false`. 116 117 ```javascript 118 function alwaysTrue() { 119 return true; 120 } 121 var bool = someBy( [], 1, alwaysTrue ); 122 // returns false 123 ``` 124 125 - The function does **not** skip `undefined` elements. 126 127 <!-- eslint-disable no-sparse-arrays, stdlib/doctest-marker --> 128 129 ```javascript 130 function log( value, index ) { 131 console.log( '%s: %s', index, value ); 132 return ( value < 0 ); 133 } 134 135 var arr = [ 1, , , 4, -1 ]; 136 137 var bool = someBy( arr, 1, log ); 138 /* => 139 0: 1 140 1: undefined 141 2: undefined 142 3: 4 143 4: -1 144 */ 145 ``` 146 147 - The function provides limited support for dynamic collections (i.e., collections whose `length` changes during execution). 148 149 </section> 150 151 <!-- /.notes --> 152 153 <!-- Package usage examples. --> 154 155 <section class="examples"> 156 157 ## Examples 158 159 <!-- eslint no-undef: "error" --> 160 161 ```javascript 162 var randu = require( '@stdlib/random/base/randu' ); 163 var someBy = require( '@stdlib/utils/some-by' ); 164 165 function threshold( value ) { 166 return ( value > 0.95 ); 167 } 168 169 var bool; 170 var arr; 171 var i; 172 173 arr = new Array( 100 ); 174 for ( i = 0; i < arr.length; i++ ) { 175 arr[ i ] = randu(); 176 } 177 178 bool = someBy( arr, 5, threshold ); 179 // returns <boolean> 180 ``` 181 182 </section> 183 184 <!-- /.examples --> 185 186 <!-- 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. --> 187 188 <section class="references"> 189 190 </section> 191 192 <!-- /.references --> 193 194 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 195 196 <section class="links"> 197 198 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array 199 200 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray 201 202 [mdn-object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object 203 204 </section> 205 206 <!-- /.links -->