| 
 | 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 | +# someInBy  | 
 | 22 | + | 
 | 23 | +> Test whether an object contains at least `n` properties which pass a test implemented by a predicate function.  | 
 | 24 | +
  | 
 | 25 | +<section class="intro">  | 
 | 26 | + | 
 | 27 | +</section>  | 
 | 28 | + | 
 | 29 | +<!-- /.intro -->  | 
 | 30 | + | 
 | 31 | +<section class="usage">  | 
 | 32 | + | 
 | 33 | +## Usage  | 
 | 34 | + | 
 | 35 | +```javascript  | 
 | 36 | +var someInBy = require( '@stdlib/object/some-in-by' );  | 
 | 37 | +```  | 
 | 38 | + | 
 | 39 | +#### someInBy( obj, n, predicate\[, thisArg ] )  | 
 | 40 | + | 
 | 41 | +Tests whether an `obj` contains at least `n` properties which pass a test implemented by a `predicate` function.  | 
 | 42 | + | 
 | 43 | +```javascript  | 
 | 44 | +function isNegative( value ) {  | 
 | 45 | + return ( value < 0 );  | 
 | 46 | +}  | 
 | 47 | + | 
 | 48 | +var obj = {  | 
 | 49 | + 'a': 1,  | 
 | 50 | + 'b': -2,  | 
 | 51 | + 'c': 3,  | 
 | 52 | + 'd': -1  | 
 | 53 | +};  | 
 | 54 | + | 
 | 55 | +var bool = someInBy( obj, 2, isNegative );  | 
 | 56 | +// returns true  | 
 | 57 | +```  | 
 | 58 | + | 
 | 59 | +Once the function finds `n` successful properties, the function **immediately** returns `true`.  | 
 | 60 | + | 
 | 61 | +```javascript  | 
 | 62 | +function isPositive( value ) {  | 
 | 63 | + if ( value < 0 ) {  | 
 | 64 | + throw new Error( 'should never reach this line' );  | 
 | 65 | + }  | 
 | 66 | + return ( value > 0 );  | 
 | 67 | +}  | 
 | 68 | + | 
 | 69 | +var obj = {  | 
 | 70 | + 'a': 1,  | 
 | 71 | + 'b': 2,  | 
 | 72 | + 'c': -3,  | 
 | 73 | + 'd': 4  | 
 | 74 | +};  | 
 | 75 | + | 
 | 76 | +var bool = someInBy( obj, 2, isPositive );  | 
 | 77 | +// returns true  | 
 | 78 | +```  | 
 | 79 | + | 
 | 80 | +The invoked `function` is provided three arguments:  | 
 | 81 | + | 
 | 82 | +- **value**: object property value.  | 
 | 83 | +- **key**: object property key.  | 
 | 84 | +- **obj**: input object.  | 
 | 85 | + | 
 | 86 | +To set the function execution context, provide a `thisArg`.  | 
 | 87 | + | 
 | 88 | +```javascript  | 
 | 89 | +function sum( value ) {  | 
 | 90 | + this.sum += value;  | 
 | 91 | + this.count += 1;  | 
 | 92 | + return ( value < 0 );  | 
 | 93 | +}  | 
 | 94 | + | 
 | 95 | +var obj = {  | 
 | 96 | + 'a': 1,  | 
 | 97 | + 'b': 2,  | 
 | 98 | + 'c': 3,  | 
 | 99 | + 'd': -5  | 
 | 100 | +};  | 
 | 101 | + | 
 | 102 | +var context = {  | 
 | 103 | + 'sum': 0,  | 
 | 104 | + 'count': 0  | 
 | 105 | +};  | 
 | 106 | + | 
 | 107 | +var bool = someInBy( obj, 1, sum, context );  | 
 | 108 | +// returns true  | 
 | 109 | + | 
 | 110 | +var mean = context.sum / context.count;  | 
 | 111 | +// returns 0.25  | 
 | 112 | +```  | 
 | 113 | + | 
 | 114 | +</section>  | 
 | 115 | + | 
 | 116 | +<!-- /.usage -->  | 
 | 117 | + | 
 | 118 | +<section class="notes">  | 
 | 119 | + | 
 | 120 | +## Notes  | 
 | 121 | + | 
 | 122 | +- If provided an empty `obj`, the function returns `false`.  | 
 | 123 | + | 
 | 124 | + ```javascript  | 
 | 125 | + function alwaysTrue() {  | 
 | 126 | + return true;  | 
 | 127 | + }  | 
 | 128 | + var bool = someInBy( {}, 1, alwaysTrue );  | 
 | 129 | + // returns false  | 
 | 130 | + ```  | 
 | 131 | + | 
 | 132 | +- The function does **not** skip `undefined` properties.  | 
 | 133 | + | 
 | 134 | + ```javascript  | 
 | 135 | + function log( value, key ) {  | 
 | 136 | + console.log( '%s: %s', key, value );  | 
 | 137 | + return ( value < 0 );  | 
 | 138 | + }  | 
 | 139 | + | 
 | 140 | + var obj = {  | 
 | 141 | + 'a': 1,  | 
 | 142 | + 'b': void 0,  | 
 | 143 | + 'c': void 0,  | 
 | 144 | + 'd': 4,  | 
 | 145 | + 'e': -1  | 
 | 146 | + };  | 
 | 147 | + | 
 | 148 | + var bool = someInBy( obj, 1, log );  | 
 | 149 | + // logs  | 
 | 150 | + // a: 1  | 
 | 151 | + // b: void 0  | 
 | 152 | + // c: void 0  | 
 | 153 | + // d: 4  | 
 | 154 | + // e: -1  | 
 | 155 | + ```  | 
 | 156 | + | 
 | 157 | +- The function provides limited support for dynamic objects (i.e., objects whose properties change during execution).  | 
 | 158 | + | 
 | 159 | +</section>  | 
 | 160 | + | 
 | 161 | +<!-- /.notes -->  | 
 | 162 | + | 
 | 163 | +<section class="examples">  | 
 | 164 | + | 
 | 165 | +## Examples  | 
 | 166 | + | 
 | 167 | +```javascript  | 
 | 168 | +var randu = require( '@stdlib/random/base/randu' );  | 
 | 169 | +var someInBy = require( '@stdlib/object/some-in-by' );  | 
 | 170 | + | 
 | 171 | +function threshold( value ) {  | 
 | 172 | + return ( value > 0.95 );  | 
 | 173 | +}  | 
 | 174 | + | 
 | 175 | +var bool;  | 
 | 176 | +var obj = {};  | 
 | 177 | +var i;  | 
 | 178 | + | 
 | 179 | +for ( i = 0; i < 100; i++ ) {  | 
 | 180 | + obj[ 'key' + i ] = randu();  | 
 | 181 | +}  | 
 | 182 | + | 
 | 183 | +bool = someInBy( obj, 5, threshold );  | 
 | 184 | +// returns <boolean>  | 
 | 185 | +```  | 
 | 186 | + | 
 | 187 | +</section>  | 
 | 188 | + | 
 | 189 | +<!-- /.examples -->  | 
 | 190 | + | 
 | 191 | +<section class="references">  | 
 | 192 | + | 
 | 193 | +</section>  | 
 | 194 | + | 
 | 195 | +<!-- /.references -->  | 
 | 196 | + | 
 | 197 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->  | 
 | 198 | + | 
 | 199 | +<section class="related">  | 
 | 200 | + | 
 | 201 | +* * *  | 
 | 202 | + | 
 | 203 | +## See Also  | 
 | 204 | + | 
 | 205 | +- <span class="package-name">[`@stdlib/utils/any-in-by`][@stdlib/utils/any-in-by]</span><span class="delimiter">: </span><span class="description">test whether at least one property in an object passes a test implemented by a predicate function.</span>  | 
 | 206 | +- <span class="package-name">[`@stdlib/object/every-in-by`][@stdlib/object/every-in-by]</span><span class="delimiter">: </span><span class="description">test whether all properties (own and inherited) of an object pass a test implemented by a predicate function.</span>  | 
 | 207 | +- <span class="package-name">[`@stdlib/utils/some-by`][@stdlib/utils/some-by]</span><span class="delimiter">: </span><span class="description">test whether a collection contains at least `n` elements which pass a test implemented by a predicate function.</span>  | 
 | 208 | +- <span class="package-name">[`@stdlib/utils/some-own-by`][@stdlib/utils/some-own-by]</span><span class="delimiter">: </span><span class="description">test whether some `own` properties of a provided object satisfy a predicate function for at least `n` properties.</span>  | 
 | 209 | + | 
 | 210 | +</section>  | 
 | 211 | + | 
 | 212 | +<!-- /.related -->  | 
 | 213 | + | 
 | 214 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->  | 
 | 215 | + | 
 | 216 | +<section class="links">  | 
 | 217 | + | 
 | 218 | +<!-- <related-links> -->  | 
 | 219 | + | 
 | 220 | +[@stdlib/utils/any-in-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/any-in-by  | 
 | 221 | + | 
 | 222 | +[@stdlib/object/every-in-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/object/every-in-by  | 
 | 223 | + | 
 | 224 | +[@stdlib/utils/some-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/some-by  | 
 | 225 | + | 
 | 226 | +[@stdlib/utils/some-own-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/some-own-by  | 
 | 227 | + | 
 | 228 | +<!-- </related-links> -->  | 
 | 229 | + | 
 | 230 | +</section>  | 
 | 231 | + | 
 | 232 | +<!-- /.links -->  | 
0 commit comments