Skip to content

Commit d25df9e

Browse files
committed
feat: add array/base/assert/is-complex128array
1 parent d41b87d commit d25df9e

File tree

10 files changed

+730
-0
lines changed

10 files changed

+730
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# isComplex128Array
22+
23+
> Test if a value is a [`Complex128Array`][@stdlib/array/complex128].
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- ./intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var isComplex128Array = require( '@stdlib/array/base/assert/is-complex128array' );
37+
```
38+
39+
#### isComplex128Array( value )
40+
41+
Tests if a value is a [`Complex128Array`][@stdlib/array/complex128].
42+
43+
```javascript
44+
var Complex128Array = require( '@stdlib/array/complex128' );
45+
46+
var arr = new Complex128Array( 10 );
47+
var bool = isComplex128Array( arr );
48+
// returns true
49+
```
50+
51+
</section>
52+
53+
<!-- /.usage -->
54+
55+
<section class="notes">
56+
57+
## Notes
58+
59+
- This function is not robust and that is intentional. This function provides a lower cost way to reasonably determine whether an input value is a [`Complex128Array`][@stdlib/array/complex128] in order to avoid walking the prototype chain and resolving constructors, which is necessary for robust identification of cross-realm instances. For more robust validation, see [`@stdlib/assert/is-complex128array`][@stdlib/assert/is-complex128array].
60+
61+
</section>
62+
63+
<!-- /.notes -->
64+
65+
<section class="examples">
66+
67+
## Examples
68+
69+
<!-- eslint-disable object-curly-newline -->
70+
71+
<!-- eslint no-undef: "error" -->
72+
73+
```javascript
74+
var Int8Array = require( '@stdlib/array/int8' );
75+
var Uint8Array = require( '@stdlib/array/uint8' );
76+
var Uint8ClampedArray = require( '@stdlib/array/uint8c' );
77+
var Int16Array = require( '@stdlib/array/int16' );
78+
var Uint16Array = require( '@stdlib/array/uint16' );
79+
var Int32Array = require( '@stdlib/array/int32' );
80+
var Uint32Array = require( '@stdlib/array/uint32' );
81+
var Float32Array = require( '@stdlib/array/float32' );
82+
var Float64Array = require( '@stdlib/array/float64' );
83+
var Complex128Array = require( '@stdlib/array/complex128' );
84+
var Complex64Array = require( '@stdlib/array/complex64' );
85+
var isComplex128Array = require( '@stdlib/array/base/assert/is-complex128array' );
86+
87+
var bool = isComplex128Array( new Complex128Array( 10 ) );
88+
// returns true
89+
90+
bool = isComplex128Array( new Complex64Array( 10 ) );
91+
// returns false
92+
93+
bool = isComplex128Array( [] );
94+
// returns false
95+
96+
bool = isComplex128Array( new Float64Array( 10 ) );
97+
// returns false
98+
99+
bool = isComplex128Array( new Float32Array( 10 ) );
100+
// returns false
101+
102+
bool = isComplex128Array( new Int32Array( 10 ) );
103+
// returns false
104+
105+
bool = isComplex128Array( new Uint32Array( 10 ) );
106+
// returns false
107+
108+
bool = isComplex128Array( new Int16Array( 10 ) );
109+
// returns false
110+
111+
bool = isComplex128Array( new Uint16Array( 10 ) );
112+
// returns false
113+
114+
bool = isComplex128Array( new Int8Array( 10 ) );
115+
// returns false
116+
117+
bool = isComplex128Array( new Uint8Array( 10 ) );
118+
// returns false
119+
120+
bool = isComplex128Array( new Uint8ClampedArray( 10 ) );
121+
// returns false
122+
123+
bool = isComplex128Array( { 'length': 0 } );
124+
// returns false
125+
```
126+
127+
</section>
128+
129+
<!-- /.examples -->
130+
131+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
132+
133+
<section class="related">
134+
135+
</section>
136+
137+
<!-- /.related -->
138+
139+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
140+
141+
<section class="links">
142+
143+
[@stdlib/array/complex128]: https://github.com/stdlib-js/stdlib
144+
145+
[@stdlib/assert/is-complex128array]: https://github.com/stdlib-js/stdlib
146+
147+
<!-- <related-links> -->
148+
149+
<!-- </related-links> -->
150+
151+
</section>
152+
153+
<!-- /.links -->
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 Float64Array = require( '@stdlib/array/float64' );
25+
var Complex128Array = require( '@stdlib/array/complex128' );
26+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
27+
var pkg = require( './../package.json' ).name;
28+
var isComplex128Array = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg+'::array', function benchmark( b ) {
34+
var bool;
35+
var obj;
36+
var i;
37+
38+
b.tic();
39+
for ( i = 0; i < b.iterations; i++ ) {
40+
obj = [ i, i+1 ];
41+
bool = isComplex128Array( obj );
42+
if ( typeof bool !== 'boolean' ) {
43+
b.fail( 'should return a boolean' );
44+
}
45+
}
46+
b.toc();
47+
if ( !isBoolean( bool ) ) {
48+
b.fail( 'should return a boolean' );
49+
}
50+
b.pass( 'benchmark finished' );
51+
b.end();
52+
});
53+
54+
bench( pkg+'::real_typed_array', function benchmark( b ) {
55+
var values;
56+
var bool;
57+
var obj;
58+
var N;
59+
var i;
60+
61+
values = [
62+
new Float64Array( [ 1.0, 2.0 ] ),
63+
new Float64Array( [ 3.0, 4.0 ] )
64+
];
65+
N = values.length;
66+
67+
b.tic();
68+
for ( i = 0; i < b.iterations; i++ ) {
69+
obj = values[ i%N ];
70+
bool = isComplex128Array( obj );
71+
if ( typeof bool !== 'boolean' ) {
72+
b.fail( 'should return a boolean' );
73+
}
74+
}
75+
b.toc();
76+
if ( !isBoolean( bool ) ) {
77+
b.fail( 'should return a boolean' );
78+
}
79+
b.pass( 'benchmark finished' );
80+
b.end();
81+
});
82+
83+
bench( pkg+'::complex_typed_array', function benchmark( b ) {
84+
var values;
85+
var bool;
86+
var obj;
87+
var N;
88+
var i;
89+
90+
values = [
91+
new Complex128Array( [ 1.0, 2.0 ] ),
92+
new Complex128Array( [ 3.0, 4.0 ] )
93+
];
94+
N = values.length;
95+
96+
b.tic();
97+
for ( i = 0; i < b.iterations; i++ ) {
98+
obj = values[ i%N ];
99+
bool = isComplex128Array( obj );
100+
if ( typeof bool !== 'boolean' ) {
101+
b.fail( 'should return a boolean' );
102+
}
103+
}
104+
b.toc();
105+
if ( !isBoolean( bool ) ) {
106+
b.fail( 'should return a boolean' );
107+
}
108+
b.pass( 'benchmark finished' );
109+
b.end();
110+
});
111+
112+
bench( pkg+'::array_like_object', function benchmark( b ) {
113+
var bool;
114+
var obj;
115+
var i;
116+
117+
b.tic();
118+
for ( i = 0; i < b.iterations; i++ ) {
119+
obj = {
120+
'length': 2,
121+
'0': i,
122+
'1': i + 1
123+
};
124+
bool = isComplex128Array( obj );
125+
if ( typeof bool !== 'boolean' ) {
126+
b.fail( 'should return a boolean' );
127+
}
128+
}
129+
b.toc();
130+
if ( !isBoolean( bool ) ) {
131+
b.fail( 'should return a boolean' );
132+
}
133+
b.pass( 'benchmark finished' );
134+
b.end();
135+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
{{alias}}( value )
3+
Tests if a value is a Complex128Array.
4+
5+
Parameters
6+
----------
7+
value: ArrayLikeObject
8+
Value to test.
9+
10+
Returns
11+
-------
12+
bool: boolean
13+
Boolean indicating whether a value is a Complex128Array.
14+
15+
Examples
16+
--------
17+
> var bool = {{alias}}( new {{alias:@stdlib/array/complex128}}( 10 ) )
18+
true
19+
> bool = {{alias}}( [] )
20+
false
21+
> bool = {{alias}}( { 'length': 0 } )
22+
false
23+
24+
See Also
25+
--------
26+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
/// <reference types="@stdlib/types"/>
22+
23+
import { Collection, Complex128Array } from '@stdlib/types/array';
24+
25+
/**
26+
* Tests if a value is a `Complex128Array`.
27+
*
28+
* @param value - value to test
29+
* @returns boolean indicating whether a value is a `Complex128Array`
30+
*
31+
* @example
32+
* var Complex128Array = require( '@stdlib/array/complex128' );
33+
*
34+
* var arr = new Complex128Array( 10 );
35+
* var bool = isComplex128Array( arr );
36+
* // returns true
37+
*
38+
* @example
39+
* var bool = isComplex128Array( [] );
40+
* // returns false
41+
*/
42+
declare function isComplex128Array( value: Collection | Complex128Array ): value is Complex128Array;
43+
44+
45+
// EXPORTS //
46+
47+
export = isComplex128Array;
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) 2024 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 isComplex128Array = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a boolean...
25+
{
26+
isComplex128Array( [] ); // $ExpectType boolean
27+
}
28+
29+
// The compiler throws an error if the function is provided an unsupported number of arguments...
30+
{
31+
isComplex128Array(); // $ExpectError
32+
isComplex128Array( [], 123 ); // $ExpectError
33+
}

0 commit comments

Comments
 (0)