|
| 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/utils/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/utils/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-by`][@stdlib/utils/any-by]</span><span class="delimiter">: </span><span class="description">test whether at least one element in a collection passes a test implemented by a predicate function.</span> |
| 206 | +- <span class="package-name">[`@stdlib/utils/every-by`][@stdlib/utils/every-by]</span><span class="delimiter">: </span><span class="description">test whether all elements in a collection pass a test implemented by a predicate function.</span> |
| 207 | +- <span class="package-name">[`@stdlib/utils/none-by`][@stdlib/utils/none-by]</span><span class="delimiter">: </span><span class="description">test whether all elements in a collection fail a test implemented by a predicate function.</span> |
| 208 | +- <span class="package-name">[`@stdlib/utils/async/some-by`][@stdlib/utils/async/some-by]</span><span class="delimiter">: </span><span class="description">test whether a collection contains `n` elements which pass a test implemented by a predicate function.</span> |
| 209 | +- <span class="package-name">[`@stdlib/utils/some-by-right`][@stdlib/utils/some-by-right]</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, iterating from right to left.</span> |
| 210 | + |
| 211 | +</section> |
| 212 | + |
| 213 | +<!-- /.related --> |
| 214 | + |
| 215 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 216 | + |
| 217 | +<section class="links"> |
| 218 | +<!-- <related-links> --> |
| 219 | + |
| 220 | +[@stdlib/utils/any-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/any-by |
| 221 | + |
| 222 | +[@stdlib/utils/every-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/every-by |
| 223 | + |
| 224 | +[@stdlib/utils/none-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/none-by |
| 225 | + |
| 226 | +[@stdlib/utils/async/some-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/some-by |
| 227 | + |
| 228 | +[@stdlib/utils/some-by-right]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/some-by-right |
| 229 | + |
| 230 | +<!-- </related-links> --> |
| 231 | + |
| 232 | +</section> |
| 233 | + |
| 234 | +<!-- /.links --> |
0 commit comments