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