Skip to content

Commit 9764a3e

Browse files
feat: add object/every-own-by
Ref: #6752
1 parent a41f30e commit 9764a3e

File tree

10 files changed

+935
-0
lines changed

10 files changed

+935
-0
lines changed
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
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 fuction , the
128+
function throws a Type Error.
129+
130+
- If provided an empty object, the function returns `true`.
131+
132+
```javascript
133+
function untrue() {
134+
return false;
135+
}
136+
var bool = everyOwnBy( {}, untrue );
137+
// returns true
138+
```
139+
140+
</section>
141+
142+
<!-- /.notes -->
143+
144+
<!-- Package usage examples. -->
145+
146+
<section class="examples">
147+
148+
## Examples
149+
150+
<!-- eslint no-undef: "error" -->
151+
152+
```javascript
153+
var randu = require( '@stdlib/random/base/randu' );
154+
var everyOwnBy = require( '@stdlib/object/every-own-by' );
155+
156+
function isPositive( value ) {
157+
return ( value > 0 );
158+
}
159+
160+
var obj = {};
161+
var i;
162+
163+
// Populate object with random values
164+
for ( i = 0; i < 100; i++ ) {
165+
obj[ 'prop_' + i ] = randu();
166+
}
167+
168+
var bool = everyOwnBy( obj, isPositive );
169+
// returns <boolean>
170+
```
171+
172+
</section>
173+
174+
<!-- /.examples -->
175+
176+
<!-- 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. -->
177+
178+
<section class="references">
179+
180+
</section>
181+
182+
<!-- /.references -->
183+
184+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
185+
186+
<section class="related">
187+
188+
* * *
189+
190+
## See Also
191+
192+
- <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>
193+
- <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>
194+
- <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>
195+
- <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>
196+
- <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>
197+
198+
</section>
199+
200+
<!-- /.related -->
201+
202+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
203+
204+
<section class="links">
205+
206+
[mdn-object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
207+
208+
<!-- <related-links> -->
209+
210+
[@stdlib/utils/any-own-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/any-own-by
211+
212+
[@stdlib/object/every-in-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/object/every-in-by
213+
214+
[@stdlib/utils/none-own-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/none-own-by
215+
216+
[@stdlib/utils/some-own-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/some-own-by
217+
218+
[@stdlib/utils/every-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/every-by
219+
220+
<!-- </related-links> -->
221+
222+
</section>
223+
224+
<!-- /.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+
});
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)