Skip to content

Commit e81031d

Browse files
committed
feat: add assert/is-float16array
1 parent 279ed57 commit e81031d

File tree

10 files changed

+728
-0
lines changed

10 files changed

+728
-0
lines changed
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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+
# isFloat16Array
22+
23+
> Test if a value is a [Float16Array][mdn-float16array].
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var isFloat16Array = require( '@stdlib/assert/is-float16array' );
31+
```
32+
33+
#### isFloat16Array( value )
34+
35+
Tests if a value is a [`Float16Array`][mdn-float16array].
36+
37+
```javascipt
38+
var Float16Array = require( '@stdlib/array/float16' );
39+
40+
var bool = isFloat16Array( new Float16Array( 10 ) );
41+
// returns true
42+
43+
bool = isFloat16Array( [] );
44+
// returns false
45+
```
46+
47+
</section>
48+
49+
<!-- /.usage -->
50+
51+
<section class="examples">
52+
53+
## Examples
54+
55+
<!-- eslint no-undef: "error" -->
56+
57+
```javascript
58+
var Int8Array = require( '@stdlib/array/int8' );
59+
var Uint8Array = require( '@stdlib/array/uint8' );
60+
var Uint8ClampedArray = require( '@stdlib/array/uint8c' );
61+
var Int16Array = require( '@stdlib/array/int16' );
62+
var Uint16Array = require( '@stdlib/array/uint16' );
63+
var Int32Array = require( '@stdlib/array/int32' );
64+
var Uint32Array = require( '@stdlib/array/uint32' );
65+
var Float32Array = require( '@stdlib/array/float32' );
66+
var Float64Array = require( '@stdlib/array/float64' );
67+
var Float16Array = require( '@stdlib/array/float16' );
68+
var isFloat16Array = require( '@stdlib/assert/is-float16array' );
69+
70+
var bool = isFloat16Array( new Float16Array( 10 ) );
71+
// returns true
72+
73+
bool = isFloat16Array( new Int8Array( 10 ) );
74+
// returns false
75+
76+
bool = isFloat16Array( new Uint8Array( 10 ) );
77+
// returns false
78+
79+
bool = isFloat16Array( new Uint8ClampedArray( 10 ) );
80+
// returns false
81+
82+
bool = isFloat16Array( new Int16Array( 10 ) );
83+
// returns false
84+
85+
bool = isFloat16Array( new Uint16Array( 10 ) );
86+
// returns false
87+
88+
bool = isFloat16Array( new Int32Array( 10 ) );
89+
// returns false
90+
91+
bool = isFloat16Array( new Uint32Array( 10 ) );
92+
// returns false
93+
94+
bool = isFloat16Array( new Float32Array( 10 ) );
95+
// returns false
96+
97+
bool = isFloat16Array( new Float64Array( 10 ) );
98+
// returns false
99+
100+
bool = isFloat16Array( new Array( 10 ) );
101+
// returns false
102+
103+
bool = isFloat16Array( {} );
104+
// returns false
105+
106+
bool = isFloat16Array( null );
107+
// returns false
108+
```
109+
110+
</section>
111+
112+
<!-- /.examples -->
113+
114+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
115+
116+
<section class="related">
117+
118+
* * *
119+
120+
## See Also
121+
122+
- <span class="package-name">[`@stdlib/assert/is-float64array`][@stdlib/assert/is-float64array]</span><span class="delimiter">: </span><span class="description">test if a value is a Float64Array.</span>
123+
124+
- <span class="package-name">[`@stdlib/assert/is-float32array`][@stdlib/assert/is-float32array]</span><span class="delimiter">: </span><span class="description">test if a value is a Float32Array.</span>
125+
126+
</section>
127+
128+
<!-- /.related -->
129+
130+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
131+
132+
<section class="links">
133+
134+
[mdn-float16array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float16Array
135+
136+
<!-- <related-links> -->
137+
138+
[mdn-float32array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float32Array
139+
140+
<!-- <related-links> -->
141+
142+
[@stdlib/assert/is-float64array]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/assert/is-float64array
143+
144+
<!-- </related-links> -->
145+
146+
</section>
147+
148+
<!-- /.links -->
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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 Int8Array = require( '@stdlib/array/int8' );
25+
var Uint8Array = require( '@stdlib/array/uint8' );
26+
var Uint8ClampedArray = require( '@stdlib/array/uint8c' );
27+
var Int16Array = require( '@stdlib/array/int16' );
28+
var Uint16Array = require( '@stdlib/array/uint16' );
29+
var Int32Array = require( '@stdlib/array/int32' );
30+
var Uint32Array = require( '@stdlib/array/uint32' );
31+
var Float32Array = require( '@stdlib/array/float32' );
32+
var Float64Array = require( '@stdlib/array/float64' );
33+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
34+
var pkg = require( './../package.json' ).name;
35+
var isFloat16Array = require( './../lib' );
36+
var Float16Array = globalThis[ Symbol.for( '@@stdlib/array/float16' ) ];
37+
38+
var opts = {
39+
skip: ( typeof Float16Array !== 'function' )
40+
};
41+
42+
// MAIN //
43+
44+
bench( pkg, opts, function benchmark( b ) {
45+
var values;
46+
var bool;
47+
var i;
48+
49+
values = [
50+
new Float64Array( 10 ),
51+
new Float16Array( 10 ),
52+
new Float32Array( 10 ),
53+
new Int32Array( 10 ),
54+
new Uint32Array( 10 ),
55+
new Int16Array( 10 ),
56+
new Uint16Array( 10 ),
57+
new Int8Array( 10 ),
58+
new Uint8Array( 10 ),
59+
new Uint8ClampedArray( 10 )
60+
];
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
bool = isFloat16Array( values[ i%values.length ] );
65+
if ( typeof bool !== 'boolean' ) {
66+
b.fail( 'should return a boolean' );
67+
}
68+
}
69+
b.toc();
70+
if ( !isBoolean( bool ) ) {
71+
b.fail( 'should return a boolean' );
72+
}
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
});
76+
77+
bench( pkg+'::true', opts, function benchmark( b ) {
78+
var values;
79+
var bool;
80+
var i;
81+
82+
values = [
83+
new Float16Array( 10 ),
84+
new Float16Array( 10 )
85+
];
86+
87+
b.tic();
88+
for ( i = 0; i < b.iterations; i++ ) {
89+
bool = isFloat16Array( values[ i%values.length ] );
90+
if ( typeof bool !== 'boolean' ) {
91+
b.fail( 'should return a boolean' );
92+
}
93+
}
94+
b.toc();
95+
if ( !isBoolean( bool ) ) {
96+
b.fail( 'should return a boolean' );
97+
}
98+
b.pass( 'benchmark finished' );
99+
b.end();
100+
});
101+
102+
bench( pkg+'::false', opts, function benchmark( b ) {
103+
var values;
104+
var bool;
105+
var i;
106+
107+
values = [
108+
new Float64Array( 10 ),
109+
new Float32Array( 10 ),
110+
new Int32Array( 10 ),
111+
new Uint32Array( 10 ),
112+
new Int16Array( 10 ),
113+
new Uint16Array( 10 ),
114+
new Int8Array( 10 ),
115+
new Uint8Array( 10 ),
116+
new Uint8ClampedArray( 10 )
117+
];
118+
119+
b.tic();
120+
for ( i = 0; i < b.iterations; i++ ) {
121+
bool = isFloat16Array( values[ i%values.length ] );
122+
if ( typeof bool !== 'boolean' ) {
123+
b.fail( 'should return a boolean' );
124+
}
125+
}
126+
b.toc();
127+
if ( !isBoolean( bool ) ) {
128+
b.fail( 'should return a boolean' );
129+
}
130+
b.pass( 'benchmark finished' );
131+
b.end();
132+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{{alias}}( value )
2+
Tests if a value is a Float16Array.
3+
4+
Parameters
5+
----------
6+
value: any
7+
Value to test.
8+
9+
Returns
10+
-------
11+
bool: boolean
12+
Boolean indicating whether value is a Float16Array.
13+
14+
Examples
15+
--------
16+
> var bool = {{alias}}( new {{alias:@stdlib/array/float16}}( 10 ) )
17+
true
18+
> bool = {{alias}}( [] )
19+
false
20+
21+
See Also
22+
--------
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
import Float16Array = require( '@stdlib/array/float16' );
22+
23+
/**
24+
* Tests if a value is a Float16Array.
25+
*
26+
* @param value - value to test
27+
* @returns boolean indicating whether value is a Float16Array
28+
*
29+
* @example
30+
*
31+
* var bool = isFloat16Array( new Float16Array( 10 ) );
32+
* // returns true
33+
*
34+
* @example
35+
* var bool = isFloat16Array( [] );
36+
* // returns false
37+
*/
38+
declare function isFloat16Array( value: any ): value is Float16Array;
39+
40+
// EXPORTS //
41+
42+
export = isFloat16Array;
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) 2019 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 isFloat16Array = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a boolean...
25+
{
26+
isFloat16Array( new Float16Array( 10 ) ); // $ExpectType boolean
27+
isFloat16Array( [] ); // $ExpectType boolean
28+
}
29+
30+
// The compiler throws an error if the function is provided an unsupported number of arguments...
31+
{
32+
isFloat16Array(); // $ExpectError
33+
isFloat16Array( new Float16Array( 10 ), 123 ); // $ExpectError
34+
}

0 commit comments

Comments
 (0)