Skip to content

Commit 6003449

Browse files
committed
feat: add ndarray/base/assert/is-scalar-mostly-safe-compatible
--- 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: passed - 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: passed - 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: passed - task: lint_license_headers status: passed ---
1 parent bc026be commit 6003449

File tree

10 files changed

+995
-0
lines changed

10 files changed

+995
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# isScalarMostlySafeCompatible
22+
23+
> Determine whether a scalar value can be safely cast or, for floating-point data types, downcast to specified ndarray [data type][@stdlib/ndarray/dtypes].
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+
<!-- eslint-disable id-length -->
40+
41+
```javascript
42+
var isScalarMostlySafeCompatible = require( '@stdlib/ndarray/base/assert/is-scalar-mostly-safe-compatible' );
43+
```
44+
45+
#### isScalarMostlySafeCompatible( value, dtype )
46+
47+
Returns a boolean indicating whether a scalar value can be safely cast or, for floating-point data types, downcast to specified ndarray [data type][@stdlib/ndarray/dtypes].
48+
49+
<!-- eslint-disable id-length -->
50+
51+
```javascript
52+
var bool = isScalarMostlySafeCompatible( 3.14, 'float64' );
53+
// returns true
54+
55+
bool = isScalarMostlySafeCompatible( 3.14, 'int32' );
56+
// returns false
57+
```
58+
59+
</section>
60+
61+
<!-- /.usage -->
62+
63+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
64+
65+
<section class="notes">
66+
67+
</section>
68+
69+
<!-- /.notes -->
70+
71+
<!-- Package usage examples. -->
72+
73+
<section class="examples">
74+
75+
## Examples
76+
77+
<!-- eslint-disable id-length -->
78+
79+
<!-- eslint no-undef: "error" -->
80+
81+
```javascript
82+
var dtypes = require( '@stdlib/ndarray/dtypes' );
83+
var logEachMap = require( '@stdlib/console/log-each-map' );
84+
var isScalarMostlySafeCompatible = require( '@stdlib/ndarray/base/assert/is-scalar-mostly-safe-compatible' );
85+
86+
// Determine whether a decimal value can be cast to various data types...
87+
logEachMap( '%f => %s: %s', 3.14, dtypes(), isScalarMostlySafeCompatible );
88+
```
89+
90+
</section>
91+
92+
<!-- /.examples -->
93+
94+
<!-- 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. -->
95+
96+
<section class="references">
97+
98+
</section>
99+
100+
<!-- /.references -->
101+
102+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
103+
104+
<section class="related">
105+
106+
</section>
107+
108+
<!-- /.related -->
109+
110+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
111+
112+
<section class="links">
113+
114+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
115+
116+
</section>
117+
118+
<!-- /.links -->
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
25+
var dtypes = require( '@stdlib/ndarray/dtypes' );
26+
var pkg = require( './../package.json' ).name;
27+
var isScalarMostlySafeCompatible = require( './../lib' ); // eslint-disable-line id-length
28+
29+
30+
// VARIABLES //
31+
32+
var DTYPES = dtypes();
33+
34+
35+
// MAIN //
36+
37+
bench( pkg, function benchmark( b ) {
38+
var values;
39+
var out;
40+
var i;
41+
42+
values = [
43+
3,
44+
-3,
45+
3.14,
46+
true,
47+
false,
48+
null,
49+
'beep',
50+
{}
51+
];
52+
53+
b.tic();
54+
for ( i = 0; i < b.iterations; i++ ) {
55+
out = isScalarMostlySafeCompatible( values[ i%values.length ], DTYPES[ i%DTYPES.length ] ); // eslint-disable-line max-len
56+
if ( typeof out !== 'boolean' ) {
57+
b.fail( 'should return a boolean' );
58+
}
59+
}
60+
b.toc();
61+
if ( !isBoolean( out ) ) {
62+
b.fail( 'should return a boolean' );
63+
}
64+
b.pass( 'benchmark finished' );
65+
b.end();
66+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
{{alias}}( value, dtype )
3+
Returns a boolean indicating whether a scalar value can be safely cast or,
4+
for floating-point data types, downcast to specified ndarray data type.
5+
6+
Parameters
7+
----------
8+
value: any
9+
Input value.
10+
11+
dtype: string
12+
ndarray data type.
13+
14+
Returns
15+
-------
16+
bool: boolean
17+
Boolean indicating whether a scalar value can be safely cast.
18+
19+
Examples
20+
--------
21+
> var bool = {{alias}}( 3.14, 'float64' )
22+
true
23+
> bool = {{alias}}( 3.14, 'int32' )
24+
false
25+
26+
See Also
27+
--------
28+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
/// <reference types="@stdlib/types"/>
22+
23+
import { DataType } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Returns a boolean indicating whether a scalar value can be safely cast or, for floating-point data types, downcast to specified ndarray data type.
27+
*
28+
* @param value - scalar value
29+
* @param dtype - ndarray data type
30+
* @returns boolean indicating whether a scalar value can be safely cast
31+
*
32+
* @example
33+
* var bool = isScalarMostlySafeCompatible( 3.0, 'float64' );
34+
* // returns true
35+
*
36+
* @example
37+
* var bool = isScalarMostlySafeCompatible( 3.14, 'int32' );
38+
* // returns false
39+
*
40+
* @example
41+
* var bool = isScalarMostlySafeCompatible( -1, 'uint32' );
42+
* // returns false
43+
*/
44+
declare function isScalarMostlySafeCompatible( value: unknown, dtype: DataType ): boolean;
45+
46+
47+
// EXPORTS //
48+
49+
export = isScalarMostlySafeCompatible;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 isScalarMostlySafeCompatible = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a boolean...
25+
{
26+
isScalarMostlySafeCompatible( 3, 'float64' ); // $ExpectType boolean
27+
isScalarMostlySafeCompatible( -1, 'int32' ); // $ExpectType boolean
28+
}
29+
30+
// The compiler throws an error if the function is provided a second argument which is not a data type...
31+
{
32+
isScalarMostlySafeCompatible( 3.14, true ); // $ExpectError
33+
isScalarMostlySafeCompatible( 3.14, false ); // $ExpectError
34+
isScalarMostlySafeCompatible( 3.14, null ); // $ExpectError
35+
isScalarMostlySafeCompatible( 3.14, undefined ); // $ExpectError
36+
isScalarMostlySafeCompatible( 3.14, 123 ); // $ExpectError
37+
isScalarMostlySafeCompatible( 3.14, [] ); // $ExpectError
38+
isScalarMostlySafeCompatible( 3.14, {} ); // $ExpectError
39+
isScalarMostlySafeCompatible( 3.14, ( x: number ): number => x ); // $ExpectError
40+
}
41+
42+
// The compiler throws an error if the function is provided an unsupported number of arguments...
43+
{
44+
isScalarMostlySafeCompatible(); // $ExpectError
45+
isScalarMostlySafeCompatible( 3.14 ); // $ExpectError
46+
isScalarMostlySafeCompatible( 3.14, 'float64', {} ); // $ExpectError
47+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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/ndarray/dtypes' );
22+
var logEachMap = require( '@stdlib/console/log-each-map' );
23+
var isScalarMostlySafeCompatible = require( './../lib' ); // eslint-disable-line id-length
24+
25+
// Determine whether a decimal value can be cast to various data types...
26+
logEachMap( '%f => %s: %s', 3.14, dtypes(), isScalarMostlySafeCompatible );
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
/**
22+
* Determine whether a scalar value can be safely cast or, for floating-point data types, downcast to specified ndarray data type.
23+
*
24+
* @module @stdlib/ndarray/base/assert/is-scalar-mostly-safe-compatible
25+
*
26+
* @example
27+
* var isScalarMostlySafeCompatible = require( '@stdlib/ndarray/base/assert/is-scalar-mostly-safe-compatible' );
28+
*
29+
* var bool = isScalarMostlySafeCompatible( 3.0, 'float64' );
30+
* // returns true
31+
*
32+
* bool = isScalarMostlySafeCompatible( 3.14, 'int32' );
33+
* // returns false
34+
*
35+
* bool = isScalarMostlySafeCompatible( -1, 'uint32' );
36+
* // returns false
37+
*/
38+
39+
// MODULES //
40+
41+
var main = require( './main.js' );
42+
43+
44+
// EXPORTS //
45+
46+
module.exports = main;

0 commit comments

Comments
 (0)