Skip to content

Commit d06165b

Browse files
committed
feat: add support for extending data type kind subsets with a "generic" data type
1 parent 1cba41b commit d06165b

File tree

6 files changed

+297
-4
lines changed

6 files changed

+297
-4
lines changed

lib/node_modules/@stdlib/ndarray/dtypes/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ The function supports the following data type kinds:
8787
- `typed`: typed data types.
8888
- `all`: all data types.
8989

90+
Additionally, the function supports extending the "kinds" listed above by appending an `_and_generic` suffix to the kind name (e.g., `real_and_generic`).
91+
92+
```javascript
93+
var out = dtypes( 'floating_point_and_generic' );
94+
// returns [...]
95+
```
96+
9097
</section>
9198

9299
<!-- /.usage -->

lib/node_modules/@stdlib/ndarray/dtypes/benchmark/benchmark.js

+26
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,29 @@ bench( pkg+'::kind', function benchmark( b ) {
7171
b.pass( 'benchmark finished' );
7272
b.end();
7373
});
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 = dtypes( 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+
});

lib/node_modules/@stdlib/ndarray/dtypes/docs/repl.txt

+25-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22
{{alias}}( [kind] )
33
Returns a list of ndarray data types.
44

5+
When not provided a data type "kind", the function returns an array
6+
containing the following data types:
7+
8+
- float32: single-precision floating-point numbers.
9+
- float64: double-precision floating-point numbers.
10+
- complex64: single-precision complex floating-point numbers.
11+
- complex128: double-precision complex floating-point numbers.
12+
- bool: boolean values.
13+
- generic: values of any type.
14+
- int16: signed 16-bit integers.
15+
- int32: signed 32-bit integers.
16+
- int8: signed 8-bit integers.
17+
- uint16: unsigned 16-bit integers.
18+
- uint32: unsigned 32-bit integers.
19+
- uint8: unsigned 8-bit integers.
20+
- uint8c: unsigned clamped 8-bit integers.
21+
- binary: binary.
22+
523
The function supports the following data type "kinds":
624

725
- floating_point: floating-point data types.
@@ -16,6 +34,9 @@
1634
- typed: typed data types.
1735
- all: all data types.
1836

37+
Additionally, the function supports extending the "kinds" listed above by
38+
appending a '_and_generic' suffix to the kind name (e.g., real_and_generic).
39+
1940
Parameters
2041
----------
2142
kind: string (optional)
@@ -29,9 +50,11 @@
2950
Examples
3051
--------
3152
> var out = {{alias}}()
32-
<Array>
53+
[...]
3354
> out = {{alias}}( 'floating_point' )
34-
<Array>
55+
[...]
56+
> out = {{alias}}( 'floating_point_and_generic' )
57+
[...]
3558

3659
See Also
3760
--------

lib/node_modules/@stdlib/ndarray/dtypes/docs/types/test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import dtypes = require( './index' );
2525
{
2626
dtypes(); // $ExpectType DataType[]
2727
dtypes( 'floating_point' ); // $ExpectType DataType[]
28+
dtypes( 'floating_point_and_generic' ); // $ExpectType DataType[]
2829
}
2930

3031
// The compiler throws an error if the function is provided an unsupported number of arguments...

lib/node_modules/@stdlib/ndarray/dtypes/lib/main.js

+22-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,15 @@
2020

2121
// MODULES //
2222

23+
var replace = require( '@stdlib/string/base/replace' );
2324
var DTYPES = require( './dtypes.json' );
2425

2526

27+
// VARIABLES //
28+
29+
var RE_SUFFIX = /_and_generic$/;
30+
31+
2632
// MAIN //
2733

2834
/**
@@ -40,12 +46,26 @@ var DTYPES = require( './dtypes.json' );
4046
* // returns [...]
4147
*/
4248
function dtypes() {
49+
var kind;
4350
var out;
51+
var FLG;
4452
if ( arguments.length === 0 ) {
4553
return DTYPES.all.slice();
4654
}
47-
out = DTYPES[ arguments[ 0 ] ];
48-
return ( out ) ? out.slice() : [];
55+
FLG = false;
56+
kind = arguments[ 0 ];
57+
if ( RE_SUFFIX.test( kind ) ) {
58+
kind = replace( kind, RE_SUFFIX, '' );
59+
if ( kind !== 'all' ) {
60+
FLG = true;
61+
}
62+
}
63+
out = DTYPES[ kind ];
64+
out = ( out ) ? out.slice() : [];
65+
if ( FLG && out.length > 0 ) {
66+
out.push( 'generic' );
67+
}
68+
return out;
4969
}
5070

5171

0 commit comments

Comments
 (0)