Skip to content

Commit ffe223b

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: passed - 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 599cf73 commit ffe223b

File tree

6 files changed

+761
-71
lines changed

6 files changed

+761
-71
lines changed

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

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ var isAllowedCast = require( '@stdlib/ndarray/base/assert/is-allowed-data-type-c
4242

4343
#### isAllowedCast( from, to, casting )
4444

45-
Returns a boolean indicating whether an ndarray [data type][@stdlib/ndarray/dtypes] can be cast to another ndarray [data type][@stdlib/ndarray/dtypes] according to a specified [`casting`][@stdlib/ndarray/casting-modes] mode.
45+
Returns a boolean indicating whether an ndarray [data type][@stdlib/ndarray/dtypes] can be cast to another ndarray [data type][@stdlib/ndarray/dtypes] according to a specified [casting][@stdlib/ndarray/casting-modes] mode.
4646

4747
```javascript
4848
var bool = isAllowedCast( 'float32', 'float64', 'safe' );
@@ -52,14 +52,14 @@ bool = isAllowedCast( 'float64', 'int32', 'safe' );
5252
// returns false
5353
```
5454

55-
The following [`casting`][@stdlib/ndarray/casting-modes] modes are supported:
55+
The following [casting][@stdlib/ndarray/casting-modes] modes are supported:
5656

57-
- `none`: only allow casting between identical types.
58-
- `equiv`: allow casting between identical and byte swapped types.
59-
- `safe`: only allow "safe" casts.
60-
- `mostly-safe`: allow "safe" casts and, for floating-point data types, downcasts.
61-
- `same-kind`: allow "safe" casts and casts within the same kind (e.g., between signed integers or between floats).
62-
- `unsafe`: allow casting between all types (including between integers and floats).
57+
- **none**: only allow casting between identical types.
58+
- **equiv**: allow casting between identical and byte swapped types.
59+
- **safe**: only allow "safe" casts.
60+
- **mostly-safe**: allow "safe" casts and, for floating-point data types, downcasts.
61+
- **same-kind**: allow "safe" casts and casts within the same kind (e.g., between signed integers or between floats).
62+
- **unsafe**: allow casting between all types (including between integers and floats).
6363

6464
</section>
6565

@@ -82,34 +82,21 @@ The following [`casting`][@stdlib/ndarray/casting-modes] modes are supported:
8282
<!-- eslint no-undef: "error" -->
8383

8484
```javascript
85+
var nCartesianProduct = require( '@stdlib/array/base/n-cartesian-product' );
86+
var unzip = require( '@stdlib/utils/unzip' );
8587
var dtypes = require( '@stdlib/ndarray/dtypes' );
8688
var modes = require( '@stdlib/ndarray/casting-modes' );
89+
var logEachMap = require( '@stdlib/console/log-each-map' );
8790
var isAllowedCast = require( '@stdlib/ndarray/base/assert/is-allowed-data-type-cast' );
8891

89-
var DTYPES;
90-
var MODES;
91-
var bool;
92-
var dt1;
93-
var dt2;
94-
var i;
95-
var j;
96-
var k;
97-
98-
// Get a list of supported ndarray data types and casting modes:
99-
DTYPES = dtypes();
100-
MODES = modes();
101-
102-
// For each data type and mode, determine whether one can cast to another data type...
103-
for ( i = 0; i < DTYPES.length; i++ ) {
104-
dt1 = DTYPES[ i ];
105-
for ( j = 0; j < DTYPES.length; j++ ) {
106-
dt2 = DTYPES[ j ];
107-
for ( k = 0; k < MODES.length; k++ ) {
108-
bool = isAllowedCast( dt1, dt2, MODES[ k ] );
109-
console.log( '%s => %s. %s: %s.', dt1, dt2, MODES[ k ], bool );
110-
}
111-
}
112-
}
92+
// Generate a list of dtype pairs and casting modes:
93+
var pairs = nCartesianProduct( dtypes(), dtypes(), modes() );
94+
95+
// Unzip the list into individual arrays:
96+
var lists = unzip( pairs );
97+
98+
// For each data type pair and mode, determine whether one can cast to another data type:
99+
logEachMap( '%s => %s. %s: %s.', lists[ 0 ], lists[ 1 ], lists[ 2 ], isAllowedCast );
113100
```
114101

115102
</section>

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99
- 'equiv': allow casting between identical and byte swapped types.
1010
- 'safe': only allow "safe" casts.
1111
- 'mostly-safe': allow "safe" casts and, for floating-point data types,
12-
downcasts.
12+
downcasts.
1313
- 'same-kind': allow "safe" casts and casts within the same kind (e.g.,
14-
between signed integers or between floats).
14+
between signed integers or between floats).
1515
- 'unsafe': allow casting between all types (including between integers and
16-
floats).
16+
floats).
1717

1818
Parameters
1919
----------
20-
from: string
21-
ndarray data type.
20+
from: string|DataType
21+
Data type to cast from.
2222

23-
to: string
24-
ndarray data type.
23+
to: string|DataType
24+
Data type to cast to.
2525

2626
casting: string
2727
ndarray casting mode.

lib/node_modules/@stdlib/ndarray/base/assert/is-allowed-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 cast to another ndarray data type according to a specified casting mode.
2327
*
@@ -33,7 +37,7 @@
3337
* bool = isAllowedCast( 'float64', 'int32', 'safe' );
3438
* // returns false
3539
*/
36-
declare function isAllowedCast( from: string, to: string, casting: string ): boolean;
40+
declare function isAllowedCast( from: DataType, to: DataType, casting: string ): boolean;
3741

3842

3943
// EXPORTS //

lib/node_modules/@stdlib/ndarray/base/assert/is-allowed-data-type-cast/examples/index.js

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,18 @@
1818

1919
'use strict';
2020

21+
var nCartesianProduct = require( '@stdlib/array/base/n-cartesian-product' );
22+
var unzip = require( '@stdlib/utils/unzip' );
2123
var dtypes = require( '@stdlib/ndarray/dtypes' );
2224
var modes = require( '@stdlib/ndarray/casting-modes' );
25+
var logEachMap = require( '@stdlib/console/log-each-map' );
2326
var isAllowedCast = require( './../lib' );
2427

25-
var DTYPES;
26-
var MODES;
27-
var bool;
28-
var dt1;
29-
var dt2;
30-
var i;
31-
var j;
32-
var k;
28+
// Generate a list of dtype pairs and casting modes:
29+
var pairs = nCartesianProduct( dtypes(), dtypes(), modes() );
3330

34-
// Get a list of supported ndarray data types and casting modes:
35-
DTYPES = dtypes();
36-
MODES = modes();
31+
// Unzip the list into individual arrays:
32+
var lists = unzip( pairs );
3733

38-
// For each data type and mode, determine whether one can cast to another data type...
39-
for ( i = 0; i < DTYPES.length; i++ ) {
40-
dt1 = DTYPES[ i ];
41-
for ( j = 0; j < DTYPES.length; j++ ) {
42-
dt2 = DTYPES[ j ];
43-
for ( k = 0; k < MODES.length; k++ ) {
44-
bool = isAllowedCast( dt1, dt2, MODES[ k ] );
45-
console.log( '%s => %s. %s: %s.', dt1, dt2, MODES[ k ], bool );
46-
}
47-
}
48-
}
34+
// For each data type pair and mode, determine whether one can cast to another data type:
35+
logEachMap( '%s => %s. %s: %s.', lists[ 0 ], lists[ 1 ], lists[ 2 ], isAllowedCast );

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@
2323
var isSafeCast = require( '@stdlib/ndarray/base/assert/is-safe-data-type-cast' );
2424
var isMostlySafeCast = require( '@stdlib/ndarray/base/assert/is-mostly-safe-data-type-cast' );
2525
var isSameKindCast = require( '@stdlib/ndarray/base/assert/is-same-kind-data-type-cast' );
26+
var resolveStr = require( '@stdlib/ndarray/base/dtype-resolve-str' );
2627

2728

2829
// MAIN //
2930

3031
/**
3132
* Returns a boolean indicating if a provided ndarray data type can be cast to another ndarray data type according to a specified casting mode.
3233
*
33-
* @param {string} from - ndarray data type
34-
* @param {string} to - ndarray data type
34+
* @param {*} from - ndarray data type
35+
* @param {*} to - ndarray data type
3536
* @param {string} casting - ndarray casting mode
3637
* @returns {boolean} boolean indicating if a data type can be cast to another data type
3738
*
@@ -43,12 +44,18 @@ var isSameKindCast = require( '@stdlib/ndarray/base/assert/is-same-kind-data-typ
4344
* // returns false
4445
*/
4546
function isAllowedCast( from, to, casting ) {
47+
var dt1;
48+
var dt2;
49+
4650
// Anything goes for "unsafe" casting...
4751
if ( casting === 'unsafe' ) {
4852
return true;
4953
}
54+
dt1 = resolveStr( from );
55+
dt2 = resolveStr( to );
56+
5057
// "Casting" to the same data type is always allowed, regardless of the casting mode...
51-
if ( from === to ) {
58+
if ( dt1 === dt2 ) {
5259
return true;
5360
}
5461
// No casts between different data types are allowed in "none" or "equiv" casting modes...

0 commit comments

Comments
 (0)