Skip to content

Commit d20a2ea

Browse files
committed
feat: add ndarray/base/assert/is-struct-data-type
--- 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 009da93 commit d20a2ea

File tree

10 files changed

+887
-0
lines changed

10 files changed

+887
-0
lines changed
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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+
# isStructDataType
22+
23+
> Test if an input value is a supported ndarray struct data type.
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 isStructDataType = require( '@stdlib/ndarray/base/assert/is-struct-data-type' );
41+
```
42+
43+
#### isStructDataType( value )
44+
45+
Tests if an input value is a supported ndarray struct data type.
46+
47+
```javascript
48+
var structFactory = require( '@stdlib/dstructs/struct' );
49+
50+
var Struct = structFactory([
51+
{
52+
'name': 'foo',
53+
'type': 'float64'
54+
}
55+
]);
56+
57+
var bool = isStructDataType( Struct );
58+
// returns true
59+
60+
bool = isStructDataType( 'int32' );
61+
// returns false
62+
```
63+
64+
</section>
65+
66+
<!-- /.usage -->
67+
68+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
69+
70+
<section class="notes">
71+
72+
</section>
73+
74+
<!-- /.notes -->
75+
76+
<!-- Package usage examples. -->
77+
78+
<section class="examples">
79+
80+
## Examples
81+
82+
<!-- eslint no-undef: "error" -->
83+
84+
```javascript
85+
var structFactory = require( '@stdlib/dstructs/struct' );
86+
var isStructDataType = require( '@stdlib/ndarray/base/assert/is-struct-data-type' );
87+
88+
var Struct = structFactory([
89+
{
90+
'name': 'foo',
91+
'type': 'float64'
92+
}
93+
]);
94+
95+
var bool = isStructDataType( Struct );
96+
// returns true
97+
98+
bool = isStructDataType( 'binary' );
99+
// returns false
100+
101+
bool = isStructDataType( 'float32' );
102+
// returns false
103+
104+
bool = isStructDataType( 'float64' );
105+
// returns false
106+
107+
bool = isStructDataType( 'generic' );
108+
// returns false
109+
110+
bool = isStructDataType( 'int16' );
111+
// returns false
112+
113+
bool = isStructDataType( 'int32' );
114+
// returns false
115+
116+
bool = isStructDataType( 'int8' );
117+
// returns false
118+
119+
bool = isStructDataType( 'uint16' );
120+
// returns false
121+
122+
bool = isStructDataType( 'uint32' );
123+
// returns false
124+
125+
bool = isStructDataType( 'uint8' );
126+
// returns false
127+
128+
bool = isStructDataType( 'uint8c' );
129+
// returns false
130+
131+
bool = isStructDataType( '' );
132+
// returns false
133+
134+
bool = isStructDataType( 'foo' );
135+
// returns false
136+
```
137+
138+
</section>
139+
140+
<!-- /.examples -->
141+
142+
<!-- 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. -->
143+
144+
<section class="references">
145+
146+
</section>
147+
148+
<!-- /.references -->
149+
150+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
151+
152+
<section class="related">
153+
154+
</section>
155+
156+
<!-- /.related -->
157+
158+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
159+
160+
<section class="links">
161+
162+
</section>
163+
164+
<!-- /.links -->
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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 structFactory = require( '@stdlib/dstructs/struct' );
26+
var pkg = require( './../package.json' ).name;
27+
var isStructDataType = require( './../lib' );
28+
29+
30+
// VARIABLES //
31+
32+
var Struct = structFactory([
33+
{
34+
'name': 'foo',
35+
'type': 'float64'
36+
}
37+
]);
38+
39+
40+
// MAIN //
41+
42+
bench( pkg+'::true', function benchmark( b ) {
43+
var values;
44+
var out;
45+
var v;
46+
var i;
47+
48+
values = [
49+
Struct,
50+
Struct
51+
];
52+
53+
b.tic();
54+
for ( i = 0; i < b.iterations; i++ ) {
55+
v = values[ i%values.length ];
56+
out = isStructDataType( v );
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+
bench( pkg+'::false', function benchmark( b ) {
70+
var values;
71+
var out;
72+
var v;
73+
var i;
74+
75+
values = [
76+
'binary',
77+
'float32',
78+
'float64',
79+
'generic',
80+
'int16',
81+
'int32',
82+
'int8',
83+
'uint16',
84+
'uint32',
85+
'uint8',
86+
'uint8c',
87+
'foo',
88+
'bar',
89+
'',
90+
'beep',
91+
'boop'
92+
];
93+
94+
b.tic();
95+
for ( i = 0; i < b.iterations; i++ ) {
96+
v = values[ i%values.length ];
97+
out = isStructDataType( v );
98+
if ( typeof out !== 'boolean' ) {
99+
b.fail( 'should return a boolean' );
100+
}
101+
}
102+
b.toc();
103+
if ( !isBoolean( out ) ) {
104+
b.fail( 'should return a boolean' );
105+
}
106+
b.pass( 'benchmark finished' );
107+
b.end();
108+
});
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
{{alias}}( value )
3+
Tests if an input value is a supported ndarray struct data type.
4+
5+
Parameters
6+
----------
7+
value: any
8+
Value to test.
9+
10+
Returns
11+
-------
12+
bool: boolean
13+
Boolean indicating if an input value is a supported ndarray struct data
14+
type.
15+
16+
Examples
17+
--------
18+
> var schema = [ { 'name': 'foo', 'type': 'float64' } ];
19+
> var S = {{alias:@stdlib/dstructs/struct}}( schema );
20+
> var bool = {{alias}}( S )
21+
true
22+
> bool = {{alias}}( 'binary' )
23+
false
24+
> bool = {{alias}}( 'float32' )
25+
false
26+
> bool = {{alias}}( 'float64' )
27+
false
28+
> bool = {{alias}}( 'int16' )
29+
false
30+
> bool = {{alias}}( 'int32' )
31+
false
32+
> bool = {{alias}}( 'int8' )
33+
false
34+
> bool = {{alias}}( 'uint16' )
35+
false
36+
> bool = {{alias}}( 'uint32' )
37+
false
38+
> bool = {{alias}}( 'uint8' )
39+
false
40+
> bool = {{alias}}( 'uint8c' )
41+
false
42+
> bool = {{alias}}( '' )
43+
false
44+
> bool = {{alias}}( 'beep' )
45+
false
46+
47+
See Also
48+
--------
49+

0 commit comments

Comments
 (0)