Skip to content

Commit 61f1243

Browse files
committed
Auto-generated commit
1 parent e8f04ea commit 61f1243

File tree

11 files changed

+936
-0
lines changed

11 files changed

+936
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
### Features
1212

1313
- [`5c5dd63`](https://github.com/stdlib-js/stdlib/commit/5c5dd632d992ce0486d5df295af417435d37579b) - add `object/some-in-by`
14+
- [`9764a3e`](https://github.com/stdlib-js/stdlib/commit/9764a3e691011e9f2d957b51fc0b5766b1df9989) - add `object/every-own-by`
1415
- [`3c99007`](https://github.com/stdlib-js/stdlib/commit/3c99007c615b7df61d85d7d88eca7a19ee4efde4) - add `object/every-in-by`
1516

1617
</section>
@@ -24,6 +25,7 @@
2425
<details>
2526

2627
- [`5c5dd63`](https://github.com/stdlib-js/stdlib/commit/5c5dd632d992ce0486d5df295af417435d37579b) - **feat:** add `object/some-in-by` _(by Neeraj Pathak)_
28+
- [`9764a3e`](https://github.com/stdlib-js/stdlib/commit/9764a3e691011e9f2d957b51fc0b5766b1df9989) - **feat:** add `object/every-own-by` _(by Neeraj Pathak)_
2729
- [`3c99007`](https://github.com/stdlib-js/stdlib/commit/3c99007c615b7df61d85d7d88eca7a19ee4efde4) - **feat:** add `object/every-in-by` _(by Neeraj Pathak)_
2830

2931
</details>

every-own-by/README.md

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
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+
# everyOwnBy
22+
23+
> Test whether all own propertes of an object 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 everyOwnBy = require( '@stdlib/object/every-own-by' );
41+
```
42+
43+
#### everyOwnBy( object, predicate\[, thisArg ] )
44+
45+
Tests whether all `own` properties of an object pass a test implemented by a `predicate` function.
46+
47+
```javascript
48+
function isPositive( value ) {
49+
return ( value > 0 );
50+
}
51+
52+
var obj = {
53+
'a': 1,
54+
'b': 2,
55+
'c': 3,
56+
'd': 4
57+
};
58+
59+
var bool = everyOwnBy( obj, isPositive );
60+
// returns true
61+
```
62+
63+
If a `predicate` function returns a non-truthy value, the function **immediately** returns `false`.
64+
65+
```javascript
66+
function isPositive( value ) {
67+
return ( value > 0 );
68+
}
69+
70+
var obj = {
71+
'a': 1,
72+
'b': -2,
73+
'c': 3,
74+
'd': 4
75+
};
76+
77+
var bool = everyOwnBy( obj, isPositive );
78+
// returns false
79+
```
80+
81+
The invoked `function` is provided three arguments:
82+
83+
- **value**: property value.
84+
- **key**: property key.
85+
- **obj**: input object.
86+
87+
To set the function execution context, provide a `thisArg`.
88+
89+
```javascript
90+
function sum( value ) {
91+
if ( value < 0 ) {
92+
return false;
93+
}
94+
this.sum += value;
95+
this.count += 1;
96+
return true;
97+
}
98+
99+
var obj = {
100+
'a': 1,
101+
'b': 2,
102+
'c': 3
103+
};
104+
105+
var context = {
106+
'sum': 0,
107+
'count': 0
108+
};
109+
110+
var bool = everyOwnBy( obj, sum, context );
111+
// returns true
112+
113+
var mean = context.sum / context.count;
114+
// returns 2
115+
```
116+
117+
</section>
118+
119+
<!-- /.usage -->
120+
121+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
122+
123+
<section class="notes">
124+
125+
## Notes
126+
127+
- If the 1st argument is not an [`object`][mdn-object] or the second argument is not a function, the function throws a Type Error.
128+
129+
- If provided an empty object, the function returns `true`.
130+
131+
```javascript
132+
function untrue() {
133+
return false;
134+
}
135+
var bool = everyOwnBy( {}, untrue );
136+
// returns true
137+
```
138+
139+
</section>
140+
141+
<!-- /.notes -->
142+
143+
<!-- Package usage examples. -->
144+
145+
<section class="examples">
146+
147+
## Examples
148+
149+
<!-- eslint no-undef: "error" -->
150+
151+
```javascript
152+
var randu = require( '@stdlib/random/base/randu' );
153+
var everyOwnBy = require( '@stdlib/object/every-own-by' );
154+
155+
function isPositive( value ) {
156+
return ( value > 0 );
157+
}
158+
159+
var obj = {};
160+
var i;
161+
162+
// Populate object with random values
163+
for ( i = 0; i < 100; i++ ) {
164+
obj[ 'prop_' + i ] = randu();
165+
}
166+
167+
var bool = everyOwnBy( obj, isPositive );
168+
// returns <boolean>
169+
```
170+
171+
</section>
172+
173+
<!-- /.examples -->
174+
175+
<!-- 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. -->
176+
177+
<section class="references">
178+
179+
</section>
180+
181+
<!-- /.references -->
182+
183+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
184+
185+
<section class="related">
186+
187+
* * *
188+
189+
## See Also
190+
191+
- <span class="package-name">[`@stdlib/utils/any-own-by`][@stdlib/utils/any-own-by]</span><span class="delimiter">: </span><span class="description">test whether whether any 'own' property of a provided object satisfies a predicate function.</span>
192+
- <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>
193+
- <span class="package-name">[`@stdlib/utils/none-own-by`][@stdlib/utils/none-own-by]</span><span class="delimiter">: </span><span class="description">tests whether every own property of an object fails a test implemented by a predicate function.</span>
194+
- <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>
195+
- <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>
196+
197+
</section>
198+
199+
<!-- /.related -->
200+
201+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
202+
203+
<section class="links">
204+
205+
[mdn-object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
206+
207+
<!-- <related-links> -->
208+
209+
[@stdlib/utils/any-own-by]: https://github.com/stdlib-js/utils-any-own-by
210+
211+
[@stdlib/object/every-in-by]: https://github.com/stdlib-js/object/tree/main/every-in-by
212+
213+
[@stdlib/utils/none-own-by]: https://github.com/stdlib-js/utils-none-own-by
214+
215+
[@stdlib/utils/some-own-by]: https://github.com/stdlib-js/utils-some-own-by
216+
217+
[@stdlib/utils/every-by]: https://github.com/stdlib-js/utils-every-by
218+
219+
<!-- </related-links> -->
220+
221+
</section>
222+
223+
<!-- /.links -->
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 everyOwnBy = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var bool;
34+
var obj;
35+
var i;
36+
37+
b.tic();
38+
for ( i = 0; i < b.iterations; i++ ) {
39+
obj = {
40+
'a': i,
41+
'b': i+1,
42+
'c': i+2,
43+
'd': i+3,
44+
'e': i+4
45+
};
46+
bool = everyOwnBy( obj, predicate );
47+
if ( typeof bool !== 'boolean' ) {
48+
b.fail( 'should return a boolean' );
49+
}
50+
}
51+
b.toc();
52+
if ( !isBoolean( bool ) ) {
53+
b.fail( 'should return a boolean' );
54+
}
55+
b.pass( 'benchmark finished' );
56+
b.end();
57+
58+
function predicate( v ) {
59+
return !isnan( v );
60+
}
61+
});
62+
63+
bench( pkg+'::loop', function benchmark( b ) {
64+
var bool;
65+
var keys;
66+
var obj;
67+
var i;
68+
var j;
69+
70+
b.tic();
71+
for ( i = 0; i < b.iterations; i++ ) {
72+
obj = {
73+
'a': i,
74+
'b': i+1,
75+
'c': i+2,
76+
'd': i+3,
77+
'e': i+4
78+
};
79+
keys = Object.keys( obj );
80+
bool = true;
81+
for ( j = 0; j < keys.length; j++ ) {
82+
if ( isnan( obj[ keys[j] ] ) ) {
83+
bool = false;
84+
break;
85+
}
86+
}
87+
if ( typeof bool !== 'boolean' ) {
88+
b.fail( 'should return a boolean' );
89+
}
90+
}
91+
b.toc();
92+
if ( !isBoolean( bool ) ) {
93+
b.fail( 'should return a boolean' );
94+
}
95+
b.pass( 'benchmark finished' );
96+
b.end();
97+
});

every-own-by/docs/repl.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
{{alias}}( object, predicate[, thisArg ] )
3+
Tests whether every own property of an object pass a test implemented by a
4+
predicate function.
5+
6+
The predicate function is provided three arguments:
7+
8+
- value: property value.
9+
- index: property key.
10+
- object: the input object.
11+
12+
The function immediately returns upon encountering a non-truthy return
13+
value.
14+
15+
If provided an empty object, the function returns `true`.
16+
17+
Parameters
18+
----------
19+
object: Object
20+
Input object.
21+
22+
predicate: Function
23+
Test function.
24+
25+
thisArg: any (optional)
26+
Execution context.
27+
28+
Returns
29+
-------
30+
bool: boolean
31+
The function returns `true` if the predicate function returns a truthy
32+
value for all elements; otherwise, the function returns `false`.
33+
34+
Examples
35+
--------
36+
> function positive( v ) { return ( v > 0 ); };
37+
> var obj = { 'a': 1, 'b': 2, 'c': 3 };
38+
> var bool = {{alias}}( obj, positive )
39+
true
40+
41+
See Also
42+
--------
43+

0 commit comments

Comments
 (0)