Skip to content

Commit aa727fa

Browse files
committed
feat: add ndarray/base/assert/is-equal-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 a278663 commit aa727fa

File tree

10 files changed

+864
-0
lines changed

10 files changed

+864
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
# isEqualDataType
22+
23+
> Test whether two values are equal ndarray [data types][@stdlib/ndarray/dtypes].
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 isEqualDataType = require( '@stdlib/ndarray/base/assert/is-equal-data-type' );
41+
```
42+
43+
#### isEqualDataType( v1, v2 )
44+
45+
Tests whether two values are equal ndarray [data types][@stdlib/ndarray/dtypes].
46+
47+
```javascript
48+
var bool = isEqualDataType( 'float32', 'float32' );
49+
// returns true
50+
51+
bool = isEqualDataType( 'int32', 'int16' );
52+
// returns false
53+
```
54+
55+
</section>
56+
57+
<!-- /.usage -->
58+
59+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
60+
61+
<section class="notes">
62+
63+
</section>
64+
65+
<!-- /.notes -->
66+
67+
<!-- Package usage examples. -->
68+
69+
<section class="examples">
70+
71+
## Examples
72+
73+
<!-- eslint no-undef: "error" -->
74+
75+
```javascript
76+
var DataType = require( '@stdlib/ndarray/dtype-ctor' );
77+
var isEqualDataType = require( '@stdlib/ndarray/base/assert/is-equal-data-type' );
78+
79+
var bool = isEqualDataType( 'binary', 'binary' );
80+
// returns true
81+
82+
bool = isEqualDataType( 'float32', 'float32' );
83+
// returns true
84+
85+
bool = isEqualDataType( 'float64', new DataType( 'float64' ) );
86+
// returns true
87+
88+
bool = isEqualDataType( 'generic', new DataType( 'generic' ) );
89+
// returns true
90+
91+
bool = isEqualDataType( 'int16', 'int32' );
92+
// returns false
93+
94+
bool = isEqualDataType( 'int32', new DataType( 'int16') );
95+
// returns false
96+
97+
bool = isEqualDataType( 'foo', 'foo' );
98+
// returns false
99+
100+
bool = isEqualDataType( {}, {} );
101+
// returns false
102+
```
103+
104+
</section>
105+
106+
<!-- /.examples -->
107+
108+
<!-- 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. -->
109+
110+
<section class="references">
111+
112+
</section>
113+
114+
<!-- /.references -->
115+
116+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
117+
118+
<section class="related">
119+
120+
</section>
121+
122+
<!-- /.related -->
123+
124+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
125+
126+
<section class="links">
127+
128+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
129+
130+
</section>
131+
132+
<!-- /.links -->
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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 DataType = require( '@stdlib/ndarray/dtype-ctor' );
26+
var pkg = require( './../package.json' ).name;
27+
var isEqualDataType = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+'::strings', function benchmark( b ) {
33+
var values;
34+
var out;
35+
var v;
36+
var i;
37+
38+
values = [
39+
'binary',
40+
'float32',
41+
'float64',
42+
'generic',
43+
'int16',
44+
'int32',
45+
'int8',
46+
'uint16',
47+
'uint32',
48+
'uint8',
49+
'uint8c',
50+
'foo',
51+
'bar',
52+
'',
53+
'beep',
54+
'boop'
55+
];
56+
57+
b.tic();
58+
for ( i = 0; i < b.iterations; i++ ) {
59+
v = values[ i%values.length ];
60+
out = isEqualDataType( v, v );
61+
if ( typeof out !== 'boolean' ) {
62+
b.fail( 'should return a boolean' );
63+
}
64+
}
65+
b.toc();
66+
if ( !isBoolean( out ) ) {
67+
b.fail( 'should return a boolean' );
68+
}
69+
b.pass( 'benchmark finished' );
70+
b.end();
71+
});
72+
73+
bench( pkg+'::data_type_objects', function benchmark( b ) {
74+
var values;
75+
var out;
76+
var v;
77+
var i;
78+
79+
values = [
80+
new DataType( 'binary' ),
81+
new DataType( 'float32' ),
82+
new DataType( 'float64' ),
83+
new DataType( 'generic' ),
84+
new DataType( 'int16' ),
85+
new DataType( 'int32' ),
86+
new DataType( 'int8' ),
87+
new DataType( 'uint16' ),
88+
new DataType( 'uint32' ),
89+
new DataType( 'uint8' ),
90+
new DataType( 'uint8c' )
91+
];
92+
93+
b.tic();
94+
for ( i = 0; i < b.iterations; i++ ) {
95+
v = values[ i%values.length ];
96+
out = isEqualDataType( v, v );
97+
if ( typeof out !== 'boolean' ) {
98+
b.fail( 'should return a boolean' );
99+
}
100+
}
101+
b.toc();
102+
if ( !isBoolean( out ) ) {
103+
b.fail( 'should return a boolean' );
104+
}
105+
b.pass( 'benchmark finished' );
106+
b.end();
107+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
{{alias}}( v1, v2 )
3+
Tests whether two values are equal ndarray data types.
4+
5+
Parameters
6+
----------
7+
v1: any
8+
First input value.
9+
10+
v2: any
11+
Second input value.
12+
13+
Returns
14+
-------
15+
bool: boolean
16+
Boolean indicating whether two values are equal ndarray data types.
17+
18+
Examples
19+
--------
20+
> var bool = {{alias}}( 'float64', 'float64' )
21+
true
22+
> var dt = new {{alias:@stdlib/ndarray/dtype-ctor}}( 'float64' );
23+
> bool = {{alias}}( 'float64', dt )
24+
true
25+
> bool = {{alias}}( 'int16', 'int32' )
26+
false
27+
28+
See Also
29+
--------
30+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 whether two values are equal ndarray data types.
23+
*
24+
* @param v1 - first input value
25+
* @param v2 - second input value
26+
* @returns boolean indicating whether two values are equal ndarray data types
27+
*
28+
* @example
29+
* var DataType = require( '@stdlib/ndarray/dtype-ctor' );
30+
*
31+
* var bool = isEqualDataType( 'binary', 'binary' );
32+
* // returns true
33+
*
34+
* bool = isEqualDataType( 'float32', 'float32' );
35+
* // returns true
36+
*
37+
* bool = isEqualDataType( 'float64', new DataType( 'float64' ) );
38+
* // returns true
39+
*
40+
* bool = isEqualDataType( 'generic', new DataType( 'generic' ) );
41+
* // returns true
42+
*
43+
* bool = isEqualDataType( 'int16', 'int32' );
44+
* // returns false
45+
*
46+
* bool = isEqualDataType( 'int32', new DataType( 'int16' ) );
47+
* // returns false
48+
*/
49+
declare function isEqualDataType( v1: any, v2: any ): boolean;
50+
51+
52+
// EXPORTS //
53+
54+
export = isEqualDataType;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 isEqualDataType = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a boolean...
25+
{
26+
isEqualDataType( 'binary', 'binary' ); // $ExpectType boolean
27+
isEqualDataType( 'foo', 'bar' ); // $ExpectType boolean
28+
}
29+
30+
// The compiler throws an error if the function is provided an unsupported number of arguments...
31+
{
32+
isEqualDataType(); // $ExpectError
33+
isEqualDataType( 'foo' ); // $ExpectError
34+
isEqualDataType( 'foo', 'bar', 'beep' ); // $ExpectError
35+
}

0 commit comments

Comments
 (0)