Skip to content

Commit b2aacbf

Browse files
feat: add object/any-in-by
Ref: stdlib-js#6754
1 parent a41f30e commit b2aacbf

File tree

10 files changed

+925
-0
lines changed

10 files changed

+925
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
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+
# anyInBy
22+
23+
> Test whether at least one property in an object passes 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 anyInBy = require( '@stdlib/object/any-in-by' );
41+
```
42+
43+
#### anyInBy( object, predicate\[, thisArg ] )
44+
45+
Tests whether at least one property in an `object` passes 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': -1
56+
};
57+
58+
var bool = anyInBy( obj, isNegative );
59+
// returns true
60+
```
61+
62+
If a `predicate` function returns a truthy value, the function **immediately** returns `true`.
63+
64+
```javascript
65+
function isPositive( value ) {
66+
if ( value < 0 ) {
67+
throw new Error( 'should never reach this line' );
68+
}
69+
return ( value > 0 );
70+
}
71+
72+
var obj = {
73+
'a': 1,
74+
'b': -2,
75+
'c': 3
76+
};
77+
78+
var bool = anyInBy( obj, isPositive );
79+
// returns true
80+
```
81+
82+
The invoked `function` is provided three arguments:
83+
84+
- **value**: property value.
85+
- **key**: property key.
86+
- **object**: input object.
87+
88+
To set the function execution context, provide a `thisArg`.
89+
90+
```javascript
91+
function sum( value ) {
92+
this.sum += value;
93+
this.count += 1;
94+
return ( value < 0 );
95+
}
96+
97+
var obj = {
98+
'a': 1,
99+
'b': 2,
100+
'c': 3,
101+
'd': -4
102+
};
103+
104+
var context = {
105+
'sum': 0,
106+
'count': 0
107+
};
108+
109+
var bool = anyInBy( obj, sum, context );
110+
// returns true
111+
112+
var mean = context.sum / context.count;
113+
// returns 0.5
114+
```
115+
116+
</section>
117+
118+
<!-- /.usage -->
119+
120+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
121+
122+
<section class="notes">
123+
124+
## Notes
125+
126+
- The `obj` object may contain own properties as well as inherited properties from its prototype chain.
127+
128+
- If provided an empty object, the function returns `false`.
129+
130+
```javascript
131+
function alwaysTrue() {
132+
return true;
133+
}
134+
var bool = anyInBy( {}, alwaysTrue );
135+
// returns false
136+
```
137+
138+
</section>
139+
140+
<!-- /.notes -->
141+
142+
<!-- Package usage examples. -->
143+
144+
<section class="examples">
145+
146+
## Examples
147+
148+
<!-- eslint no-undef: "error" -->
149+
150+
```javascript
151+
var randu = require( '@stdlib/random/base/randu' );
152+
var anyInBy = require( '@stdlib/object/any-in-by' );
153+
154+
function threshold( value ) {
155+
return ( value > 0.95 );
156+
}
157+
158+
var bool;
159+
var obj = {};
160+
var i;
161+
162+
for ( i = 0; i < 100; i++ ) {
163+
obj[i] = randu();
164+
}
165+
166+
bool = anyInBy( obj, threshold );
167+
// returns <boolean>
168+
```
169+
170+
</section>
171+
172+
<!-- /.examples -->
173+
174+
<!-- 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. -->
175+
176+
<section class="references">
177+
178+
</section>
179+
180+
<!-- /.references -->
181+
182+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
183+
184+
<section class="related">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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 anyInBy = 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': i+3,
48+
'e': i+4,
49+
'f': NaN
50+
};
51+
bool = anyInBy( obj, 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+'::built-in', function benchmark( b ) {
65+
var bool;
66+
var keys;
67+
var obj;
68+
var i;
69+
70+
function predicate( v ) {
71+
return isnan( v );
72+
}
73+
function testPredicate( key ) {
74+
return predicate( obj[ key ] );
75+
}
76+
77+
b.tic();
78+
for ( i = 0; i < b.iterations; i++ ) {
79+
obj = {
80+
'a': i,
81+
'b': i+1,
82+
'c': i+2,
83+
'd': i+3,
84+
'e': i+4,
85+
'f': NaN
86+
};
87+
keys = Object.keys( obj );
88+
bool = keys.some( testPredicate );
89+
if ( typeof bool !== 'boolean' ) {
90+
b.fail( 'should return a boolean' );
91+
}
92+
}
93+
b.toc();
94+
if ( !isBoolean( bool ) ) {
95+
b.fail( 'should return a boolean' );
96+
}
97+
b.pass( 'benchmark finished' );
98+
b.end();
99+
});
100+
101+
bench( pkg+'::loop', function benchmark( b ) {
102+
var bool;
103+
var keys;
104+
var obj;
105+
var i;
106+
var j;
107+
108+
b.tic();
109+
for ( i = 0; i < b.iterations; i++ ) {
110+
obj = {
111+
'a': i,
112+
'b': i+1,
113+
'c': i+2,
114+
'd': i+3,
115+
'e': i+4,
116+
'f': NaN
117+
};
118+
keys = Object.keys( obj );
119+
bool = false;
120+
for ( j = 0; j < keys.length; j++ ) {
121+
if ( isnan( obj[ keys[j] ] ) ) {
122+
bool = true;
123+
break;
124+
}
125+
}
126+
if ( typeof bool !== 'boolean' ) {
127+
b.fail( 'should return a boolean' );
128+
}
129+
}
130+
b.toc();
131+
if ( !isBoolean( bool ) ) {
132+
b.fail( 'should be a boolean' );
133+
}
134+
b.pass( 'benchmark finished' );
135+
b.end();
136+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
{{alias}}( object, predicate[, thisArg ] )
3+
Tests whether at least one value in an object passes a test implemented by
4+
a predicate function.
5+
6+
The predicate function is provided three arguments:
7+
8+
- value: the value of the current property being processed in the object
9+
- key: the key of the current property being processed in the object
10+
- object: the input object
11+
12+
The function immediately returns upon encountering a truthy return value.
13+
14+
If provided an empty object, the function returns `false`.
15+
16+
Parameters
17+
----------
18+
object: Object
19+
Input object over which to iterate. It must be non-null.
20+
21+
predicate: Function
22+
The test function.
23+
24+
thisArg: any (optional)
25+
Execution context.
26+
27+
Returns
28+
-------
29+
bool: boolean
30+
The function returns `true` if the predicate function returns `true` for
31+
any value; otherwise, it returns `false`.
32+
33+
Examples
34+
--------
35+
> function isNegative(value) { return value < 0 }
36+
> var obj = { a: 1, b: -2, c: 3, d: 4 }
37+
> var result = {{alias}}(obj, isNegative)
38+
true
39+
40+
See Also
41+
--------

0 commit comments

Comments
 (0)