README.md (3142B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 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 # 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. 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 safeCasts = require( '@stdlib/array/safe-casts' ); 41 ``` 42 43 #### safeCasts( \[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. 46 47 ```javascript 48 var out = safeCasts( 'float32' ); 49 // e.g., returns [ 'float32', 'float64', 'generic' ] 50 ``` 51 52 If not provided a `dtype` argument, the function returns a casting table. 53 54 ```javascript 55 var out = safeCasts(); 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 = safeCasts( '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 safeCasts = require( '@stdlib/array/safe-casts' ); 95 96 var DTYPES; 97 var list; 98 var i; 99 100 // Get the list of supported array data types: 101 DTYPES = dtypes(); 102 103 // Print the list of array data types to which a data type can be safely cast... 104 for ( i = 0; i < DTYPES.length; i++ ) { 105 list = safeCasts( DTYPES[ i ] ); 106 console.log( '%s: %s', DTYPES[ i ], list.join( ', ' ) ); 107 } 108 ``` 109 110 </section> 111 112 <!-- /.examples --> 113 114 <!-- 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. --> 115 116 <section class="references"> 117 118 </section> 119 120 <!-- /.references --> 121 122 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 123 124 <section class="links"> 125 126 [@stdlib/array/dtypes]: https://www.npmjs.com/package/@stdlib/array/tree/main/dtypes 127 128 </section> 129 130 <!-- /.links -->