Skip to content

Commit 5d70e7c

Browse files
committed
Add support for dtype enums (and other dtype values)
1 parent 790df93 commit 5d70e7c

File tree

4 files changed

+12
-29
lines changed

4 files changed

+12
-29
lines changed

lib/node_modules/@stdlib/ndarray/promotion-rules/docs/repl.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212

1313
Parameters
1414
----------
15-
dtype1: string (optional)
16-
ndarray data type.
15+
dtype1: any (optional)
16+
ndarray data type value.
1717

18-
dtype2: string (optional)
19-
ndarray data type.
18+
dtype2: any (optional)
19+
ndarray data type value.
2020

2121
Returns
2222
-------

lib/node_modules/@stdlib/ndarray/promotion-rules/docs/types/index.d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ interface Table {
2929
/**
3030
* Returns the ndarray data type with the smallest size and closest "kind" to which ndarray data types can be safely cast.
3131
*
32-
* @param dtype1 - ndarray data type
33-
* @param dtype2 - ndarray data type
32+
* @param dtype1 - ndarray data type value
33+
* @param dtype2 - ndarray data type value
3434
* @returns promotion rule(s) or null
3535
*
3636
* @example
@@ -45,7 +45,7 @@ interface Table {
4545
* var dt = promotionRules( 'float32', 'foo' );
4646
* // returns null
4747
*/
48-
declare function promotionRules( dtype1: string, dtype2: string ): number | string | null; // tslint-disable-line max-line-length
48+
declare function promotionRules( dtype1: any, dtype2: any ): number | string | null; // tslint-disable-line max-line-length
4949

5050
/**
5151
* Returns a type promotion table displaying the ndarray data types with the smallest size and closest "kind" to which ndarray data types can be safely cast.

lib/node_modules/@stdlib/ndarray/promotion-rules/docs/types/test.ts

-20
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,6 @@ import promotionRules = require( './index' );
2929
promotionRules( 'float32', 'foo' ); // $ExpectType string | number | null
3030
}
3131

32-
// The function does not compile if provided a first argument that is not a string...
33-
{
34-
promotionRules( 123, 'float64' ); // $ExpectError
35-
promotionRules( true, 'float64' ); // $ExpectError
36-
promotionRules( false, 'float64' ); // $ExpectError
37-
promotionRules( null, 'float64' ); // $ExpectError
38-
promotionRules( {}, 'float64' ); // $ExpectError
39-
promotionRules( ( x: number ): number => x, 'float64' ); // $ExpectError
40-
}
41-
42-
// The function does not compile if provided a second argument that is not a string...
43-
{
44-
promotionRules( 'int32', 123 ); // $ExpectError
45-
promotionRules( 'int32', true ); // $ExpectError
46-
promotionRules( 'int32', false ); // $ExpectError
47-
promotionRules( 'int32', null ); // $ExpectError
48-
promotionRules( 'int32', {} ); // $ExpectError
49-
promotionRules( 'int32', ( x: number ): number => x ); // $ExpectError
50-
}
51-
5232
// The function does not compile if provided more than one argument...
5333
{
5434
promotionRules( 'float32' ); // $ExpectError

lib/node_modules/@stdlib/ndarray/promotion-rules/lib/main.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
var objectKeys = require( '@stdlib/utils/keys' );
2424
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
25+
var resolve = require( '@stdlib/ndarray/base/dtype-resolve-str' );
2526
var PROMOTION_RULES = require( './promotion_rules.json' );
2627

2728

@@ -66,8 +67,8 @@ function generateFullTable() {
6667
/**
6768
* Returns the ndarray data type with the smallest size and closest "kind" to which ndarray data types can be safely cast.
6869
*
69-
* @param {string} [dtype1] - ndarray data type
70-
* @param {string} [dtype2] - ndarray data type
70+
* @param {*} [dtype1] - ndarray data type value
71+
* @param {*} [dtype2] - ndarray data type value
7172
* @returns {(Object|integer|string|null)} promotion rule(s) or null
7273
*
7374
* @example
@@ -91,8 +92,10 @@ function promotionRules( dtype1, dtype2 ) {
9192
if ( arguments.length === 0 ) {
9293
return generateFullTable();
9394
}
95+
dtype1 = resolve( dtype1 );
9496
if ( hasOwnProp( PROMOTION_RULES, dtype1 ) ) {
9597
o = PROMOTION_RULES[ dtype1 ];
98+
dtype2 = resolve( dtype2 );
9699
if ( hasOwnProp( o, dtype2 ) ) {
97100
return o[ dtype2 ];
98101
}

0 commit comments

Comments
 (0)