Skip to content

Commit 8526ef8

Browse files
committed
feat: add support for non-string data types
--- 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: na - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - 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: na - task: lint_license_headers status: passed ---
1 parent 8ae1c5e commit 8526ef8

File tree

5 files changed

+138
-11
lines changed

5 files changed

+138
-11
lines changed

lib/node_modules/@stdlib/ndarray/base/assert/is-same-kind-data-type-cast/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ var isSameKindCast = require( '@stdlib/ndarray/base/assert/is-same-kind-data-typ
4242

4343
#### isSameKindCast( from, to )
4444

45-
Returns a `boolean` indicating whether an ndarray [data type][@stdlib/ndarray/dtypes] can be safely cast to, or is of the same "kind" as, another ndarray [data type][@stdlib/ndarray/dtypes] (e.g., casting between signed integers or between floats).
45+
Returns a boolean indicating whether an ndarray [data type][@stdlib/ndarray/dtypes] can be safely cast to, or is of the same "kind" as, another ndarray [data type][@stdlib/ndarray/dtypes] (e.g., casting between signed integers or between floats).
4646

4747
```javascript
4848
var bool = isSameKindCast( 'float32', 'float64' );

lib/node_modules/@stdlib/ndarray/base/assert/is-same-kind-data-type-cast/docs/repl.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
Parameters
77
----------
8-
from: string
9-
ndarray data type.
8+
from: string|DataType
9+
Data type to cast from.
1010

11-
to: string
12-
ndarray data type.
11+
to: string|DataType
12+
Data type to cast to.
1313

1414
Returns
1515
-------

lib/node_modules/@stdlib/ndarray/base/assert/is-same-kind-data-type-cast/docs/types/index.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818

1919
// TypeScript Version: 4.1
2020

21+
/// <reference types="@stdlib/types"/>
22+
23+
import { DataType } from '@stdlib/types/ndarray';
24+
2125
/**
2226
* Returns a boolean indicating if a provided ndarray data type can be safely cast to, or is of the same "kind" as, another ndarray data type.
2327
*
@@ -32,7 +36,7 @@
3236
* bool = isSameKindCast( 'uint16', 'int16' );
3337
* // returns false
3438
*/
35-
declare function isSameKindCast( from: string, to: string ): boolean;
39+
declare function isSameKindCast( from: DataType, to: DataType ): boolean;
3640

3741

3842
// EXPORTS //

lib/node_modules/@stdlib/ndarray/base/assert/is-same-kind-data-type-cast/lib/main.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
// MODULES //
2222

2323
var sameKindCasts = require( '@stdlib/ndarray/same-kind-casts' );
24+
var resolveStr = require( '@stdlib/ndarray/base/dtype-resolve-str' );
2425

2526

2627
// VARIABLES //
@@ -33,8 +34,8 @@ var TABLE = sameKindCasts();
3334
/**
3435
* Returns a boolean indicating if a provided ndarray data type can be safely cast to, or is of the same "kind" as, another ndarray data type.
3536
*
36-
* @param {string} from - ndarray data type
37-
* @param {string} to - ndarray data type
37+
* @param {*} from - ndarray data type
38+
* @param {*} to - ndarray data type
3839
* @returns {boolean} boolean indicating if a data type can be cast to another data type
3940
*
4041
* @example
@@ -45,10 +46,17 @@ var TABLE = sameKindCasts();
4546
* // returns false
4647
*/
4748
function isSameKindCast( from, to ) {
48-
if ( from === to ) {
49+
var t;
50+
from = resolveStr( from );
51+
to = resolveStr( to );
52+
if ( from === to ) { // note: for "struct" data types, require strict equality to be considered a safe cast
4953
return true;
5054
}
51-
return ( TABLE[ from ][ to ] > 0 );
55+
t = TABLE[ from ];
56+
if ( t ) {
57+
return t[ to ] > 0;
58+
}
59+
return false;
5260
}
5361

5462

lib/node_modules/@stdlib/ndarray/base/assert/is-same-kind-data-type-cast/test/test.js

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@
1616
* limitations under the License.
1717
*/
1818

19+
/* eslint-disable max-len */
20+
1921
'use strict';
2022

2123
// MODULES //
2224

2325
var tape = require( 'tape' );
26+
var DataType = require( '@stdlib/ndarray/dtype-ctor' );
27+
var structFactory = require( '@stdlib/dstructs/struct' );
2428
var dtypes = require( '@stdlib/ndarray/dtypes' );
2529
var sameKindCasts = require( '@stdlib/ndarray/same-kind-casts' );
30+
var str2enum = require( '@stdlib/ndarray/base/dtype-str2enum' );
2631
var isSameKindCast = require( './../lib' );
2732

2833

@@ -40,7 +45,7 @@ tape( 'main export is a function', function test( t ) {
4045
t.end();
4146
});
4247

43-
tape( 'the function returns a boolean indicating if an ndarray data type can be cast to another ndarray data type', function test( t ) {
48+
tape( 'the function returns a boolean indicating if an ndarray data type can be cast to another ndarray data type (strings)', function test( t ) {
4449
var expected;
4550
var actual;
4651
var dt;
@@ -57,3 +62,113 @@ tape( 'the function returns a boolean indicating if an ndarray data type can be
5762
}
5863
t.end();
5964
});
65+
66+
tape( 'the function returns a boolean indicating if an ndarray data type can be cast to another ndarray data type (enums)', function test( t ) {
67+
var expected;
68+
var actual;
69+
var dt;
70+
var i;
71+
var j;
72+
73+
for ( i = 0; i < DTYPES.length; i++ ) {
74+
dt = DTYPES[ i ];
75+
for ( j = 0; j < DTYPES.length; j++ ) {
76+
expected = ( SAME_KIND_CASTS[ dt ][ DTYPES[j] ] > 0 );
77+
actual = isSameKindCast( str2enum( dt ), str2enum( DTYPES[ j ] ) );
78+
t.strictEqual( actual, expected, 'returns expected value. from: '+dt+'. to: '+DTYPES[j]+'.' );
79+
}
80+
}
81+
t.end();
82+
});
83+
84+
tape( 'the function returns a boolean indicating if an ndarray data type can be cast to another ndarray data type (data type, strings)', function test( t ) {
85+
var expected;
86+
var actual;
87+
var dt;
88+
var i;
89+
var j;
90+
91+
for ( i = 0; i < DTYPES.length; i++ ) {
92+
dt = DTYPES[ i ];
93+
for ( j = 0; j < DTYPES.length; j++ ) {
94+
expected = ( SAME_KIND_CASTS[ dt ][ DTYPES[j] ] > 0 );
95+
actual = isSameKindCast( new DataType( dt ), new DataType( DTYPES[ j ] ) );
96+
t.strictEqual( actual, expected, 'returns expected value. from: '+dt+'. to: '+DTYPES[j]+'.' );
97+
}
98+
}
99+
t.end();
100+
});
101+
102+
tape( 'the function returns a boolean indicating if an ndarray data type can be cast to another ndarray data type (struct)', function test( t ) {
103+
var schemas;
104+
var values;
105+
var actual;
106+
var dt;
107+
var i;
108+
109+
schemas = [
110+
[
111+
{
112+
'name': 'foo',
113+
'type': 'float64'
114+
}
115+
],
116+
[
117+
{
118+
'name': 'bar',
119+
'type': 'float32'
120+
}
121+
]
122+
];
123+
values = [
124+
structFactory( schemas[ 0 ] ),
125+
structFactory( schemas[ 1 ] )
126+
];
127+
128+
for ( i = 0; i < values.length; i++ ) {
129+
dt = values[ i ];
130+
actual = isSameKindCast( dt, dt );
131+
t.strictEqual( actual, true, 'returns expected value. from: '+dt.layout+'. to: '+dt.layout+'.' );
132+
}
133+
actual = isSameKindCast( values[ 0 ], values[ 1 ] );
134+
t.strictEqual( actual, false, 'returns expected value' );
135+
136+
t.end();
137+
});
138+
139+
tape( 'the function returns a boolean indicating if an ndarray data type can be cast to another ndarray data type (data type, struct)', function test( t ) {
140+
var schemas;
141+
var values;
142+
var actual;
143+
var dt;
144+
var i;
145+
146+
schemas = [
147+
[
148+
{
149+
'name': 'foo',
150+
'type': 'float64'
151+
}
152+
],
153+
[
154+
{
155+
'name': 'bar',
156+
'type': 'float32'
157+
}
158+
]
159+
];
160+
values = [
161+
structFactory( schemas[ 0 ] ),
162+
structFactory( schemas[ 1 ] )
163+
];
164+
165+
for ( i = 0; i < values.length; i++ ) {
166+
dt = values[ i ];
167+
actual = isSameKindCast( new DataType( dt ), new DataType( dt ) );
168+
t.strictEqual( actual, true, 'returns expected value. from: '+dt.layout+'. to: '+dt.layout+'.' );
169+
}
170+
actual = isSameKindCast( new DataType( values[ 0 ] ), new DataType( values[ 1 ] ) );
171+
t.strictEqual( actual, false, 'returns expected value' );
172+
173+
t.end();
174+
});

0 commit comments

Comments
 (0)