Skip to content

Commit a1169f1

Browse files
committed
feat: add ndarray/base/dtypes2strings
--- 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 d66fe7c commit a1169f1

File tree

10 files changed

+799
-0
lines changed

10 files changed

+799
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+
# dtypes2strings
22+
23+
> Resolve a list of [data type][@stdlib/ndarray/dtypes] strings.
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 dtypes2strings = require( '@stdlib/ndarray/base/dtypes2strings' );
41+
```
42+
43+
#### dtypes2strings( dtypes )
44+
45+
Resolves a list of [data type][@stdlib/ndarray/dtypes] strings.
46+
47+
```javascript
48+
var DataType = require( '@stdlib/ndarray/dtype-ctor' );
49+
50+
var out = dtypes2strings( [ new DataType( 'float32' ), new DataType( 'float64' ) ] );
51+
// returns [...]
52+
```
53+
54+
The function accepts the following arguments:
55+
56+
- **dtypes**: an array of [data types][@stdlib/ndarray/dtypes].
57+
58+
If the function is unable to resolve a data type string for a provided [data type][@stdlib/ndarray/dtypes], the corresponding element in the returned array will be `null`.
59+
60+
```javascript
61+
var out = dtypes2strings( [ 'foo', 'bar' ] );
62+
// returns [ null, null ]
63+
```
64+
65+
</section>
66+
67+
<!-- /.usage -->
68+
69+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
70+
71+
<section class="notes">
72+
73+
</section>
74+
75+
<!-- /.notes -->
76+
77+
<!-- Package usage examples. -->
78+
79+
<section class="examples">
80+
81+
## Examples
82+
83+
<!-- eslint no-undef: "error" -->
84+
85+
```javascript
86+
var dtypes = require( '@stdlib/ndarray/dtypes' );
87+
var logEach = require( '@stdlib/console/log-each' );
88+
var dtypes2strings = require( '@stdlib/ndarray/base/dtypes2strings' );
89+
90+
// Get the list of supported data types:
91+
var dt = dtypes();
92+
93+
// Resolve strings:
94+
var strs = dtypes2strings( dt );
95+
96+
// Print the results:
97+
logEach( '%s => %s', dt, strs );
98+
```
99+
100+
</section>
101+
102+
<!-- /.examples -->
103+
104+
<!-- 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. -->
105+
106+
<section class="references">
107+
108+
</section>
109+
110+
<!-- /.references -->
111+
112+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
113+
114+
<section class="related">
115+
116+
</section>
117+
118+
<!-- /.related -->
119+
120+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
121+
122+
<section class="links">
123+
124+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
125+
126+
</section>
127+
128+
<!-- /.links -->
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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 isStringArray = require( '@stdlib/assert/is-string-array' ).primitives;
25+
var nCartesianProduct = require( '@stdlib/array/base/n-cartesian-product' );
26+
var dtypes = require( '@stdlib/ndarray/dtypes' );
27+
var format = require( '@stdlib/string/format' );
28+
var pkg = require( './../package.json' ).name;
29+
var dtypes2strings = require( './../lib' );
30+
31+
32+
// VARIABLES //
33+
34+
var DTYPES = dtypes();
35+
36+
37+
// MAIN //
38+
39+
bench( format( '%s::one_dtype', pkg ), function benchmark( b ) {
40+
var v;
41+
var i;
42+
43+
b.tic();
44+
for ( i = 0; i < b.iterations; i++ ) {
45+
v = dtypes2strings( [ DTYPES[ i%DTYPES.length ] ] );
46+
if ( typeof v !== 'object' ) {
47+
b.fail( 'should return an array' );
48+
}
49+
}
50+
b.toc();
51+
if ( !isStringArray( v ) ) {
52+
b.fail( 'should return an array' );
53+
}
54+
b.pass( 'benchmark finished' );
55+
b.end();
56+
});
57+
58+
bench( format( '%s::two_dtypes', pkg ), function benchmark( b ) {
59+
var values;
60+
var v;
61+
var i;
62+
63+
values = nCartesianProduct( DTYPES, DTYPES );
64+
65+
b.tic();
66+
for ( i = 0; i < b.iterations; i++ ) {
67+
v = dtypes2strings( values[ i%values.length ] );
68+
if ( typeof v !== 'object' ) {
69+
b.fail( 'should return an array' );
70+
}
71+
}
72+
b.toc();
73+
if ( !isStringArray( v ) ) {
74+
b.fail( 'should return an array' );
75+
}
76+
b.pass( 'benchmark finished' );
77+
b.end();
78+
});
79+
80+
bench( format( '%s::three_dtypes', pkg ), function benchmark( b ) {
81+
var values;
82+
var v;
83+
var i;
84+
85+
values = nCartesianProduct( DTYPES, DTYPES, DTYPES );
86+
87+
b.tic();
88+
for ( i = 0; i < b.iterations; i++ ) {
89+
v = dtypes2strings( values[ i%values.length ] );
90+
if ( typeof v !== 'object' ) {
91+
b.fail( 'should return an array' );
92+
}
93+
}
94+
b.toc();
95+
if ( !isStringArray( v ) ) {
96+
b.fail( 'should return an array' );
97+
}
98+
b.pass( 'benchmark finished' );
99+
b.end();
100+
});
101+
102+
bench( format( '%s::four_dtypes', pkg ), function benchmark( b ) {
103+
var values;
104+
var v;
105+
var i;
106+
107+
values = nCartesianProduct( DTYPES, DTYPES, DTYPES, DTYPES );
108+
109+
b.tic();
110+
for ( i = 0; i < b.iterations; i++ ) {
111+
v = dtypes2strings( values[ i%values.length ] );
112+
if ( typeof v !== 'object' ) {
113+
b.fail( 'should return an array' );
114+
}
115+
}
116+
b.toc();
117+
if ( !isStringArray( v ) ) {
118+
b.fail( 'should return an array' );
119+
}
120+
b.pass( 'benchmark finished' );
121+
b.end();
122+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
{{alias}}( dtypes )
3+
Resolves a list of data type strings.
4+
5+
If the function is unable to resolve a data type string for a provided data
6+
type, the corresponding element in the returned array will be `null`.
7+
8+
Parameters
9+
----------
10+
dtypes: Array<string|DataType>
11+
List of data types.
12+
13+
Returns
14+
-------
15+
out: Array<string|null>
16+
Results.
17+
18+
Examples
19+
--------
20+
> var out = {{alias}}( [ 'float32', 'float64' ] )
21+
[...]
22+
> out = {{alias}}( [ 'foo', 'bar' ] )
23+
[ null, null ]
24+
25+
See Also
26+
--------
27+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 { DataType } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Resolves a list of data type strings.
27+
*
28+
* ## Notes
29+
*
30+
* - If the function is unable to resolve a data type string for a provided data type, the corresponding element in the returned array will be `null`.
31+
*
32+
* @param dtypes - list of data types
33+
* @returns results
34+
*
35+
* @example
36+
* var out = dtypes2strings( [ 'float32', 'float64' ] );
37+
* // returns [...]
38+
*
39+
* @example
40+
* var out = dtypes2strings( [ 'foo', 'bar' ] );
41+
* // returns [ null, null ]
42+
*/
43+
declare function dtypes2strings( dtypes: ArrayLike<DataType> ): Array<string|null>;
44+
45+
46+
// EXPORTS //
47+
48+
export = dtypes2strings;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 dtypes2strings = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an array...
25+
{
26+
dtypes2strings( [ 'float64', 'float64' ] ); // $ExpectType (string | null)[]
27+
}
28+
29+
// The compiler throws an error if the function is not provided an array-like object containing data types...
30+
{
31+
dtypes2strings( '5' ); // $ExpectError
32+
dtypes2strings( 5 ); // $ExpectError
33+
dtypes2strings( true ); // $ExpectError
34+
dtypes2strings( false ); // $ExpectError
35+
dtypes2strings( null ); // $ExpectError
36+
dtypes2strings( {} ); // $ExpectError
37+
dtypes2strings( [ '5' ] ); // $ExpectError
38+
dtypes2strings( ( x: number ): number => x ); // $ExpectError
39+
}
40+
41+
// The compiler throws an error if the function is provided an unsupported number of arguments...
42+
{
43+
dtypes2strings(); // $ExpectError
44+
dtypes2strings( [ 'float64' ], {} ); // $ExpectError
45+
}

0 commit comments

Comments
 (0)