Skip to content

Commit 67b59c6

Browse files
committed
feat: add assert/is-struct-constructor-like
--- 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 cb44d2a commit 67b59c6

File tree

10 files changed

+625
-0
lines changed

10 files changed

+625
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
# isStructConstructorLike
22+
23+
> Test if a value is [struct constructor][@stdlib/dstructs/struct]-like.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var isStructConstructorLike = require( '@stdlib/assert/is-struct-constructor-like' );
31+
```
32+
33+
#### isStructConstructorLike( value )
34+
35+
Tests if a value is [struct constructor][@stdlib/dstructs/struct]-like.
36+
37+
```javascript
38+
var structFactory = require( '@stdlib/dstructs/struct' );
39+
40+
var schema = [
41+
{
42+
'name': 'value',
43+
'type': 'float64'
44+
}
45+
];
46+
var Struct = structFactory( schema );
47+
48+
var bool = isStructConstructorLike( Struct );
49+
// returns true
50+
```
51+
52+
</section>
53+
54+
<!-- /.usage -->
55+
56+
<section class="examples">
57+
58+
## Examples
59+
60+
<!-- eslint no-undef: "error" -->
61+
62+
```javascript
63+
var structFactory = require( '@stdlib/dstructs/struct' );
64+
var isStructConstructorLike = require( '@stdlib/assert/is-struct-constructor-like' );
65+
66+
var schema = [
67+
{
68+
'name': 'value',
69+
'type': 'float64'
70+
}
71+
];
72+
73+
var bool = isStructConstructorLike( structFactory( schema ) );
74+
// returns true
75+
76+
bool = isStructConstructorLike( [ 1, 2, 3, 4 ] );
77+
// returns false
78+
79+
bool = isStructConstructorLike( {} );
80+
// returns false
81+
82+
bool = isStructConstructorLike( null );
83+
// returns false
84+
```
85+
86+
</section>
87+
88+
<!-- /.examples -->
89+
90+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
91+
92+
<section class="related">
93+
94+
</section>
95+
96+
<!-- /.related -->
97+
98+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
99+
100+
<section class="links">
101+
102+
[@stdlib/dstructs/struct]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/dstructs/struct
103+
104+
</section>
105+
106+
<!-- /.links -->
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
25+
var Float64Results = require( '@stdlib/stats/base/ztest/one-sample/results/float64' );
26+
var pkg = require( './../package.json' ).name;
27+
var isStructConstructorLike = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+'::true', function benchmark( b ) {
33+
var values;
34+
var bool;
35+
var i;
36+
37+
values = [
38+
Float64Results,
39+
Float64Results
40+
];
41+
42+
b.tic();
43+
for ( i = 0; i < b.iterations; i++ ) {
44+
bool = isStructConstructorLike( values[ i%values.length ] );
45+
if ( typeof bool !== 'boolean' ) {
46+
b.fail( 'should return a boolean' );
47+
}
48+
}
49+
b.toc();
50+
if ( !isBoolean( bool ) ) {
51+
b.fail( 'should return a boolean' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
});
56+
57+
bench( pkg+'::false', function benchmark( b ) {
58+
var values;
59+
var bool;
60+
var i;
61+
62+
values = [
63+
[ 1, 2, 3 ],
64+
{},
65+
null,
66+
5,
67+
'beep'
68+
];
69+
70+
b.tic();
71+
for ( i = 0; i < b.iterations; i++ ) {
72+
bool = isStructConstructorLike( values[ i%values.length ] );
73+
if ( typeof bool !== 'boolean' ) {
74+
b.fail( 'should return a boolean' );
75+
}
76+
}
77+
b.toc();
78+
if ( !isBoolean( bool ) ) {
79+
b.fail( 'should return a boolean' );
80+
}
81+
b.pass( 'benchmark finished' );
82+
b.end();
83+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
{{alias}}( value )
3+
Tests if a value is struct constructor-like.
4+
5+
Parameters
6+
----------
7+
value: any
8+
Value to test.
9+
10+
Returns
11+
-------
12+
bool: boolean
13+
Boolean indicating whether a value is struct constructor-like.
14+
15+
Examples
16+
--------
17+
> var schema = [ { 'name': 'foo', 'type': 'float64' } ];
18+
> var S = {{alias:@stdlib/dstructs/struct}}( schema );
19+
> var bool = {{alias}}( S )
20+
true
21+
> bool = {{alias}}( [ 1, 2, 3, 4 ] )
22+
false
23+
> bool = {{alias}}( 3.14 )
24+
false
25+
> bool = {{alias}}( {} )
26+
false
27+
28+
See Also
29+
--------
30+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
/**
22+
* Tests if a value is struct constructor-like.
23+
*
24+
* @param v - value to test
25+
* @returns boolean indicating if a value is struct constructor-like
26+
*
27+
* @example
28+
* var structFactory = require( '@stdlib/dstructs/struct' );
29+
*
30+
* var schema = [
31+
* {
32+
* 'name': 'foo',
33+
* 'type': 'float64'
34+
* }
35+
* ];
36+
* var Struct = structFactory( schema );
37+
*
38+
* var bool = isStructConstructorLike( Struct );
39+
* // returns true
40+
*
41+
* bool = isStructConstructorLike( [] );
42+
* // returns false
43+
*/
44+
declare function isStructConstructorLike( v: any ): boolean;
45+
46+
47+
// EXPORTS //
48+
49+
export = isStructConstructorLike;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 isStructConstructorLike = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a boolean...
25+
{
26+
isStructConstructorLike( {} ); // $ExpectType boolean
27+
isStructConstructorLike( [] ); // $ExpectType boolean
28+
}
29+
30+
// The compiler throws an error if the function is provided an unsupported number of arguments...
31+
{
32+
isStructConstructorLike(); // $ExpectError
33+
isStructConstructorLike( {}, 123 ); // $ExpectError
34+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
var structFactory = require( '@stdlib/dstructs/struct' );
22+
var isStructConstructorLike = require( './../lib' );
23+
24+
var schema = [
25+
{
26+
'name': 'value',
27+
'type': 'float64'
28+
}
29+
];
30+
31+
console.log( isStructConstructorLike( structFactory( schema ) ) );
32+
// => true
33+
34+
console.log( isStructConstructorLike( [ 1, 2, 3, 4 ] ) );
35+
// => false
36+
37+
console.log( isStructConstructorLike( {} ) );
38+
// => false
39+
40+
console.log( isStructConstructorLike( null ) );
41+
// => false

0 commit comments

Comments
 (0)