Skip to content

Commit e6e4501

Browse files
udaykakade25kgrytestdlib-bot
authored
feat: add assert/has-float16array-support
PR-URL: #7347 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent 522af27 commit e6e4501

File tree

15 files changed

+870
-0
lines changed

15 files changed

+870
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
# Float16Array Support
22+
23+
> Detect native [`Float16Array`][mdn-float16array] support.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var hasFloat16ArraySupport = require( '@stdlib/assert/has-float16array-support' );
31+
```
32+
33+
#### hasFloat16ArraySupport()
34+
35+
Detects if a runtime environment supports [`Float16Array`][mdn-float16array].
36+
37+
```javascript
38+
var bool = hasFloat16ArraySupport();
39+
// returns <boolean>
40+
```
41+
42+
</section>
43+
44+
<!-- /.usage -->
45+
46+
<section class="examples">
47+
48+
## Examples
49+
50+
<!-- eslint no-undef: "error" -->
51+
52+
```javascript
53+
var hasFloat16ArraySupport = require( '@stdlib/assert/has-float16array-support' );
54+
55+
var bool = hasFloat16ArraySupport();
56+
if ( bool ) {
57+
console.log( 'Environment has Float16Array support.' );
58+
} else {
59+
console.log( 'Environment lacks Float16Array support.' );
60+
}
61+
```
62+
63+
</section>
64+
65+
<!-- /.examples -->
66+
67+
* * *
68+
69+
<section class="cli">
70+
71+
## CLI
72+
73+
<section class="usage">
74+
75+
### Usage
76+
77+
```text
78+
Usage: has-float16array-support [options]
79+
80+
Options:
81+
82+
-h, --help Print this message.
83+
-V, --version Print the package version.
84+
```
85+
86+
</section>
87+
88+
<!-- /.usage -->
89+
90+
<section class="examples">
91+
92+
### Examples
93+
94+
```bash
95+
$ has-float16array-support
96+
<boolean>
97+
```
98+
99+
</section>
100+
101+
<!-- /.examples -->
102+
103+
</section>
104+
105+
<!-- /.cli -->
106+
107+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
108+
109+
<section class="related">
110+
111+
</section>
112+
113+
<!-- /.related -->
114+
115+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
116+
117+
<section class="links">
118+
119+
[mdn-float16array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float16Array
120+
121+
</section>
122+
123+
<!-- /.links -->
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+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var hasFloat16ArraySupport = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var bool;
33+
var i;
34+
35+
b.tic();
36+
for ( i = 0; i < b.iterations; i++ ) {
37+
// Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear.
38+
bool = hasFloat16ArraySupport();
39+
if ( typeof bool !== 'boolean' ) {
40+
b.fail( 'should return a boolean' );
41+
}
42+
}
43+
b.toc();
44+
if ( !isBoolean( bool ) ) {
45+
b.fail( 'should return a boolean' );
46+
}
47+
b.pass( 'benchmark finished' );
48+
b.end();
49+
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* @license Apache-2.0
5+
*
6+
* Copyright (c) 2025 The Stdlib Authors.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
'use strict';
22+
23+
// MODULES //
24+
25+
var resolve = require( 'path' ).resolve;
26+
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
27+
var CLI = require( '@stdlib/cli/ctor' );
28+
var detect = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
/**
34+
* Main execution sequence.
35+
*
36+
* @private
37+
*/
38+
function main() {
39+
var flags;
40+
var cli;
41+
42+
// Create a command-line interface:
43+
cli = new CLI({
44+
'pkg': require( './../package.json' ),
45+
'options': require( './../etc/cli_opts.json' ),
46+
'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
47+
'encoding': 'utf8'
48+
})
49+
});
50+
51+
// Get any provided command-line options:
52+
flags = cli.flags();
53+
if ( flags.help || flags.version ) {
54+
return;
55+
}
56+
57+
console.log( detect() ); // eslint-disable-line no-console
58+
}
59+
60+
main();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
{{alias}}()
3+
Tests for native `Float16Array` support.
4+
5+
Returns
6+
-------
7+
bool: boolean
8+
Boolean indicating if an environment has `Float16Array` support.
9+
10+
Examples
11+
--------
12+
> var bool = {{alias}}()
13+
<boolean>
14+
15+
See Also
16+
--------
17+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
/**
22+
* Tests for native `Float16Array` support.
23+
*
24+
* @returns boolean indicating if an environment has `Float16Array` support
25+
*
26+
* @example
27+
* var bool = hasFloat16ArraySupport();
28+
* // returns <boolean>
29+
*/
30+
declare function hasFloat16ArraySupport(): boolean;
31+
32+
33+
// EXPORTS //
34+
35+
export = hasFloat16ArraySupport;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 hasFloat16ArraySupport = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a boolean...
25+
{
26+
hasFloat16ArraySupport(); // $ExpectType boolean
27+
}
28+
29+
// The compiler throws an error if the function is provided arguments...
30+
{
31+
hasFloat16ArraySupport( true ); // $ExpectError
32+
hasFloat16ArraySupport( [], 123 ); // $ExpectError
33+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
Usage: has-float16array-support [options]
3+
4+
Options:
5+
6+
-h, --help Print this message.
7+
-V, --version Print the package version.
8+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"boolean": [
3+
"help",
4+
"version"
5+
],
6+
"alias": {
7+
"help": [
8+
"h"
9+
],
10+
"version": [
11+
"V"
12+
]
13+
}
14+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 hasFloat16ArraySupport = require( './../lib' );
22+
23+
var bool = hasFloat16ArraySupport();
24+
if ( bool ) {
25+
console.log( 'Environment has Float16Array support.' );
26+
} else {
27+
console.log( 'Environment lacks Float16Array support.' );
28+
}

0 commit comments

Comments
 (0)