Skip to content

Commit c26710a

Browse files
kgrytesahil20021008
authored andcommitted
refactor: enforce mostly safe casts
This commit updates documentation and tests to match the "base" implementation `ndarray/base/fill`. --- 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: na - 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 818e011 commit c26710a

File tree

5 files changed

+49
-1
lines changed

5 files changed

+49
-1
lines changed

lib/node_modules/@stdlib/ndarray/fill/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ The function accepts the following arguments:
7373

7474
- An input [`ndarray`][@stdlib/ndarray/ctor] **must** be writable. If provided a **read-only** [`ndarray`][@stdlib/ndarray/ctor], the function throws an error.
7575
- If `value` is a number and `x` has a complex [data type][@stdlib/ndarray/dtypes], the function fills an input [`ndarray`][@stdlib/ndarray/ctor] with a complex number whose real component equals the provided scalar `value` and whose imaginary component is zero.
76+
- A `value` must be able to safely cast to the input [`ndarray`][@stdlib/ndarray/ctor] [data type][@stdlib/ndarray/dtypes]. Scalar values having floating-point data types (both real and complex) are allowed to downcast to a lower precision data type of the same kind (e.g., a scalar double-precision floating-point number can be used to fill a 'float32' input [`ndarray`][@stdlib/ndarray/ctor]).
7677
- The function **mutates** the input [`ndarray`][@stdlib/ndarray/ctor].
7778

7879
</section>

lib/node_modules/@stdlib/ndarray/fill/docs/repl.txt

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
Input ndarray.
1515

1616
value: any
17-
Scalar value.
17+
Scalar value. Must be able to safely cast to the input ndarray data
18+
type. Scalar values having floating-point data types (both real and
19+
complex) are allowed to downcast to a lower precision data type of the
20+
same kind (e.g., a scalar double-precision floating-point number can be
21+
used to fill a 'float32' input ndarray).
1822

1923
Returns
2024
-------

lib/node_modules/@stdlib/ndarray/fill/docs/types/index.d.ts

+9
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { ComplexLike } from '@stdlib/types/complex';
2929
* ## Notes
3030
*
3131
* - If `value` is a number, the function fills an input ndarray with a complex number whose real component equals the provided scalar value and whose imaginary component is zero.
32+
* - A `value` must be able to safely cast to the input ndarray data type. Scalar values having floating-point data types (both real and complex) are allowed to downcast to a lower precision data type of the same kind (e.g., a scalar double-precision floating-point number can be used to fill a 'float32' input ndarray).
3233
*
3334
* @param x - input ndarray
3435
* @param value - scalar value
@@ -52,6 +53,10 @@ declare function fill<T extends complexndarray>( x: T, value: number | ComplexLi
5253
/**
5354
* Fills an input ndarray with a specified value.
5455
*
56+
* ## Notes
57+
*
58+
* - A `value` must be able to safely cast to the input ndarray data type. Scalar values having floating-point data types (both real and complex) are allowed to downcast to a lower precision data type of the same kind (e.g., a scalar double-precision floating-point number can be used to fill a 'float32' input ndarray).
59+
*
5560
* @param x - input ndarray
5661
* @param value - scalar value
5762
* @returns input ndarray
@@ -74,6 +79,10 @@ declare function fill<T = unknown, U extends genericndarray<T> = genericndarray<
7479
/**
7580
* Fills an input ndarray with a specified value.
7681
*
82+
* ## Notes
83+
*
84+
* - A `value` must be able to safely cast to the input ndarray data type. Scalar values having floating-point data types (both real and complex) are allowed to downcast to a lower precision data type of the same kind (e.g., a scalar double-precision floating-point number can be used to fill a 'float32' input ndarray).
85+
*
7786
* @param x - input ndarray
7887
* @param value - scalar value
7988
* @returns input ndarray

lib/node_modules/@stdlib/ndarray/fill/lib/main.js

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ var format = require( '@stdlib/string/format' );
3434
* @param {ndarray} x - input ndarray
3535
* @param {*} value - scalar value
3636
* @throws {TypeError} first argument must be an ndarray-like object
37+
* @throws {TypeError} second argument cannot be safely cast to the input ndarray data type
3738
* @throws {Error} cannot write to a read-only ndarray
3839
* @returns {ndarray} input ndarray
3940
*

lib/node_modules/@stdlib/ndarray/fill/test/test.js

+33
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,39 @@ tape( 'the function throws an error if provided a first argument which is a read
8686
}
8787
});
8888

89+
tape( 'the function throws an error if provided a second argument which cannot be safely cast to the input ndarray data type', function test( t ) {
90+
var values;
91+
var x;
92+
var i;
93+
94+
x = scalar2ndarray( 0.0, {
95+
'dtype': 'int32'
96+
});
97+
98+
values = [
99+
'5',
100+
3.14,
101+
NaN,
102+
true,
103+
false,
104+
null,
105+
void 0,
106+
[],
107+
{},
108+
function noop() {}
109+
];
110+
for ( i = 0; i < values.length; i++ ) {
111+
t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] );
112+
}
113+
t.end();
114+
115+
function badValue( value ) {
116+
return function badValue() {
117+
fill( x, value );
118+
};
119+
}
120+
});
121+
89122
tape( 'the function fills a 0-dimensional input ndarray with a specified value', function test( t ) {
90123
var expected;
91124
var x;

0 commit comments

Comments
 (0)