Skip to content

Commit 27331ac

Browse files
feat: add object/none-in-by
Ref: #6758
1 parent a41f30e commit 27331ac

File tree

10 files changed

+931
-0
lines changed

10 files changed

+931
-0
lines changed
Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
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+
# noneInBy
22+
23+
> Test whether every property of an object fails 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 noneInBy = require( '@stdlib/object/none-in-by' );
41+
```
42+
43+
#### noneInBy( object, predicate\[, thisArg ] )
44+
45+
Tests whether every property of an `object` fails a test implemented by a `predicate` function.
46+
47+
```javascript
48+
function isUnderage( age ) {
49+
return ( age < 18 );
50+
}
51+
52+
var obj = {
53+
'a': 28,
54+
'b': 22,
55+
'c': 25
56+
};
57+
58+
var bool = noneInBy( obj, isUnderage );
59+
// returns true
60+
```
61+
62+
If a `predicate` function returns a truthy value, the function **immediately** returns `false`.
63+
64+
```javascript
65+
function isUnderage( age ) {
66+
return ( age < 18 );
67+
}
68+
69+
var obj = {
70+
'a': 12,
71+
'b': 22,
72+
'c': 25
73+
};
74+
75+
var bool = noneInBy( obj, isUnderage );
76+
// returns false
77+
```
78+
79+
The invoked `function` is provided three agruments:
80+
81+
- **value**: property value.
82+
- **key**: property key.
83+
- **object**: input object.
84+
85+
To set the function execution context, provide a `thisArg`.
86+
87+
```javascript
88+
function sum( value ) {
89+
if ( value < 0 ) {
90+
return true;
91+
}
92+
this.sum += value;
93+
this.count += 1;
94+
return false;
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 = noneInBy( obj, sum, context );
110+
// returns true
111+
112+
var mean = context.sum / context.count;
113+
// returns 2.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+
- If the 1st argument is not an object or the second argument is not a fuction , the
127+
function throws a Type Error.
128+
129+
- If provided an empty object, the function returns `true`.
130+
131+
```javascript
132+
function truthy() {
133+
return true;
134+
}
135+
var bool = noneInBy( {}, truthy );
136+
// returns true
137+
```
138+
139+
- The function does **not** skip `undefined` elements.
140+
141+
<!-- eslint-disable no-sparse-arrays, stdlib/doctest-marker -->
142+
143+
```javascript
144+
function log( value, index ) {
145+
console.log( '%s: %s', index, value );
146+
return false;
147+
}
148+
149+
var obj = {
150+
'a': 1,
151+
'b': NaN,
152+
'c': NaN,
153+
'd': 4
154+
};
155+
156+
var bool = noneInBy( arr, log );
157+
/* =>
158+
0: 1
159+
1: undefined
160+
2: undefined
161+
3: 4
162+
*/
163+
```
164+
165+
</section>
166+
167+
<!-- /.notes -->
168+
169+
<!-- Package usage examples. -->
170+
171+
<section class="examples">
172+
173+
## Examples
174+
175+
<!-- eslint no-undef: "error" -->
176+
177+
```javascript
178+
var noneInBy = require( '@stdlib/object/none-in-by' );
179+
180+
function isUnderage( age ) {
181+
return age < 18;
182+
}
183+
184+
var obj = {
185+
'a': 26,
186+
'b': 20,
187+
'c': 25
188+
};
189+
190+
var bool = noneInBy( obj, isUnderage );
191+
// returns true
192+
```
193+
194+
</section>
195+
196+
<!-- /.examples -->
197+
198+
<!-- 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. -->
199+
200+
<section class="references">
201+
202+
</section>
203+
204+
<!-- /.references -->
205+
206+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
207+
208+
<section class="related">
209+
210+
* * *
211+
212+
## See Also
213+
214+
- <span class="package-name">[`@stdlib/utils/any-in-by`][@stdlib/utils/any-in-by]</span><span class="delimiter">: </span><span class="description">test whether at least one property in an object passes a test implemented by a predicate function.</span>
215+
- <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>
216+
- <span class="package-name">[`@stdlib/utils/for-in`][@stdlib/utils/for-in]</span><span class="delimiter">: </span><span class="description">invoke a function for each own and inherited enumerable property of an object.</span>
217+
- <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>
218+
- <span class="package-name">[`@stdlib/utils/some-in-by`][@stdlib/utils/some-in-by]</span><span class="delimiter">: </span><span class="description">test whether an object contains at least n properties (own and inherited) which pass a test implemented by a predicate function.</span>
219+
220+
</section>
221+
222+
<!-- /.related -->
223+
224+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
225+
226+
<section class="links">
227+
228+
<!-- <related-links> -->
229+
230+
[@stdlib/utils/any-in-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/any-in-by
231+
232+
[@stdlib/object/every-in-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/object/every-in-by
233+
234+
[@stdlib/utils/for-in]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/for-in
235+
236+
[@stdlib/utils/none-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/none-by
237+
238+
[@stdlib/utils/some-in-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/some-in-by
239+
240+
<!-- </related-links> -->
241+
242+
</section>
243+
244+
<!-- /.links -->
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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 noneInBy = 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+
};
50+
bool = noneInBy( obj, predicate );
51+
if ( typeof bool !== 'boolean' ) {
52+
b.fail( 'should return a boolean' );
53+
}
54+
}
55+
b.toc();
56+
if ( !isBoolean( bool ) ) {
57+
b.fail( 'should return a boolean' );
58+
}
59+
b.pass( 'benchmark finished' );
60+
b.end();
61+
});
62+
63+
bench( pkg+'::for-in-loop', function benchmark( b ) {
64+
var bool;
65+
var obj;
66+
var key;
67+
var i;
68+
69+
b.tic();
70+
for ( i = 0; i < b.iterations; i++ ) {
71+
obj = {
72+
'a': i,
73+
'b': i + 1,
74+
'c': i + 2,
75+
'd': i + 3,
76+
'e': i + 4
77+
};
78+
bool = true;
79+
for ( key in obj ) {
80+
if ( isnan( obj[key] ) ) {
81+
bool = false;
82+
break;
83+
}
84+
}
85+
if ( typeof bool !== 'boolean' ) {
86+
b.fail( 'should return a boolean' );
87+
}
88+
}
89+
b.toc();
90+
if ( !isBoolean( bool ) ) {
91+
b.fail( 'should return a boolean' );
92+
}
93+
b.pass( 'benchmark finished' );
94+
b.end();
95+
});
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
{{alias}}( object, predicate[, thisArg ] )
3+
Tests whether every property in an object fails a test implemented by a
4+
predicate function.
5+
6+
The predicate function is provided three arguments:
7+
8+
- value: object value.
9+
- key: object key.
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 `true`.
15+
16+
Parameters
17+
----------
18+
object: Object
19+
Input object over which to iterate.
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 a falsy
31+
value for all properties; otherwise, the function returns `false`.
32+
33+
Examples
34+
--------
35+
> function negative( v ) { return ( v < 0 ); };
36+
> var obj = { 'a': 1, 'b': 2, 'c': 4 };
37+
> var bool = {{alias}}( obj, negative )
38+
true
39+
40+
See Also
41+
--------
42+

0 commit comments

Comments
 (0)