Skip to content

Commit e884dfe

Browse files
committed
Add constant for maximum typed array length
1 parent 2c8a55d commit e884dfe

File tree

6 files changed

+253
-0
lines changed

6 files changed

+253
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Maximum Typed Array Length
2+
3+
> Maximum length for a typed array.
4+
5+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
6+
7+
<section class="intro">
8+
9+
</section>
10+
11+
<!-- /.intro -->
12+
13+
<!-- Package usage documentation. -->
14+
15+
<section class="usage">
16+
17+
## Usage
18+
19+
```javascript
20+
var MAX_TYPEDARRAY_LENGTH = require( '@stdlib/array/constants/max-typedarray-length' );
21+
```
22+
23+
#### MAX_TYPEDARRAY_LENGTH
24+
25+
Maximum length for a typed array.
26+
27+
```javascript
28+
var len = MAX_TYPEDARRAY_LENGTH;
29+
// returns 9007199254740991
30+
```
31+
32+
</section>
33+
34+
<!-- /.usage -->
35+
36+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
37+
38+
<section class="notes">
39+
40+
</section>
41+
42+
<!-- /.notes -->
43+
44+
<!-- Package usage examples. -->
45+
46+
<section class="examples">
47+
48+
## Examples
49+
50+
<!-- eslint-disable no-new-cap -->
51+
52+
```javascript
53+
var ctors = require( '@stdlib/array/ctors' );
54+
var MAX_TYPEDARRAY_LENGTH = require( '@stdlib/array/constants/max-typedarray-length' );
55+
56+
function fill( dtype, len, value ) {
57+
var ctor;
58+
var arr;
59+
var i;
60+
if ( len > MAX_TYPEDARRAY_LENGTH ) {
61+
throw new RangeError( 'invalid input argument. The maximum length for a typed array is '+MAX_TYPEDARRAY_LENGTH+'.' );
62+
}
63+
ctor = ctors( dtype );
64+
arr = new ctor( len );
65+
for ( i = 0; i < len; i++ ) {
66+
arr[ i ] = value;
67+
}
68+
return arr;
69+
}
70+
71+
var arr = fill( 'float64', 10, 3.14 );
72+
console.log( arr );
73+
74+
try {
75+
arr = fill( 'float64', 1e300, 3.14 );
76+
} catch ( err ) {
77+
console.error( err.message );
78+
}
79+
```
80+
81+
</section>
82+
83+
<!-- /.examples -->
84+
85+
<!-- 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. -->
86+
87+
<section class="references">
88+
89+
</section>
90+
91+
<!-- /.references -->
92+
93+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
94+
95+
<section class="links">
96+
97+
</section>
98+
99+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
{{alias}}
3+
Maximum length for a typed array.
4+
5+
Examples
6+
--------
7+
> {{alias}}
8+
9007199254740991
9+
10+
See Also
11+
--------
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
var ctors = require( '@stdlib/array/ctors' );
4+
var MAX_TYPEDARRAY_LENGTH = require( './../lib' );
5+
6+
function fill( dtype, len, value ) {
7+
var ctor;
8+
var arr;
9+
var i;
10+
if ( len > MAX_TYPEDARRAY_LENGTH ) {
11+
throw new RangeError( 'invalid input argument. The maximum length for a typed array is '+MAX_TYPEDARRAY_LENGTH+'.' );
12+
}
13+
ctor = ctors( dtype );
14+
arr = new ctor( len ); // eslint-disable-line no-new-cap
15+
for ( i = 0; i < len; i++ ) {
16+
arr[ i ] = value;
17+
}
18+
return arr;
19+
}
20+
21+
var arr = fill( 'float64', 10, 3.14 );
22+
console.log( arr );
23+
24+
try {
25+
arr = fill( 'float64', 1e300, 3.14 );
26+
} catch ( err ) {
27+
console.error( err.message );
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
/**
4+
* Maximum length of a typed array.
5+
*
6+
* @module @stdlib/array/constants/max-typed-array-length
7+
*
8+
* @example
9+
* var MAX_TYPEDARRAY_LENGTH = require( '@stdlib/array/constants/max-typedarray-length' );
10+
* // returns 9007199254740991
11+
*/
12+
13+
// MAIN //
14+
15+
/**
16+
* Maximum length of a typed array.
17+
*
18+
* ```tex
19+
* 2^{53} - 1
20+
* ```
21+
*
22+
* @constant
23+
* @type {uinteger}
24+
* @default 9007199254740991
25+
*/
26+
var MAX_TYPEDARRAY_LENGTH = 9007199254740991;
27+
28+
29+
// EXPORTS //
30+
31+
module.exports = MAX_TYPEDARRAY_LENGTH;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"name": "@stdlib/array/constants/max-typedarray-length",
3+
"version": "0.0.0",
4+
"description": "Maximum length for a typed array.",
5+
"license": "Apache-2.0",
6+
"author": {
7+
"name": "The Stdlib Authors",
8+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
9+
},
10+
"contributors": [
11+
{
12+
"name": "The Stdlib Authors",
13+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
14+
}
15+
],
16+
"main": "./lib",
17+
"directories": {
18+
"doc": "./docs",
19+
"example": "./examples",
20+
"lib": "./lib",
21+
"test": "./test"
22+
},
23+
"scripts": {},
24+
"homepage": "https://github.com/stdlib-js/stdlib",
25+
"repository": {
26+
"type": "git",
27+
"url": "git://github.com/stdlib-js/stdlib.git"
28+
},
29+
"bugs": {
30+
"url": "https://github.com/stdlib-js/stdlib/issues"
31+
},
32+
"dependencies": {},
33+
"devDependencies": {},
34+
"engines": {
35+
"node": ">=0.10.0",
36+
"npm": ">2.7.0"
37+
},
38+
"os": [
39+
"aix",
40+
"darwin",
41+
"freebsd",
42+
"linux",
43+
"macos",
44+
"openbsd",
45+
"sunos",
46+
"win32",
47+
"windows"
48+
],
49+
"keywords": [
50+
"stdlib",
51+
"stdtypes",
52+
"constant",
53+
"const",
54+
"length",
55+
"len",
56+
"typed",
57+
"array",
58+
"typed-array",
59+
"typed array",
60+
"maximum",
61+
"max"
62+
]
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
// MODULES //
4+
5+
var tape = require( 'tape' );
6+
var MAX_TYPEDARRAY_LENGTH = require( './../lib' );
7+
8+
9+
// TESTS //
10+
11+
tape( 'main export is a number', function test( t ) {
12+
t.ok( true, __filename );
13+
t.strictEqual( typeof MAX_TYPEDARRAY_LENGTH, 'number', 'main export is a number' );
14+
t.end();
15+
});
16+
17+
tape( 'the exported value is 9007199254740991', function test( t ) {
18+
t.strictEqual( MAX_TYPEDARRAY_LENGTH, 9007199254740991, 'returns expected value' );
19+
t.end();
20+
});

0 commit comments

Comments
 (0)