Skip to content

Commit 1438c86

Browse files
committed
Add assertion utility to test if an object is complex number-like
1 parent aebe080 commit 1438c86

File tree

8 files changed

+514
-0
lines changed

8 files changed

+514
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2018 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+
# isComplexLike
22+
23+
> Test if a value is a complex number-like object.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var isComplexLike = require( '@stdlib/assert/is-complex-like' );
31+
```
32+
33+
#### isComplexLike( value )
34+
35+
Tests if a value is a complex number-like `object`.
36+
37+
```javascript
38+
var Complex128 = require( '@stdlib/complex/float64' );
39+
var Complex64 = require( '@stdlib/complex/float32' );
40+
41+
var x = new Complex128( 1.0, 3.0 );
42+
var bool = isComplexLike( x );
43+
// returns true
44+
45+
x = new Complex64( 3.0, 1.0 );
46+
bool = isComplexLike( x );
47+
// returns true
48+
49+
x = {
50+
're': 1.0,
51+
'im': -1.0
52+
};
53+
bool = isComplexLike( x );
54+
// returns true
55+
```
56+
57+
</section>
58+
59+
<!-- /.usage -->
60+
61+
<section class="notes">
62+
63+
## Notes
64+
65+
- A complex number-like `object` is defined as an `object` having the following properties assigned to numeric values:
66+
67+
- **re**: real component.
68+
- **im**: imaginary component.
69+
70+
</section>
71+
72+
<!-- /.notes -->
73+
74+
<section class="examples">
75+
76+
## Examples
77+
78+
<!-- eslint no-undef: "error" -->
79+
80+
<!-- eslint-disable object-curly-newline, object-property-newline -->
81+
82+
```javascript
83+
var Complex64 = require( '@stdlib/complex/float32' );
84+
var Complex128 = require( '@stdlib/complex/float64' );
85+
var isComplexLike = require( '@stdlib/assert/is-complex-like' );
86+
87+
var out = isComplexLike( new Complex64( 2.0, 2.0 ) );
88+
// returns true
89+
90+
out = isComplexLike( new Complex128( 3.0, 1.0 ) );
91+
// returns true
92+
93+
out = isComplexLike( { 're': 1.0, 'im': -1.0 } );
94+
// returns true
95+
96+
out = isComplexLike( {} );
97+
// returns false
98+
99+
out = isComplexLike( null );
100+
// returns false
101+
```
102+
103+
</section>
104+
105+
<!-- /.examples -->
106+
107+
<section class="links">
108+
109+
</section>
110+
111+
<!-- /.links -->
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 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 Complex64 = require( '@stdlib/complex/float32' );
26+
var Complex128 = require( '@stdlib/complex/float64' );
27+
var pkg = require( './../package.json' ).name;
28+
var isComplexLike = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var values;
35+
var bool;
36+
var z;
37+
var N;
38+
var i;
39+
40+
values = [
41+
new Complex128( 1.0, -1.0 ),
42+
new Complex64( 2.0, -2.0 ),
43+
{
44+
're': 3.0,
45+
'im': -3.0
46+
}
47+
];
48+
N = values.length;
49+
50+
b.tic();
51+
for ( i = 0; i < b.iterations; i++ ) {
52+
z = values[ i%N ];
53+
bool = isComplexLike( z );
54+
if ( typeof bool !== 'boolean' ) {
55+
b.fail( 'should return a boolean' );
56+
}
57+
}
58+
b.toc();
59+
if ( !isBoolean( bool ) ) {
60+
b.fail( 'should return a boolean' );
61+
}
62+
b.pass( 'benchmark finished' );
63+
b.end();
64+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
{{alias}}( value )
3+
Tests if a value is a complex number-like object.
4+
5+
Parameters
6+
----------
7+
value: any
8+
Value to test.
9+
10+
Returns
11+
-------
12+
bool: boolean
13+
Boolean indicating whether a value is a complex number-like object.
14+
15+
Examples
16+
--------
17+
> var bool = {{alias}}( {{alias:@stdlib/complex/float32}}( 2.0, 2.0 ) )
18+
true
19+
> bool = {{alias}}( {{alias:@stdlib/complex/float64}}( 3.0, 1.0 ) )
20+
true
21+
> bool = {{alias}}( 3.14 )
22+
false
23+
> bool = {{alias}}( {} )
24+
false
25+
26+
See Also
27+
--------
28+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 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+
/* eslint-disable object-curly-newline, object-property-newline */
20+
21+
'use strict';
22+
23+
var Complex64 = require( '@stdlib/complex/float32' );
24+
var Complex128 = require( '@stdlib/complex/float64' );
25+
var isComplexLike = require( './../lib' );
26+
27+
console.log( isComplexLike( new Complex64( 2.0, 2.0 ) ) );
28+
// => true
29+
30+
console.log( isComplexLike( new Complex128( 3.0, 1.0 ) ) );
31+
// => true
32+
33+
console.log( isComplexLike( { 're': 1.0, 'im': -1.0 } ) );
34+
// => true
35+
36+
console.log( isComplexLike( {} ) );
37+
// => false
38+
39+
console.log( isComplexLike( null ) );
40+
// => false
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) 2018 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+
/**
22+
* Test if a value is a complex number-like object.
23+
*
24+
* @module @stdlib/assert/is-complex-like
25+
*
26+
* @example
27+
* var Complex128 = require( '@stdlib/complex/float64' );
28+
* var Complex64 = require( '@stdlib/complex/float32' );
29+
* var isComplexLike = require( '@stdlib/assert/is-complex-like' );
30+
*
31+
* var x = new Complex128( 4.0, 2.0 );
32+
* var bool = isComplexLike( x );
33+
* // returns true
34+
*
35+
* x = new Complex64( 4.0, 2.0 );
36+
* bool = isComplexLike( x );
37+
* // returns true
38+
*/
39+
40+
// MODULES //
41+
42+
var isComplexLike = require( './main.js' );
43+
44+
45+
// EXPORTS //
46+
47+
module.exports = isComplexLike;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 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 Complex128 = require( '@stdlib/complex/float64' );
24+
var Complex64 = require( '@stdlib/complex/float32' );
25+
26+
27+
// MAIN //
28+
29+
/**
30+
* Tests if a value is a complex number-like object.
31+
*
32+
* @param {*} value - value to test
33+
* @returns {boolean} boolean indicating if a value is a complex number-like object.
34+
*
35+
* @example
36+
* var Complex128 = require( '@stdlib/complex/float64' );
37+
* var Complex64 = require( '@stdlib/complex/float32' );
38+
*
39+
* var x = new Complex128( 4.0, 2.0 );
40+
* var bool = isComplexLike( x );
41+
* // returns true
42+
*
43+
* x = new Complex64( 4.0, 2.0 );
44+
* bool = isComplexLike( x );
45+
* // returns true
46+
*/
47+
function isComplexLike( value ) {
48+
if ( value instanceof Complex128 || value instanceof Complex64 ) {
49+
return true;
50+
}
51+
return (
52+
typeof value === 'object' &&
53+
value !== null &&
54+
typeof value.re === 'number' &&
55+
typeof value.im === 'number'
56+
);
57+
}
58+
59+
60+
// EXPORTS //
61+
62+
module.exports = isComplexLike;

0 commit comments

Comments
 (0)