Skip to content

Commit 907d7f2

Browse files
committed
Add utility to return the data type string associated with a single letter character abbreviation
1 parent c9d678a commit 907d7f2

File tree

10 files changed

+658
-0
lines changed

10 files changed

+658
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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+
# char2dtype
22+
23+
> Return the [data type][@stdlib/ndarray/dtypes] string associated with a provided [single letter character abbreviation][@stdlib/ndarray/base/dtype-char].
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 char2dtype = require( '@stdlib/ndarray/base/char2dtype' );
41+
```
42+
43+
#### char2dtype( \[dtype] )
44+
45+
Returns the [data type][@stdlib/ndarray/dtypes] string associated with a provided [single letter character abbreviation][@stdlib/ndarray/base/dtype-char].
46+
47+
```javascript
48+
var out = char2dtype( 'd' );
49+
// returns 'float64'
50+
51+
out = char2dtype( 'i' );
52+
// returns 'int32'
53+
```
54+
55+
If provided an unknown or unsupported [single letter character abbreviation][@stdlib/ndarray/base/dtype-char], the function returns `null`.
56+
57+
```javascript
58+
var out = char2dtype( '(' );
59+
// returns null
60+
```
61+
62+
If not provided a [data type][@stdlib/ndarray/dtypes] string, the function returns an object mapping [single letter character abbreviations][@stdlib/ndarray/base/dtype-char] to [data type][@stdlib/ndarray/dtypes] strings.
63+
64+
```javascript
65+
var out = char2dtype();
66+
// returns {...}
67+
```
68+
69+
</section>
70+
71+
<!-- /.usage -->
72+
73+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
74+
75+
<section class="notes">
76+
77+
</section>
78+
79+
<!-- /.notes -->
80+
81+
<!-- Package usage examples. -->
82+
83+
<section class="examples">
84+
85+
## Examples
86+
87+
<!-- eslint no-undef: "error" -->
88+
89+
```javascript
90+
var dtypeChar = require( '@stdlib/ndarray/base/dtype-char' );
91+
var char2dtype = require( '@stdlib/ndarray/base/char2dtype' );
92+
93+
var chars;
94+
var out;
95+
var i;
96+
97+
chars = [
98+
dtypeChar( 'float64' ),
99+
dtypeChar( 'float32' ),
100+
dtypeChar( 'int8' ),
101+
dtypeChar( 'uint8' ),
102+
dtypeChar( 'uint8c' ),
103+
dtypeChar( 'int16' ),
104+
dtypeChar( 'uint16' ),
105+
dtypeChar( 'int32' ),
106+
dtypeChar( 'uint32' ),
107+
dtypeChar( 'binary' ),
108+
dtypeChar( 'generic' ),
109+
'('
110+
];
111+
112+
for ( i = 0; i < chars.length; i++ ) {
113+
out = char2dtype( chars[ i ] );
114+
console.log( '%s => %s', chars[ i ], out );
115+
}
116+
```
117+
118+
</section>
119+
120+
<!-- /.examples -->
121+
122+
<!-- 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. -->
123+
124+
<section class="references">
125+
126+
</section>
127+
128+
<!-- /.references -->
129+
130+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
131+
132+
<section class="related">
133+
134+
</section>
135+
136+
<!-- /.related -->
137+
138+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
139+
140+
<section class="links">
141+
142+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
143+
144+
[@stdlib/ndarray/base/dtype-char]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/dtype-char
145+
146+
</section>
147+
148+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 dtypeChar = require( '@stdlib/ndarray/base/dtype-char' );
25+
var pkg = require( './../package.json' ).name;
26+
var char2dtype = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var chars;
33+
var out;
34+
var ch;
35+
var i;
36+
37+
chars = [
38+
dtypeChar( 'float64' ),
39+
dtypeChar( 'float32' ),
40+
dtypeChar( 'int8' ),
41+
dtypeChar( 'uint8' ),
42+
dtypeChar( 'int16' ),
43+
dtypeChar( 'uint16' ),
44+
dtypeChar( 'int32' ),
45+
dtypeChar( 'uint32' ),
46+
47+
dtypeChar( 'binary' ),
48+
dtypeChar( 'generic' ),
49+
dtypeChar( 'uint8c' )
50+
];
51+
52+
b.tic();
53+
for ( i = 0; i < b.iterations; i++ ) {
54+
ch = chars[ i%chars.length ];
55+
out = char2dtype( ch );
56+
if ( typeof out !== 'string' && out !== null ) {
57+
b.fail( 'should return a string or null' );
58+
}
59+
}
60+
b.toc();
61+
if ( typeof out !== 'string' && out !== null ) {
62+
b.fail( 'should return a string or null' );
63+
}
64+
b.pass( 'benchmark finished' );
65+
b.end();
66+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
{{alias}}( [ch] )
3+
Returns the data type string associated with a provided single letter
4+
character abbreviation.
5+
6+
If not provided an abbreviation, the function returns an object mapping
7+
single letter character abbreviations to data type strings.
8+
9+
If provided an unknown/unsupported abbreviation, the function returns
10+
`null`.
11+
12+
Parameters
13+
----------
14+
ch: string (optional)
15+
Single letter character abbreviation.
16+
17+
Returns
18+
-------
19+
out: Object|string|null
20+
Data type string.
21+
22+
Examples
23+
--------
24+
> var out = {{alias}}( 'd' )
25+
'float64'
26+
> out = {{alias}}( 'foobar' )
27+
null
28+
> var obj = {{alias}}()
29+
{...}
30+
31+
See Also
32+
--------
33+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
* Interface describing an object mapping single letter character abbreviations to data type strings.
23+
*/
24+
interface Table {
25+
// Table properties:
26+
[key: string]: string;
27+
}
28+
29+
/**
30+
* Returns the data type string associated with a provided single letter character abbreviation.
31+
*
32+
* @param ch - single letter character abbreviation
33+
* @returns data type string (or null)
34+
*
35+
* @example
36+
* var out = char2dtype( 'd' );
37+
* // returns 'float64'
38+
*
39+
* out = char2dtype( '{' );
40+
* // returns null
41+
*/
42+
declare function char2dtype( ch: string ): string | null;
43+
44+
/**
45+
* Returns an object mapping single letter character abbreviations to data type strings.
46+
*
47+
* @returns object mapping single letter character abbreviations to data type strings
48+
*
49+
* @example
50+
* var out = char2dtype();
51+
* // returns {...}
52+
*/
53+
declare function char2dtype(): Table;
54+
55+
56+
// EXPORTS //
57+
58+
export = char2dtype;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 char2dtype = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a table if not provided an argument...
25+
{
26+
char2dtype(); // $ExpectType Table
27+
}
28+
29+
// The function returns a string or null if provided an argument...
30+
{
31+
char2dtype( 'd' ); // $ExpectType string | null
32+
char2dtype( 'f' ); // $ExpectType string | null
33+
}
34+
35+
// The function does not compile if provided a value other than a string...
36+
{
37+
char2dtype( true ); // $ExpectError
38+
char2dtype( false ); // $ExpectError
39+
char2dtype( null ); // $ExpectError
40+
char2dtype( undefined ); // $ExpectError
41+
char2dtype( 5 ); // $ExpectError
42+
char2dtype( [] ); // $ExpectError
43+
char2dtype( {} ); // $ExpectError
44+
char2dtype( ( x: number ): number => x ); // $ExpectError
45+
}

0 commit comments

Comments
 (0)