Skip to content

Commit 3deb612

Browse files
committed
Add utility to resolve the enumeration constant associated with a data type value
1 parent b589c4a commit 3deb612

File tree

9 files changed

+596
-0
lines changed

9 files changed

+596
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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 enumeration constant associated with a supported 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/strided/dtype-resolve-enum' );
41+
```
42+
43+
#### resolve( dtype )
44+
45+
Return the enumeration constant associated with a strided array data type value.
46+
47+
```javascript
48+
var str2enum = require( '@stdlib/strided/dtype-str2enum' );
49+
50+
var v = resolve( 'float64' );
51+
// returns <number>
52+
53+
v = resolve( str2enum( 'float64' ) );
54+
// returns <number>
55+
```
56+
57+
If the function is unable to resolve an enumeration constant, the function returns `undefined`.
58+
59+
```javascript
60+
var v = resolve( 'beep' );
61+
// returns undefined
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+
## Notes
73+
74+
- Downstream consumers of this function should **not** rely on specific integer values (e.g., `INT8 == 0`). Instead, the function should be used in an opaque manner.
75+
76+
</section>
77+
78+
<!-- /.notes -->
79+
80+
<!-- Package usage examples. -->
81+
82+
<section class="examples">
83+
84+
## Examples
85+
86+
<!-- eslint no-undef: "error" -->
87+
88+
```javascript
89+
var resolve = require( '@stdlib/strided/dtype-resolve-enum' );
90+
91+
var v = resolve( 'float64' );
92+
// returns <number>
93+
94+
v = resolve( 'float32' );
95+
// returns <number>
96+
97+
v = resolve( 'int32' );
98+
// returns <number>
99+
100+
v = resolve( 'int16' );
101+
// returns <number>
102+
103+
v = resolve( 'int8' );
104+
// returns <number>
105+
106+
v = resolve( 'uint32' );
107+
// returns <number>
108+
109+
v = resolve( 'uint16' );
110+
// returns <number>
111+
112+
v = resolve( 'uint8' );
113+
// returns <number>
114+
```
115+
116+
</section>
117+
118+
<!-- /.examples -->
119+
120+
<!-- 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. -->
121+
122+
<section class="references">
123+
124+
</section>
125+
126+
<!-- /.references -->
127+
128+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
129+
130+
<section class="related">
131+
132+
</section>
133+
134+
<!-- /.related -->
135+
136+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
137+
138+
<section class="links">
139+
140+
</section>
141+
142+
<!-- /.links -->
Lines changed: 84 additions & 0 deletions
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 isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
25+
var str2enum = require( '@stdlib/strided/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 !== 'number' ) {
48+
b.fail( 'should return a number' );
49+
}
50+
}
51+
b.toc();
52+
if ( !isInteger( out ) ) {
53+
b.fail( 'should return an integer' );
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 !== 'number' ) {
75+
b.fail( 'should return a number' );
76+
}
77+
}
78+
b.toc();
79+
if ( !isInteger( out ) ) {
80+
b.fail( 'should return an integer' );
81+
}
82+
b.pass( 'benchmark finished' );
83+
b.end();
84+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 enumeration constant associated with a strided array data type value.
23+
*
24+
* ## Notes
25+
*
26+
* - Downstream consumers of this function should **not** rely on specific integer values (e.g., `INT8 == 0`). Instead, the function should be used in an opaque manner.
27+
*
28+
* @param dtype - data type value
29+
* @returns enumeration constant
30+
*
31+
* @example
32+
* var v = resolve( 'float64' );
33+
* // returns <number>
34+
*/
35+
declare function resolve( dtype: any ): number | void;
36+
37+
38+
// EXPORTS //
39+
40+
export = resolve;
Lines changed: 28 additions & 0 deletions
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 number or undefined...
25+
{
26+
resolve( 0 ); // $ExpectType number | void
27+
resolve( 'float64' ); // $ExpectType number | void
28+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 resolve = require( './../lib' );
22+
23+
var v = resolve( 'float64' );
24+
console.log( v );
25+
// => <number>
26+
27+
v = resolve( 'float32' );
28+
console.log( v );
29+
// => <number>
30+
31+
v = resolve( 'int32' );
32+
console.log( v );
33+
// => <number>
34+
35+
v = resolve( 'int16' );
36+
console.log( v );
37+
// => <number>
38+
39+
v = resolve( 'int8' );
40+
console.log( v );
41+
// => <number>
42+
43+
v = resolve( 'uint32' );
44+
console.log( v );
45+
// => <number>
46+
47+
v = resolve( 'uint16' );
48+
console.log( v );
49+
// => <number>
50+
51+
v = resolve( 'uint8' );
52+
console.log( v );
53+
// => <number>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 enumeration constant associated with a supported data type value.
23+
*
24+
* @module @stdlib/strided/dtype-resolve-enum
25+
*
26+
* @example
27+
* var resolve = require( '@stdlib/strided/dtype-resolve-enum' );
28+
*
29+
* var v = resolve( 'float64' );
30+
* // returns <number>
31+
*/
32+
33+
// MODULES //
34+
35+
var main = require( './main.js' );
36+
37+
38+
// EXPORTS //
39+
40+
module.exports = main;

0 commit comments

Comments
 (0)