Skip to content

Commit d5cb7ec

Browse files
committed
feat: add ndarray/base/dtype-chars
--- 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 605a0cc commit d5cb7ec

File tree

11 files changed

+1376
-0
lines changed

11 files changed

+1376
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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+
# dtypeChars
22+
23+
> List of ndarray data type single letter character abbreviations.
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 dtypeChars = require( '@stdlib/ndarray/base/dtype-chars' );
41+
```
42+
43+
#### dtypeChars( \[kind] )
44+
45+
Returns a list of ndarray data type single letter character abbreviations.
46+
47+
```javascript
48+
var out = dtypeChars();
49+
// e.g., returns [ 'r', 'j', 'c', 'z', ... ]
50+
```
51+
52+
When not provided a data type "kind", the function returns an array containing the single letter character abbreviations for all support ndarray [data types][@stdlib/ndarray/dtypes]. To return the subset of data type single letter character abbreviations belonging to a specified data type kind, provide a `kind` argument.
53+
54+
```javascript
55+
var out = dtypeChars( 'floating_point' );
56+
// returns [...]
57+
```
58+
59+
The function supports the following data type kinds:
60+
61+
- `floating_point`: floating-point data types.
62+
- `real_floating_point`: real-valued floating-point data types.
63+
- `complex_floating_point`: complex-valued floating-point data types.
64+
- `boolean`: boolean data types.
65+
- `integer`: integer data types.
66+
- `signed_integer`: signed integer data types.
67+
- `unsigned_integer`: unsigned integer data types.
68+
- `real`: real-valued data types.
69+
- `numeric`: numeric data types.
70+
- `typed`: typed data types.
71+
- `integer_index`: integer index data types.
72+
- `boolean_index`: boolean index data types.
73+
- `mask_index`: mask index data types.
74+
- `typed_index`: typed index data types.
75+
- `index`: index data types.
76+
- `all`: all data types.
77+
78+
Additionally, the function supports extending the "kinds" listed above by appending an `_and_generic` suffix to the kind name (e.g., `real_and_generic`).
79+
80+
```javascript
81+
var out = dtypeChars( 'floating_point_and_generic' );
82+
// returns [...]
83+
```
84+
85+
</section>
86+
87+
<!-- /.usage -->
88+
89+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
90+
91+
<section class="notes">
92+
93+
</section>
94+
95+
<!-- /.notes -->
96+
97+
<!-- Package usage examples. -->
98+
99+
<section class="examples">
100+
101+
## Examples
102+
103+
<!-- eslint no-undef: "error" -->
104+
105+
```javascript
106+
var contains = require( '@stdlib/array/base/assert/contains' ).factory;
107+
var dtypeChars = require( '@stdlib/ndarray/base/dtype-chars' );
108+
109+
var isdtypeChar = contains( dtypeChars() );
110+
111+
var bool = isdtypeChar( 'd' );
112+
// returns true
113+
114+
bool = isdtypeChar( 'k' );
115+
// returns true
116+
117+
bool = isdtypeChar( 'b' );
118+
// returns true
119+
120+
bool = isdtypeChar( '~' );
121+
// returns false
122+
```
123+
124+
</section>
125+
126+
<!-- /.examples -->
127+
128+
<!-- 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. -->
129+
130+
<section class="references">
131+
132+
</section>
133+
134+
<!-- /.references -->
135+
136+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
137+
138+
<section class="related">
139+
140+
</section>
141+
142+
<!-- /.related -->
143+
144+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
145+
146+
<section class="links">
147+
148+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
149+
150+
</section>
151+
152+
<!-- /.links -->
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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 pkg = require( './../package.json' ).name;
26+
var dtypeChars = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var out;
33+
var i;
34+
35+
b.tic();
36+
for ( i = 0; i < b.iterations; i++ ) {
37+
out = dtypeChars();
38+
if ( out.length === 0 ) {
39+
b.fail( 'should return a non-empty array' );
40+
}
41+
}
42+
b.toc();
43+
if ( !isStringArray( out ) ) {
44+
b.fail( 'should return an array of strings' );
45+
}
46+
b.pass( 'benchmark finished' );
47+
b.end();
48+
});
49+
50+
bench( pkg+'::kind', function benchmark( b ) {
51+
var values;
52+
var out;
53+
var i;
54+
55+
values = [
56+
'floating_point',
57+
'integer'
58+
];
59+
60+
b.tic();
61+
for ( i = 0; i < b.iterations; i++ ) {
62+
out = dtypeChars( values[ i%values.length ] );
63+
if ( out.length === 0 ) {
64+
b.fail( 'should return a non-empty array' );
65+
}
66+
}
67+
b.toc();
68+
if ( !isStringArray( out ) ) {
69+
b.fail( 'should return an array of strings' );
70+
}
71+
b.pass( 'benchmark finished' );
72+
b.end();
73+
});
74+
75+
bench( pkg+'::kind,generic', function benchmark( b ) {
76+
var values;
77+
var out;
78+
var i;
79+
80+
values = [
81+
'floating_point_and_generic',
82+
'integer_and_generic',
83+
'boolean_and_generic'
84+
];
85+
86+
b.tic();
87+
for ( i = 0; i < b.iterations; i++ ) {
88+
out = dtypeChars( values[ i%values.length ] );
89+
if ( out.length === 0 ) {
90+
b.fail( 'should return a non-empty array' );
91+
}
92+
}
93+
b.toc();
94+
if ( !isStringArray( out ) ) {
95+
b.fail( 'should return an array of strings' );
96+
}
97+
b.pass( 'benchmark finished' );
98+
b.end();
99+
});
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
{{alias}}( [kind] )
3+
Returns a list of ndarray data type single letter character abbreviations.
4+
5+
When not provided a data type "kind", the function returns a list of single
6+
letter character abbreviations for all supported ndarray data types.
7+
8+
The function supports the following data type "kinds":
9+
10+
- floating_point: floating-point data types.
11+
- real_floating_point: real-valued floating-point data types.
12+
- complex_floating_point: complex-valued floating-point data types.
13+
- boolean: boolean data types.
14+
- integer: integer data types.
15+
- signed_integer: signed integer data types.
16+
- unsigned_integer: unsigned integer data types.
17+
- real: real-valued data types.
18+
- numeric: numeric data types.
19+
- typed: typed data types.
20+
- integer_index: integer index data types.
21+
- boolean_index: boolean index data types.
22+
- mask_index: mask index data types.
23+
- typed_index: typed index data types.
24+
- index: index data types.
25+
- all: all data types.
26+
27+
Additionally, the function supports extending the "kinds" listed above by
28+
appending a '_and_generic' suffix to the kind name (e.g., real_and_generic).
29+
30+
Parameters
31+
----------
32+
kind: string (optional)
33+
Data type kind.
34+
35+
Returns
36+
-------
37+
out: Array<string>
38+
List of ndarray data type single letter character abbreviations.
39+
40+
Examples
41+
--------
42+
> var out = {{alias}}()
43+
[...]
44+
> out = {{alias}}( 'floating_point' )
45+
[...]
46+
> out = {{alias}}( 'floating_point_and_generic' )
47+
[...]
48+
49+
See Also
50+
--------
51+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 { DataTypeKind } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Returns a list of ndarray data type single letter character abbreviations.
27+
*
28+
* @param kind - data type kind
29+
* @returns list of ndarray data type single letter character abbreviations
30+
*
31+
* @example
32+
* var list = dtypeChars();
33+
* // returns [...]
34+
*
35+
* @example
36+
* var list = dtypeChars( 'floating_point' );
37+
* // returns [...]
38+
*/
39+
declare function dtypeChars( kind?: DataTypeKind ): Array<string>;
40+
41+
42+
// EXPORTS //
43+
44+
export = dtypeChars;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 dtypeChars = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an array of data type strings...
25+
{
26+
dtypeChars(); // $ExpectType string[]
27+
dtypeChars( 'floating_point' ); // $ExpectType string[]
28+
dtypeChars( 'floating_point_and_generic' ); // $ExpectType string[]
29+
}
30+
31+
// The compiler throws an error if the function is provided an unsupported number of arguments...
32+
{
33+
dtypeChars( 'floating_point', 2 ); // $ExpectError
34+
}

0 commit comments

Comments
 (0)