Skip to content

Commit 33eebe7

Browse files
committed
Add utility to detect Uint16Array support
1 parent 4b1fefd commit 33eebe7

File tree

12 files changed

+513
-0
lines changed

12 files changed

+513
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Uint16Array Support
2+
3+
> Detect native [`Uint16Array`][mdn-uint16array] support.
4+
5+
6+
<section class="usage">
7+
8+
## Usage
9+
10+
``` javascript
11+
var hasUint16ArraySupport = require( '@stdlib/utils/detect-uint16array-support' );
12+
```
13+
14+
#### hasUint16ArraySupport()
15+
16+
Detects if a runtime environment supports [`Uint16Array`][mdn-uint16array].
17+
18+
``` javascript
19+
var bool = hasUint16ArraySupport();
20+
// returns <boolean>
21+
```
22+
23+
</section>
24+
25+
<!-- /.usage -->
26+
27+
28+
<section class="examples">
29+
30+
## Examples
31+
32+
``` javascript
33+
var hasUint16ArraySupport = require( '@stdlib/utils/detect-uint16array-support' );
34+
35+
var bool = hasUint16ArraySupport();
36+
if ( bool ) {
37+
console.log( 'Environment has Uint16Array support.' );
38+
} else {
39+
console.log( 'Environment lacks Uint16Array support.' );
40+
}
41+
```
42+
43+
</section>
44+
45+
<!-- /.examples -->
46+
47+
48+
---
49+
50+
<section class="cli">
51+
52+
## CLI
53+
54+
<section class="usage">
55+
56+
### Usage
57+
58+
``` bash
59+
Usage: hasuint16arrays [options]
60+
61+
Options:
62+
63+
-h, --help Print this message.
64+
-V, --version Print the package version.
65+
```
66+
67+
</section>
68+
69+
<!-- /.usage -->
70+
71+
<section class="examples">
72+
73+
### Examples
74+
75+
``` bash
76+
$ hasuint16arrays
77+
<boolean>
78+
```
79+
80+
</section>
81+
82+
<!-- /.examples -->
83+
84+
</section>
85+
86+
<!-- /.cli -->
87+
88+
89+
<section class="links">
90+
91+
[mdn-uint16array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint16Array
92+
93+
</section>
94+
95+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
// MODULES //
4+
5+
var bench = require( '@stdlib/bench' );
6+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
7+
var pkg = require( './../package.json' ).name;
8+
var hasUint16ArraySupport = require( './../lib' );
9+
10+
11+
// MAIN //
12+
13+
bench( pkg, function benchmark( b ) {
14+
var bool;
15+
var i;
16+
17+
b.tic();
18+
for ( i = 0; i < b.iterations; i++ ) {
19+
// Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear.
20+
bool = hasUint16ArraySupport();
21+
if ( !isBoolean( bool ) ) {
22+
b.fail( 'should return a boolean' );
23+
}
24+
}
25+
b.toc();
26+
if ( !isBoolean( bool ) ) {
27+
b.fail( 'should return a boolean' );
28+
}
29+
b.pass( 'benchmark finished' );
30+
b.end();
31+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/env node
2+
'use strict';
3+
4+
// MODULES //
5+
6+
var fs = require( 'fs' );
7+
var path = require( 'path' );
8+
var parseArgs = require( 'minimist' );
9+
var notifier = require( 'update-notifier' );
10+
var pkg = require( './../package.json' );
11+
var opts = require( './opts.json' );
12+
var detect = require( './../lib' );
13+
14+
15+
// FUNCTIONS //
16+
17+
/**
18+
* Performs initialization tasks.
19+
*
20+
* @private
21+
* @example
22+
* init();
23+
*/
24+
function init() {
25+
var opts;
26+
27+
// Check if newer versions exist for this package:
28+
opts = {
29+
'pkg': pkg
30+
};
31+
notifier( opts ).notify();
32+
33+
// Set the process title to allow the process to be more easily identified:
34+
process.title = pkg.name;
35+
process.stdout.on( 'error', process.exit );
36+
} // end FUNCTION init()
37+
38+
/**
39+
* Prints usage information.
40+
*
41+
* @private
42+
* @example
43+
* help();
44+
* // => '...'
45+
*/
46+
function help() {
47+
var fpath = path.join( __dirname, 'usage.txt' );
48+
fs.createReadStream( fpath )
49+
.pipe( process.stdout )
50+
.on( 'close', onClose );
51+
52+
function onClose() {
53+
process.exit( 0 );
54+
}
55+
} // end FUNCTION help()
56+
57+
/**
58+
* Prints the package version.
59+
*
60+
* @private
61+
* @example
62+
* version();
63+
* // => '#.#.#'
64+
*/
65+
function version() {
66+
var msg = pkg.version.toString()+'\n';
67+
process.stdout.write( msg, 'utf8' );
68+
process.exit( 0 );
69+
} // end FUNCTION version()
70+
71+
72+
// VARIABLES //
73+
74+
var args;
75+
76+
77+
// MAIN //
78+
79+
init();
80+
81+
// Parse command-line arguments:
82+
args = parseArgs( process.argv.slice( 2 ), opts );
83+
84+
if ( args.help ) {
85+
return help();
86+
}
87+
if ( args.version ) {
88+
return version();
89+
}
90+
console.log( detect() );
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
Usage: hasuint16arrays [options]
3+
4+
Options:
5+
6+
-h, --help Print this message.
7+
-V, --version Print the package version.
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
{{alias}}()
3+
Tests for native `Uint16Array` support.
4+
5+
Returns
6+
-------
7+
bool: boolean
8+
Boolean indicating if an environment has `Uint16Array` support.
9+
10+
Examples
11+
--------
12+
> var bool = {{alias}}()
13+
<boolean>
14+
15+
See Also
16+
--------
17+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
var hasUint16ArraySupport = require( './../lib' );
4+
5+
var bool = hasUint16ArraySupport();
6+
if ( bool ) {
7+
console.log( 'Environment has Uint16Array support.' );
8+
} else {
9+
console.log( 'Environment lacks Uint16Array support.' );
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict';
2+
3+
// MODULES //
4+
5+
var isUint16Array = require( '@stdlib/assert/is-uint16array' );
6+
var UINT16_MAX = require( '@stdlib/math/constants/uint16-max' );
7+
var GlobalUint16Array = require( './uint16array.js' );
8+
9+
10+
// MAIN //
11+
12+
/**
13+
* Tests for native `Uint16Array` support.
14+
*
15+
* @returns {boolean} boolean indicating if an environment has `Uint16Array` support
16+
*
17+
* @example
18+
* var bool = hasUint16ArraySupport();
19+
* // returns <boolean>
20+
*/
21+
function hasUint16ArraySupport() {
22+
var bool;
23+
var arr;
24+
25+
if ( typeof GlobalUint16Array !== 'function' ) {
26+
return false;
27+
}
28+
// Test basic support...
29+
try {
30+
arr = [ 1, 3.14, -3.14, UINT16_MAX+1, UINT16_MAX+2 ];
31+
arr = new GlobalUint16Array( arr );
32+
bool = (
33+
isUint16Array( arr ) &&
34+
arr[ 0 ] === 1 &&
35+
arr[ 1 ] === 3 && // truncation
36+
arr[ 2 ] === UINT16_MAX-2 && // truncation and wrap around
37+
arr[ 3 ] === 0 && // wrap around
38+
arr[ 4 ] === 1 // wrap around
39+
);
40+
} catch ( err ) { // eslint-disable-line no-unused-vars
41+
bool = false;
42+
}
43+
return bool;
44+
} // end FUNCTION hasUint16ArraySupport()
45+
46+
47+
// EXPORTS //
48+
49+
module.exports = hasUint16ArraySupport;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
/**
4+
* Tests for native `Uint16Array` support.
5+
*
6+
* @module @stdlib/utils/detect-uint16array-support
7+
*
8+
* @example
9+
* var hasUint16ArraySupport = require( '@stdlib/utils/detect-uint16array-support' );
10+
*
11+
* var bool = hasUint16ArraySupport();
12+
* // returns <boolean>
13+
*/
14+
15+
// MODULES //
16+
17+
var hasUint16ArraySupport = require( './detect_uint16array_support.js' );
18+
19+
20+
// EXPORTS //
21+
22+
module.exports = hasUint16ArraySupport;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
// EXPORTS //
4+
5+
module.exports = ( typeof Uint16Array === 'function' ) ? Uint16Array : null;

0 commit comments

Comments
 (0)