Skip to content

Commit 6351cf0

Browse files
feat: add object/none-own-by
Ref: stdlib-js#6804
1 parent a41f30e commit 6351cf0

File tree

10 files changed

+795
-0
lines changed

10 files changed

+795
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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+
# noneOwnBy
22+
23+
> Tests whether every own 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 noneOwnBy = require( '@stdlib/object/none-own-by' );
41+
```
42+
43+
#### noneOwnBy( object, predicate\[, thisArg ] )
44+
45+
Tests whether every `own` 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 = noneOwnBy( 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 = noneOwnBy( obj, isUnderage );
76+
// returns false
77+
```
78+
79+
</section>
80+
81+
<!-- /.usage -->
82+
83+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
84+
85+
<section class="notes">
86+
87+
## Notes
88+
89+
- If the 1st argument is not an object or the second argument is not a fuction , the function throws a Type Error.
90+
91+
- If provided an empty object, the function returns `true`.
92+
93+
```javascript
94+
function truthy() {
95+
return true;
96+
}
97+
var bool = noneOwnBy( {}, truthy );
98+
// returns true
99+
```
100+
101+
</section>
102+
103+
<!-- /.notes -->
104+
105+
<!-- Package usage examples. -->
106+
107+
<section class="examples">
108+
109+
## Examples
110+
111+
<!-- eslint no-undef: "error" -->
112+
113+
```javascript
114+
var noneOwnBy = require( '@stdlib/object/none-own-by' );
115+
116+
function isUnderage( age ) {
117+
return age < 18;
118+
}
119+
120+
var obj = {
121+
'a': 26,
122+
'b': 20,
123+
'c': 25
124+
};
125+
126+
var bool = noneOwnBy( obj, isUnderage );
127+
// returns true
128+
```
129+
130+
</section>
131+
132+
<!-- /.examples -->
133+
134+
<!-- 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. -->
135+
136+
<section class="references">
137+
138+
</section>
139+
140+
<!-- /.references -->
141+
142+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
143+
144+
<section class="related">
145+
146+
* * *
147+
148+
## See Also
149+
150+
- <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>
151+
- <span class="package-name">[`@stdlib/utils/every-own-by`][@stdlib/utils/every-own-by]</span><span class="delimiter">: </span><span class="description">test whether all own properties of an object pass a test implemented by a predicate function.</span>
152+
- <span class="package-name">[`@stdlib/utils/for-own`][@stdlib/utils/for-own]</span><span class="delimiter">: </span><span class="description">invoke a function for each own enumerable property of an object.</span>
153+
- <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>
154+
- <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>
155+
156+
</section>
157+
158+
<!-- /.related -->
159+
160+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
161+
162+
<section class="links">
163+
164+
<!-- <related-links> -->
165+
166+
[@stdlib/utils/any-own-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/any-own-by
167+
168+
[@stdlib/utils/every-own-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/every-own-by
169+
170+
[@stdlib/utils/for-own]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/for-own
171+
172+
[@stdlib/utils/none-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/none-by
173+
174+
[@stdlib/utils/some-own-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/some-own-by
175+
176+
<!-- </related-links> -->
177+
178+
</section>
179+
180+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 noneOwnBy = 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+
};
49+
bool = noneOwnBy( obj, predicate );
50+
if ( typeof bool !== 'boolean' ) {
51+
b.fail( 'should return a boolean' );
52+
}
53+
}
54+
b.toc();
55+
if ( !isBoolean( bool ) ) {
56+
b.fail( 'should return a boolean' );
57+
}
58+
b.pass( 'benchmark finished' );
59+
b.end();
60+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
{{alias}}( object, predicate[, thisArg ] )
3+
Tests whether every own property of an object fails a test implemented
4+
by a 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 truthy return value.
13+
14+
If provided an empty object, the function returns `true`.
15+
16+
Parameters
17+
----------
18+
object: Object
19+
Input object.
20+
21+
predicate: Function
22+
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 own properties; otherwise, the function returns `false`.
32+
33+
Examples
34+
--------
35+
> function isUnderage( v ) { return ( v < 18 ); };
36+
> var obj = { 'a': 11, 'b': 12, 'c': 22 };
37+
> var bool = {{alias}}( obj, isUnderage )
38+
false
39+
40+
See Also
41+
--------
42+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
24+
/**
25+
* Checks whether an own property of the object passes the test.
26+
*
27+
* @returns boolean indicating whether an own property of the object passes the test
28+
*/
29+
type Nullary<U> = ( this: U ) => boolean;
30+
31+
/**
32+
* Checks whether an own property of the object passes the test.
33+
*
34+
* @param value - property value
35+
* @returns boolean indicating whether an own property of the object passes the test
36+
*/
37+
type Unary<T, U> = ( this: U, value: T ) => boolean;
38+
39+
/**
40+
* Checks whether an own property of the object passes the test.
41+
*
42+
* @param value - property value
43+
* @param key - property key
44+
* @returns boolean indicating whether an own property of the object passes the test
45+
*/
46+
type Binary<T, U> = ( this: U, value: T, key: number ) => boolean;
47+
48+
/**
49+
* Checks whether an own property of the object passes the test.
50+
*
51+
* @param value - property value
52+
* @param key - property key
53+
* @param object - input object
54+
* @returns boolean indicating whether an own property of the object passes the test
55+
*/
56+
type Ternary<T, U> = ( this: U, value: T, key: number, object: Object ) => boolean;
57+
58+
/**
59+
* Checks whether an own property of the object passes the test.
60+
*
61+
* @param value - object value
62+
* @param key - object key
63+
* @param object - input object
64+
* @returns boolean indicating whether an own property of the object passes the test
65+
*/
66+
type Predicate<T, U> = Nullary<U> | Unary<T, U> | Binary<T, U> | Ternary<T, U>;
67+
68+
/**
69+
* Tests whether every property of an object fails a test implemented by a predicate function.
70+
*
71+
* ## Notes
72+
*
73+
* - The predicate function is provided three arguments:
74+
*
75+
* - `value`: property value
76+
* - `key`: property key
77+
* - `object`: the input object
78+
*
79+
* - The function immediately returns upon encountering a truthy return value.
80+
* - If provided an empty object, the function returns `true`.
81+
*
82+
* @param object - input object
83+
* @param predicate - test function
84+
* @param thisArg - execution context
85+
* @returns boolean indicating whether every property fails a test
86+
*
87+
* @example
88+
* function isUnderage( v ) {
89+
* return ( v < 18 );
90+
* }
91+
*
92+
* var obj = { 'a': 20, 'b': 22, 'c': 25 };
93+
*
94+
* var bool = noneOwnBy( obj, isUnderage );
95+
* // returns true
96+
*/
97+
declare function noneOwnBy<T = unknown, U = unknown>( object: Record<string, unknown>, predicate: Predicate<T, U>, thisArg?: ThisParameterType<Predicate<T, U>> ): boolean;
98+
99+
100+
// EXPORTS //
101+
102+
export = noneOwnBy;

0 commit comments

Comments
 (0)