Skip to content

Commit 2351845

Browse files
feat: add utils/some-in-by
PR-URL: #1412 Closes: #825 Add support to test whether some "own" and inherited properties of a provided object satisfies a predicate function. Signed-off-by: Philipp Burckhardt <[email protected]> Co-authored-by: Philipp Burckhardt <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent ce5ea47 commit 2351845

File tree

10 files changed

+1116
-0
lines changed

10 files changed

+1116
-0
lines changed
Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
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 -->
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pkg = require( './../package.json' ).name;
27+
var someInBy = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var bool;
34+
var obj;
35+
var i;
36+
37+
function predicate( v ) {
38+
return isnan( v );
39+
}
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
obj = {
44+
'a': i,
45+
'b': i + 1,
46+
'c': i + 2,
47+
'd': NaN,
48+
'e': i + 4,
49+
'f': NaN
50+
};
51+
bool = someInBy( obj, 2, predicate );
52+
if ( typeof bool !== 'boolean' ) {
53+
b.fail( 'should return a boolean' );
54+
}
55+
}
56+
b.toc();
57+
if ( !isBoolean( bool ) ) {
58+
b.fail( 'should return a boolean' );
59+
}
60+
b.pass( 'benchmark finished' );
61+
b.end();
62+
});
63+
64+
bench( pkg + '::loop', function benchmark( b ) {
65+
var total;
66+
var count;
67+
var bool;
68+
var obj;
69+
var key;
70+
var i;
71+
72+
total = 2;
73+
74+
b.tic();
75+
for ( i = 0; i < b.iterations; i++ ) {
76+
obj = {
77+
'a': i,
78+
'b': i + 1,
79+
'c': i + 2,
80+
'd': NaN,
81+
'e': i + 4,
82+
'f': NaN
83+
};
84+
bool = false;
85+
count = 0;
86+
for ( key in obj ) {
87+
if ( Object.prototype.hasOwnProperty.call( obj, key ) && isnan(obj[ key ] )) {
88+
count += 1;
89+
if ( count === total ) {
90+
bool = true;
91+
break;
92+
}
93+
}
94+
}
95+
if ( typeof bool !== 'boolean' ) {
96+
b.fail( 'should return a boolean' );
97+
}
98+
}
99+
b.toc();
100+
if ( !isBoolean( bool ) ) {
101+
b.fail( 'should be a boolean' );
102+
}
103+
b.pass( 'benchmark finished' );
104+
b.end();
105+
});
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
{{alias}}( obj, n, predicate[, thisArg ] )
3+
Tests whether an object contains at least `n` properties
4+
(own and inherited) which pass a test
5+
implemented by a predicate function.
6+
7+
The predicate function is provided three arguments:
8+
9+
- `value`: object value
10+
- `key`: object key
11+
- `obj`: the input object
12+
13+
The function immediately returns upon finding `n` successful properties.
14+
15+
If provided an empty object, the function returns `false`.
16+
17+
Parameters
18+
----------
19+
obj: Object
20+
Input object over which to iterate.
21+
22+
n: number
23+
Minimum number of successful properties.
24+
25+
predicate: Function
26+
Test function.
27+
28+
thisArg: any (optional)
29+
Execution context.
30+
31+
Returns
32+
-------
33+
bool: boolean
34+
The function returns `true` if an object contains at least `n`
35+
successful properties; otherwise, the function returns `false`.
36+
37+
Examples
38+
--------
39+
> function negative( v ) { return ( v < 0 ); };
40+
> var obj = { 'a': 1, 'b': 2, 'c': -3, 'd': 4, 'e': -1 };
41+
> var bool = {{alias}}( obj, 2, negative )
42+
true
43+
44+
See Also
45+
--------

0 commit comments

Comments
 (0)