Skip to content

Commit b268a6f

Browse files
committed
feat: add array/mostly-safe-casts
1 parent ca14511 commit b268a6f

File tree

11 files changed

+828
-0
lines changed

11 files changed

+828
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# Mostly Safe Casts
22+
23+
> Return a list of array [data types][@stdlib/array/dtypes] to which a provided array [data type][@stdlib/array/dtypes] can be safely cast and, for floating-point data types, can be downcast.
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 mostlySafeCasts = require( '@stdlib/array/mostly-safe-casts' );
41+
```
42+
43+
#### mostlySafeCasts( \[dtype] )
44+
45+
If provided a `dtype` argument, returns a list of array [data types][@stdlib/array/dtypes] to which a provided array [data type][@stdlib/array/dtypes] can be safely cast and, for floating-point data types, can be downcast.
46+
47+
```javascript
48+
var out = mostlySafeCasts( 'float32' );
49+
// e.g., returns [ 'float32', 'float64', ... ]
50+
```
51+
52+
If not provided a `dtype` argument, the function returns a casting table.
53+
54+
```javascript
55+
var out = mostlySafeCasts();
56+
// returns {...}
57+
58+
var f32 = out[ 'float32' ];
59+
// returns {...}
60+
61+
var v = f32[ 'float64' ];
62+
// returns 1
63+
```
64+
65+
If provided an unrecognized or unsupported `dtype`, the function returns `null`.
66+
67+
```javascript
68+
var out = mostlySafeCasts( 'foo' );
69+
// returns null
70+
```
71+
72+
</section>
73+
74+
<!-- /.usage -->
75+
76+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
77+
78+
<section class="notes">
79+
80+
</section>
81+
82+
<!-- /.notes -->
83+
84+
<!-- Package usage examples. -->
85+
86+
<section class="examples">
87+
88+
## Examples
89+
90+
<!-- eslint no-undef: "error" -->
91+
92+
```javascript
93+
var dtypes = require( '@stdlib/array/dtypes' );
94+
var mostlySafeCasts = require( '@stdlib/array/mostly-safe-casts' );
95+
96+
// Get the list of supported array data types:
97+
var DTYPES = dtypes();
98+
99+
// Print the list of array data types to which a data type can be cast...
100+
var list;
101+
var i;
102+
for ( i = 0; i < DTYPES.length; i++ ) {
103+
list = mostlySafeCasts( DTYPES[ i ] );
104+
console.log( '%s: %s', DTYPES[ i ], list.join( ', ' ) );
105+
}
106+
```
107+
108+
</section>
109+
110+
<!-- /.examples -->
111+
112+
<!-- 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. -->
113+
114+
<section class="references">
115+
116+
</section>
117+
118+
<!-- /.references -->
119+
120+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
121+
122+
<section class="related">
123+
124+
</section>
125+
126+
<!-- /.related -->
127+
128+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
129+
130+
<section class="links">
131+
132+
[@stdlib/array/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/dtypes
133+
134+
</section>
135+
136+
<!-- /.links -->
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 dtypes = require( '@stdlib/array/dtypes' );
26+
var pkg = require( './../package.json' ).name;
27+
var mostlySafeCasts = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var out;
34+
var i;
35+
36+
b.tic();
37+
for ( i = 0; i < b.iterations; i++ ) {
38+
out = mostlySafeCasts();
39+
if ( typeof out !== 'object' ) {
40+
b.fail( 'should return an object' );
41+
}
42+
}
43+
b.toc();
44+
if ( typeof out !== 'object' ) {
45+
b.fail( 'should return an object' );
46+
}
47+
b.pass( 'benchmark finished' );
48+
b.end();
49+
});
50+
51+
bench( pkg+'::dtype', function benchmark( b ) {
52+
var out;
53+
var dt;
54+
var i;
55+
56+
dt = dtypes();
57+
58+
b.tic();
59+
for ( i = 0; i < b.iterations; i++ ) {
60+
out = mostlySafeCasts( dt[ i%dt.length ] );
61+
if ( out.length === 0 ) {
62+
b.fail( 'should not be empty' );
63+
}
64+
}
65+
b.toc();
66+
if ( !isStringArray( out ) ) {
67+
b.fail( 'should return an array of strings' );
68+
}
69+
b.pass( 'benchmark finished' );
70+
b.end();
71+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
{{alias}}( [dtype] )
3+
Returns a list of array data types to which a provided array data type can
4+
be safely cast and, for floating-point data types, can be downcast.
5+
6+
If not provided an array data type, the function returns a casting table.
7+
8+
If provided an unrecognized array data type, the function returns `null`.
9+
10+
Parameters
11+
----------
12+
dtype: any (optional)
13+
Array data type value.
14+
15+
Returns
16+
-------
17+
out: Object|Array<string>|null
18+
Array data types to which a data type can be cast.
19+
20+
Examples
21+
--------
22+
> var out = {{alias}}( 'float32' )
23+
<Array>
24+
25+
See Also
26+
--------
27+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
/**
22+
* Returns a list of array data types to which a provided array data type can be safely cast and, for floating-point data types, can be downcast.
23+
*
24+
* ## Notes
25+
*
26+
* - If not provided an array data type, the function returns a casting table.
27+
* - If provided an unrecognized array data type, the function returns `null`.
28+
*
29+
* @param dtype - array data type value
30+
* @returns list of array data types or null
31+
*
32+
* @example
33+
* var list = mostlySafeCasts( 'float32' );
34+
* // returns [...]
35+
*/
36+
declare function mostlySafeCasts( dtype?: any ): any; // FIXME: use overloads to improve this definition
37+
38+
39+
// EXPORTS //
40+
41+
export = mostlySafeCasts;
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) 2024 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 mostlySafeCasts = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an object, array of strings, or null...
25+
{
26+
mostlySafeCasts(); // $ExpectType any
27+
mostlySafeCasts( 'float32' ); // $ExpectType any
28+
mostlySafeCasts( 'float' ); // $ExpectType any
29+
}
30+
31+
// The compiler throws an error if the function is provided more than one argument...
32+
{
33+
mostlySafeCasts( 'float32', 123 ); // $ExpectError
34+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
var dtypes = require( '@stdlib/array/dtypes' );
22+
var mostlySafeCasts = require( './../lib' );
23+
24+
// Get the list of supported array data types:
25+
var DTYPES = dtypes();
26+
27+
// Print the list of array data types to which a data type can be cast...
28+
var list;
29+
var i;
30+
for ( i = 0; i < DTYPES.length; i++ ) {
31+
list = mostlySafeCasts( DTYPES[ i ] );
32+
console.log( '%s: %s', DTYPES[ i ], list.join( ', ' ) );
33+
}

0 commit comments

Comments
 (0)