Skip to content

Commit b79ea89

Browse files
committed
Add package to resolve a data type string
1 parent da8360c commit b79ea89

File tree

9 files changed

+592
-0
lines changed

9 files changed

+592
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2021 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+
# resolve
22+
23+
> Return the data type string associated with a supported ndarray data type value.
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 resolve = require( '@stdlib/ndarray/base/dtype-resolve-str' );
41+
```
42+
43+
#### resolve( dtype )
44+
45+
Returns the data type string associated with an ndarray data type value.
46+
47+
```javascript
48+
var str2enum = require( '@stdlib/ndarray/base/dtype-str2enum' );
49+
50+
var v = resolve( 'float64' );
51+
// returns 'float64'
52+
53+
v = resolve( str2enum( 'float64' ) );
54+
// returns 'float64'
55+
```
56+
57+
If unable to resolve a data type string, the function returns `null`.
58+
59+
```javascript
60+
var v = resolve( 'beep' );
61+
// returns null
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 str2enum = require( '@stdlib/ndarray/base/dtype-str2enum' );
86+
var resolve = require( '@stdlib/ndarray/base/dtype-resolve-str' );
87+
88+
var v = resolve( str2enum( 'float64' ) );
89+
// returns 'float64'
90+
91+
v = resolve( str2enum( 'float32' ) );
92+
// returns 'float32'
93+
94+
v = resolve( str2enum( 'int32' ) );
95+
// returns 'int32'
96+
97+
v = resolve( str2enum( 'int16' ) );
98+
// returns 'int16'
99+
100+
v = resolve( str2enum( 'int8' ) );
101+
// returns 'int8'
102+
103+
v = resolve( str2enum( 'uint32' ) );
104+
// returns 'uint32'
105+
106+
v = resolve( str2enum( 'uint16' ) );
107+
// returns 'uint16'
108+
109+
v = resolve( str2enum( 'uint8' ) );
110+
// returns 'uint8'
111+
```
112+
113+
</section>
114+
115+
<!-- /.examples -->
116+
117+
<!-- 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. -->
118+
119+
<section class="references">
120+
121+
</section>
122+
123+
<!-- /.references -->
124+
125+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
126+
127+
<section class="related">
128+
129+
</section>
130+
131+
<!-- /.related -->
132+
133+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
134+
135+
<section class="links">
136+
137+
</section>
138+
139+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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 isString = require( '@stdlib/assert/is-string' ).isPrimitive;
25+
var str2enum = require( '@stdlib/ndarray/base/dtype-str2enum' );
26+
var pkg = require( './../package.json' ).name;
27+
var resolve = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+'::string', function benchmark( b ) {
33+
var values;
34+
var out;
35+
var i;
36+
37+
values = [
38+
'float64',
39+
'float32',
40+
'int32',
41+
'int8'
42+
];
43+
44+
b.tic();
45+
for ( i = 0; i < b.iterations; i++ ) {
46+
out = resolve( values[ i%values.length ] );
47+
if ( typeof out !== 'string' ) {
48+
b.fail( 'should return a string' );
49+
}
50+
}
51+
b.toc();
52+
if ( !isString( out ) ) {
53+
b.fail( 'should return a string' );
54+
}
55+
b.pass( 'benchmark finished' );
56+
b.end();
57+
});
58+
59+
bench( pkg+'::integer', function benchmark( b ) {
60+
var values;
61+
var out;
62+
var i;
63+
64+
values = [
65+
str2enum( 'float64' ),
66+
str2enum( 'float32' ),
67+
str2enum( 'int32' ),
68+
str2enum( 'int8' )
69+
];
70+
71+
b.tic();
72+
for ( i = 0; i < b.iterations; i++ ) {
73+
out = resolve( values[ i%values.length ] );
74+
if ( typeof out !== 'string' ) {
75+
b.fail( 'should return a string' );
76+
}
77+
}
78+
b.toc();
79+
if ( !isString( out ) ) {
80+
b.fail( 'should return a string' );
81+
}
82+
b.pass( 'benchmark finished' );
83+
b.end();
84+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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: 2.0
20+
21+
/**
22+
* Returns the data type string associated with an ndarray data type value.
23+
*
24+
* @param dtype - data type value
25+
* @returns data type string
26+
*
27+
* @example
28+
* var str2enum = require( `@stdlib/ndarray/base/dtype-str2enum` );
29+
*
30+
* var v = resolve( str2enum( 'float64' ) );
31+
* // returns 'float64'
32+
*/
33+
declare function resolve( dtype: any ): string | null;
34+
35+
36+
// EXPORTS //
37+
38+
export = resolve;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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 resolve = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a string or null...
25+
{
26+
resolve( 0 ); // $ExpectType string | null
27+
resolve( 'float64' ); // $ExpectType string | null
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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 str2enum = require( '@stdlib/ndarray/base/dtype-str2enum' );
22+
var resolve = require( './../lib' );
23+
24+
var v = resolve( str2enum( 'float64' ) );
25+
console.log( v );
26+
// => 'float64'
27+
28+
v = resolve( str2enum( 'float32' ) );
29+
console.log( v );
30+
// => 'float32'
31+
32+
v = resolve( str2enum( 'int32' ) );
33+
console.log( v );
34+
// => 'int32'
35+
36+
v = resolve( str2enum( 'int16' ) );
37+
console.log( v );
38+
// => 'int16'
39+
40+
v = resolve( str2enum( 'int8' ) );
41+
console.log( v );
42+
// => 'int8'
43+
44+
v = resolve( str2enum( 'uint32' ) );
45+
console.log( v );
46+
// => 'uint32'
47+
48+
v = resolve( str2enum( 'uint16' ) );
49+
console.log( v );
50+
// => 'uint16'
51+
52+
v = resolve( str2enum( 'uint8' ) );
53+
console.log( v );
54+
// => 'uint8'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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+
/**
22+
* Return the data type string associated with a supported ndarray data type value.
23+
*
24+
* @module @stdlib/ndarray/base/dtype-resolve-str
25+
*
26+
* @example
27+
* var str2enum = require( '@stdlib/ndarray/base/dtype-str2enum' );
28+
* var resolve = require( '@stdlib/ndarray/base/dtype-resolve-str' );
29+
*
30+
* var v = resolve( str2enum( 'float64' ) );
31+
* // returns 'float64'
32+
*/
33+
34+
// MODULES //
35+
36+
var main = require( './main.js' );
37+
38+
39+
// EXPORTS //
40+
41+
module.exports = main;

0 commit comments

Comments
 (0)