Skip to content

Commit 4226d6d

Browse files
committed
feat: add array/base/any-has-property
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 73cfffd commit 4226d6d

File tree

10 files changed

+784
-0
lines changed

10 files changed

+784
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# anyHasProp
22+
23+
> Test whether at least one element in a provided array has a specified property, either own or inherited.
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 anyHasProp = require( '@stdlib/array/base/any-has-property' );
41+
```
42+
43+
#### anyHasProp( arr, property )
44+
45+
Tests whether at least one element in a provided array has a specified property, either own or inherited.
46+
47+
```javascript
48+
var o1 = {
49+
'a': 1
50+
};
51+
var o2 = {
52+
'b': 2
53+
};
54+
var o3 = {
55+
'c': 3
56+
};
57+
58+
var bool = anyHasProp( [ o1, o2, o3 ], 'b' );
59+
// returns true
60+
61+
bool = anyHasProp( [ o1, o2, o3 ], 'd' );
62+
// returns false
63+
64+
bool = anyHasProp( [ o1, o2, o3 ], 'toString' );
65+
// returns true
66+
```
67+
68+
</section>
69+
70+
<!-- /.usage -->
71+
72+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
73+
74+
<section class="notes">
75+
76+
</section>
77+
78+
<!-- /.notes -->
79+
80+
<!-- Package usage examples. -->
81+
82+
<section class="examples">
83+
84+
## Examples
85+
86+
<!-- eslint no-undef: "error" -->
87+
88+
```javascript
89+
var fromCodePoint = require( '@stdlib/string/from-code-point' );
90+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
91+
var filledBy = require( '@stdlib/array/filled-by' );
92+
var anyHasProp = require( '@stdlib/array/base/any-has-property' );
93+
94+
function randomObject() {
95+
var o = {};
96+
o[ fromCodePoint( 97+discreteUniform( 0, 25 ) ) ] = 0;
97+
return o;
98+
}
99+
100+
var arr = filledBy( 10, 'generic', randomObject );
101+
console.log( arr );
102+
103+
var bool = anyHasProp( arr, 'a' );
104+
console.log( 'a: %s', bool );
105+
106+
bool = anyHasProp( arr, 'b' );
107+
console.log( 'b: %s', bool );
108+
```
109+
110+
</section>
111+
112+
<!-- /.examples -->
113+
114+
<!-- 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. -->
115+
116+
<section class="references">
117+
118+
</section>
119+
120+
<!-- /.references -->
121+
122+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
123+
124+
<section class="related">
125+
126+
</section>
127+
128+
<!-- /.related -->
129+
130+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
131+
132+
<section class="links">
133+
134+
</section>
135+
136+
<!-- /.links -->
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 pow = require( '@stdlib/math/base/special/pow' );
25+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
26+
var filled = require( '@stdlib/array/filled' );
27+
var pkg = require( './../package.json' ).name;
28+
var anyHasProp = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
var x = filled( {}, len, 'generic' );
42+
return benchmark;
43+
44+
/**
45+
* Benchmark function.
46+
*
47+
* @private
48+
* @param {Benchmark} b - benchmark instance
49+
*/
50+
function benchmark( b ) {
51+
var out;
52+
var i;
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
out = anyHasProp( x, 'foo' );
57+
if ( typeof out !== 'boolean' ) {
58+
b.fail( 'should return a boolean' );
59+
}
60+
}
61+
b.toc();
62+
if ( !isBoolean( out ) ) {
63+
b.fail( 'should return a boolean' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
}
68+
}
69+
70+
71+
// MAIN //
72+
73+
/**
74+
* Main execution sequence.
75+
*
76+
* @private
77+
*/
78+
function main() {
79+
var len;
80+
var min;
81+
var max;
82+
var f;
83+
var i;
84+
85+
min = 1; // 10^min
86+
max = 6; // 10^max
87+
88+
for ( i = min; i <= max; i++ ) {
89+
len = pow( 10, i );
90+
91+
f = createBenchmark( len );
92+
bench( pkg+':dtype=generic,len='+len, f );
93+
}
94+
}
95+
96+
main();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
{{alias}}( arr, property )
3+
Tests whether at least one element in a provided indexed array has a
4+
specified property, either own or inherited.
5+
6+
Parameters
7+
----------
8+
arr: Array|TypedArray|Object
9+
Input array.
10+
11+
property: string|symbol|number
12+
Property.
13+
14+
Returns
15+
-------
16+
bool: boolean
17+
Result.
18+
19+
Examples
20+
--------
21+
> var o1 = { 'a': 1 };
22+
> var o2 = { 'b': 2 };
23+
> var o3 = { 'c': 3 };
24+
> var x = [ o1, o2, o3 ];
25+
> var out = {{alias}}( x, 'a' )
26+
true
27+
> out = {{alias}}( x, 'd' )
28+
false
29+
> out = {{alias}}( x, 'toString' )
30+
true
31+
32+
See Also
33+
--------
34+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
import { Collection, AccessorArrayLike } from '@stdlib/types/array';
24+
import { PropertyName } from '@stdlib/types/object';
25+
26+
/**
27+
* Tests whether at least one element in a provided array has a specified property, either own or inherited.
28+
*
29+
* @param arr - input array
30+
* @param prop - property
31+
* @returns result
32+
*
33+
* @example
34+
* var o1 = {
35+
* 'a': 1
36+
* };
37+
* var o2 = {
38+
* 'b': 2
39+
* };
40+
* var o3 = {
41+
* 'c': 3
42+
* };
43+
*
44+
* var bool = anyHasProp( [ o1, o2, o3 ], 'b' );
45+
* // returns true
46+
*
47+
* bool = anyHasProp( [ o1, o2, o3 ], 'd' );
48+
* // returns false
49+
*
50+
* bool = anyHasProp( [ o1, o2, o3 ], 'toString' );
51+
* // returns true
52+
*/
53+
declare function anyHasProp( arr: Collection<unknown> | AccessorArrayLike<unknown>, prop: PropertyName | number ): boolean;
54+
55+
56+
// EXPORTS //
57+
58+
export = anyHasProp;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
import toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
20+
import anyHasProp = require( './index' );
21+
22+
23+
// TESTS //
24+
25+
// The function returns a boolean...
26+
{
27+
const x = [ 1, 2, 3 ];
28+
29+
anyHasProp( x, 'a' ); // $ExpectType boolean
30+
anyHasProp( new Float64Array( x ), 'a' ); // $ExpectType boolean
31+
anyHasProp( new Float32Array( x ), 'a' ); // $ExpectType boolean
32+
anyHasProp( new Int32Array( x ), 'a' ); // $ExpectType boolean
33+
anyHasProp( new Int16Array( x ), 'a' ); // $ExpectType boolean
34+
anyHasProp( new Int8Array( x ), 'a' ); // $ExpectType boolean
35+
anyHasProp( new Uint32Array( x ), 'a' ); // $ExpectType boolean
36+
anyHasProp( new Uint16Array( x ), 'a' ); // $ExpectType boolean
37+
anyHasProp( new Uint8Array( x ), 'a' ); // $ExpectType boolean
38+
anyHasProp( new Uint8ClampedArray( x ), 'a' ); // $ExpectType boolean
39+
anyHasProp( toAccessorArray( x ), 'a' ); // $ExpectType boolean
40+
}
41+
42+
// The compiler throws an error if the function is provided a first argument which is not a collection...
43+
{
44+
anyHasProp( 2, 'a' ); // $ExpectError
45+
anyHasProp( null, 'a' ); // $ExpectError
46+
anyHasProp( false, 'a' ); // $ExpectError
47+
anyHasProp( true, 'a' ); // $ExpectError
48+
anyHasProp( {}, 'a' ); // $ExpectError
49+
anyHasProp( ( x: number ): number => x, 'a' ); // $ExpectError
50+
}
51+
52+
// The compiler throws an error if the function is provided a second argument which is not a valid property...
53+
{
54+
const x = [ 1, 2, 3 ];
55+
56+
anyHasProp( x, null ); // $ExpectError
57+
anyHasProp( x, false ); // $ExpectError
58+
anyHasProp( x, true ); // $ExpectError
59+
anyHasProp( x, {} ); // $ExpectError
60+
anyHasProp( x, [] ); // $ExpectError
61+
anyHasProp( x, ( x: number ): number => x ); // $ExpectError
62+
}
63+
64+
// The compiler throws an error if the function is provided an unsupported number of arguments...
65+
{
66+
const x = [ 1, 2, 3 ];
67+
68+
anyHasProp(); // $ExpectError
69+
anyHasProp( x ); // $ExpectError
70+
anyHasProp( x, 'a', {} ); // $ExpectError
71+
}

0 commit comments

Comments
 (0)