Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | /**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
// MODULES //
var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
var isStruct = require( '@stdlib/assert/is-struct' );
var isComplexLike = require( '@stdlib/assert/is-complex-like' );
var isComplexDataType = require( '@stdlib/ndarray/base/assert/is-complex-floating-point-data-type' );
var isBooleanDataType = require( '@stdlib/ndarray/base/assert/is-boolean-data-type' );
var isRealFloatingDataType = require( '@stdlib/ndarray/base/assert/is-real-floating-point-data-type' );
var isUnsignedIntegerDataType = require( '@stdlib/ndarray/base/assert/is-unsigned-integer-data-type' );
var isSignedIntegerDataType = require( '@stdlib/ndarray/base/assert/is-signed-integer-data-type' );
var isStructDataType = require( '@stdlib/ndarray/base/assert/is-struct-data-type' );
var isSafeCast = require( '@stdlib/ndarray/base/assert/is-safe-data-type-cast' );
var minDataType = require( '@stdlib/ndarray/min-dtype' );
var minSignedIntegerDataType = require( '@stdlib/ndarray/base/min-signed-integer-dtype' );
var resolveStr = require( '@stdlib/ndarray/base/dtype-resolve-str' );
// FUNCTIONS //
/**
* Verifies whether a provided value can be safely cast to a "generic" or unknown data type.
*
* @private
* @param {*} value - input value
* @param {string} dtype - data type
* @returns {boolean} boolean result
*
* @example
* var out = validateGeneric( 3, 'generic' );
* // returns true
*/
function validateGeneric() {
return true;
}
/**
* Verifies whether a provided value can be safely cast to a boolean data type.
*
* @private
* @param {*} value - input value
* @param {string} dtype - data type
* @returns {boolean} boolean result
*
* @example
* var out = validateBoolean( true, 'bool' );
* // returns true
*
* @example
* var Complex128 = require( '@stdlib/complex/float64/ctor' );
*
* var out = validateBoolean( new Complex128( 5.0, 6.0 ), 'bool' );
* // returns false
*/
function validateBoolean( value ) {
return isBoolean( value );
}
/**
* Verifies whether a provided value can be safely cast to a real-valued floating-point data type.
*
* @private
* @param {*} value - input value
* @param {string} dtype - data type
* @returns {boolean} boolean result
*
* @example
* var out = validateRealFloating( 3.14, 'float64' );
* // returns true
*
* @example
* var Complex128 = require( '@stdlib/complex/float64/ctor' );
*
* var out = validateRealFloating( new Complex128( 5.0, 6.0 ), 'float64' );
* // returns false
*/
function validateRealFloating( value ) {
return isNumber( value );
}
/**
* Verifies whether a provided value can be safely cast to a complex-valued floating-point data type.
*
* @private
* @param {*} value - input value
* @param {string} dtype - data type
* @returns {boolean} boolean result
*
* @example
* var Complex128 = require( '@stdlib/complex/float64/ctor' );
*
* var out = validateComplexFloating( new Complex128( 5.0, 6.0 ), 'complex128' );
* // returns true
*
* @example
* var out = validateComplexFloating( {}, 'complex128' );
* // returns false
*/
function validateComplexFloating( value ) {
return ( isNumber( value ) || isComplexLike( value ) );
}
/**
* Verifies whether a provided value can be safely cast to a signed integer data type.
*
* @private
* @param {*} value - input value
* @param {string} dtype - data type
* @returns {boolean} boolean result
*
* @example
* var out = validateSignedInteger( 3, 'int32' );
* // returns true
*
* @example
* var out = validateSignedInteger( 3.14, 'int32' );
* // returns false
*/
function validateSignedInteger( value, dtype ) {
return ( isInteger( value ) && isSafeCast( minSignedIntegerDataType( value ), dtype ) ); // eslint-disable-line max-len
}
/**
* Verifies whether a provided value can be safely cast to an unsigned integer data type.
*
* @private
* @param {*} value - input value
* @param {string} dtype - array data type
* @returns {boolean} boolean result
*
* @example
* var out = validateUnsignedInteger( 3, 'uint32' );
* // returns true
*
* @example
* var out = validateUnsignedInteger( -3, 'uint32' );
* // returns false
*/
function validateUnsignedInteger( value, dtype ) {
return ( isInteger( value ) && isSafeCast( minDataType( value ), dtype ) );
}
/**
* Verifies whether a provided value can be safely cast to a binary data type.
*
* @private
* @param {*} value - input value
* @param {string} dtype - array data type
* @returns {boolean} boolean result
*
* @example
* var out = validateBinary( 3, 'binary' );
* // returns true
*
* @example
* var out = validateBinary( -3, 'binary' );
* // returns false
*/
function validateBinary( value ) {
return ( isInteger( value ) && minDataType( value ) === 'uint8' );
}
/**
* Verifies whether a provided value can be safely cast to a struct data type.
*
* @private
* @param {*} value - input value
* @param {string} dtype - array data type
* @returns {boolean} boolean result
*
* @example
* var structFactory = require( '@stdlib/dstructs/struct' );
*
* var schema = [
* {
* 'name': 'value',
* 'type': 'float64'
* }
* ];
* var Struct = structFactory( schema );
*
* var data = {
* 'value': 3.0
* };
* var s = new Struct( data );
*
* var out = validateStruct( s, '|<float64>[0,8]|' );
* // returns true
*
* @example
* var structFactory = require( '@stdlib/dstructs/struct' );
*
* var schema = [
* {
* 'name': 'value',
* 'type': 'float32'
* }
* ];
* var Struct = structFactory( schema );
*
* var data = {
* 'value': 3.0
* };
* var s = new Struct( data );
*
* var out = validateStruct( s, '|<float64>[0,8]|' );
* // returns false
*/
function validateStruct( value, dtype ) {
var o = {
'format': 'layout'
};
return ( isStruct( value ) && value.toString( o ) === dtype );
}
// MAIN //
/**
* Returns a boolean indicating whether a scalar value can be safely cast or, for floating-point data types, downcast to specified ndarray data type.
*
* @param {*} value - scalar value
* @param {*} dtype - ndarray data type
* @returns {boolean} boolean indicating whether a scalar value can be safely cast
*
* @example
* var bool = isScalarMostlySafeCompatible( 3.0, 'float64' );
* // returns true
*
* @example
* var bool = isScalarMostlySafeCompatible( 3.14, 'int32' );
* // returns false
*
* @example
* var bool = isScalarMostlySafeCompatible( -1, 'uint32' );
* // returns false
*/
function isScalarMostlySafeCompatible( value, dtype ) { // eslint-disable-line id-length
var dt = resolveStr( dtype ) || ''; // note: empty string when unable to resolve a second argument to a data type string
if ( dt === 'generic' ) {
return validateGeneric( value, dt );
}
if ( dt === 'binary' ) {
return validateBinary( value, dt );
}
if ( isRealFloatingDataType( dt ) ) {
return validateRealFloating( value, dt );
}
if ( isUnsignedIntegerDataType( dt ) ) {
return validateUnsignedInteger( value, dt );
}
if ( isSignedIntegerDataType( dt ) ) {
return validateSignedInteger( value, dt );
}
if ( isBooleanDataType( dt ) ) {
return validateBoolean( value, dt );
}
if ( isComplexDataType( dt ) ) {
return validateComplexFloating( value, dt );
}
if ( isStructDataType( dtype ) ) {
return validateStruct( value, dt );
}
return false;
}
// EXPORTS //
module.exports = isScalarMostlySafeCompatible;
|