Skip to content

Commit fd066b2

Browse files
committed
Auto-generated commit
1 parent f64c7bc commit fd066b2

File tree

8 files changed

+318
-3
lines changed

8 files changed

+318
-3
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,6 +1911,23 @@ im = imagf( z );
19111911
// returns 6.0
19121912
```
19131913

1914+
<a name="method-to-string"></a>
1915+
1916+
#### Complex64Array.prototype.toString()
1917+
1918+
Serializes an array as a string.
1919+
1920+
```javascript
1921+
var arr = new Complex64Array( 3 );
1922+
1923+
arr.set( [ 1.0, 1.0 ], 0 );
1924+
arr.set( [ 2.0, -2.0 ], 1 );
1925+
arr.set( [ 3.0, 3.0 ], 2 );
1926+
1927+
var str = arr.toString();
1928+
// returns '1 + 1i,2 - 2i,3 + 3i'
1929+
```
1930+
19141931
</section>
19151932

19161933
<!-- /.usage -->

benchmark/benchmark.to_string.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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-harness' );
24+
var pkg = require( './../package.json' ).name;
25+
var Complex64Array = require( './../lib' );
26+
27+
28+
// MAIN //
29+
30+
bench( pkg+':toString', function benchmark( b ) {
31+
var out;
32+
var arr;
33+
var i;
34+
35+
arr = new Complex64Array( [ 1, 2, 3, 4, 5, 6 ] );
36+
37+
b.tic();
38+
for ( i = 0; i < b.iterations; i++ ) {
39+
out = arr.toString();
40+
if ( typeof out !== 'string' ) {
41+
b.fail( 'should return a string' );
42+
}
43+
}
44+
b.toc();
45+
if ( typeof out !== 'string' ) {
46+
b.fail( 'should return a string' );
47+
}
48+
b.pass( 'benchmark finished' );
49+
b.end();
50+
});
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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-harness' );
24+
var pow = require( '@stdlib/math-base-special-pow' );
25+
var Complex64 = require( '@stdlib/complex-float32' );
26+
var pkg = require( './../package.json' ).name;
27+
var Complex64Array = require( './../lib' );
28+
29+
30+
// FUNCTIONS //
31+
32+
/**
33+
* Creates a benchmark function.
34+
*
35+
* @private
36+
* @param {PositiveInteger} len - array length
37+
* @returns {Function} benchmark function
38+
*/
39+
function createBenchmark( len ) {
40+
var arr;
41+
var i;
42+
43+
arr = [];
44+
for ( i = 0; i < len; i++ ) {
45+
arr.push( new Complex64( i, i ) );
46+
}
47+
arr = new Complex64Array( arr );
48+
49+
return benchmark;
50+
51+
/**
52+
* Benchmark function.
53+
*
54+
* @private
55+
* @param {Benchmark} b - benchmark instance
56+
*/
57+
function benchmark( b ) {
58+
var out;
59+
var i;
60+
61+
b.tic();
62+
for ( i = 0; i < b.iterations; i++ ) {
63+
out = arr.toString();
64+
if ( typeof out !== 'string' ) {
65+
b.fail( 'should return a string' );
66+
}
67+
}
68+
b.toc();
69+
if ( typeof out !== 'string' ) {
70+
b.fail( 'should return a string' );
71+
}
72+
b.pass( 'benchmark finished' );
73+
b.end();
74+
}
75+
}
76+
77+
78+
// MAIN //
79+
80+
/**
81+
* Main execution sequence.
82+
*
83+
* @private
84+
*/
85+
function main() {
86+
var len;
87+
var min;
88+
var max;
89+
var f;
90+
var i;
91+
92+
min = 1; // 10^min
93+
max = 6; // 10^max
94+
95+
for ( i = min; i <= max; i++ ) {
96+
len = pow( 10, i );
97+
f = createBenchmark( len );
98+
bench( pkg+':toString:len='+len, f );
99+
}
100+
}
101+
102+
main();

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/types/index.d.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,22 @@ declare class Complex64Array implements Complex64ArrayInterface {
947947
* // returns -3.0
948948
*/
949949
subarray( begin?: number, end?: number ): Complex64Array;
950+
951+
/**
952+
* Serializes an array as a string.
953+
*
954+
* @returns string
955+
*
956+
* @example
957+
* var arr = new Complex64Array( 2 );
958+
*
959+
* arr.set( [ 1.0, 1.0 ], 0 );
960+
* arr.set( [ 2.0, 2.0 ], 1 );
961+
*
962+
* var str = arr.toString();
963+
* // returns '1 + 1i,2 + 2i'
964+
*/
965+
toString(): string;
950966
}
951967

952968
/**

lib/main.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,6 +2043,39 @@ setReadOnly( Complex64Array.prototype, 'subarray', function subarray( begin, end
20432043
return new this.constructor( buf.buffer, offset, ( len < 0 ) ? 0 : len );
20442044
});
20452045

2046+
/**
2047+
* Serializes an array as a string.
2048+
*
2049+
* @name toString
2050+
* @memberof Complex64Array.prototype
2051+
* @type {Function}
2052+
* @throws {TypeError} `this` must be a complex number array
2053+
* @returns {string} string representation
2054+
*
2055+
* @example
2056+
* var arr = new Complex64Array( 2 );
2057+
*
2058+
* arr.set( [ 1.0, 1.0 ], 0 );
2059+
* arr.set( [ 2.0, 2.0 ], 1 );
2060+
*
2061+
* var str = arr.toString();
2062+
* // returns '1 + 1i,2 + 2i'
2063+
*/
2064+
setReadOnly( Complex64Array.prototype, 'toString', function toString() {
2065+
var out;
2066+
var buf;
2067+
var i;
2068+
if ( !isComplexArray( this ) ) {
2069+
throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
2070+
}
2071+
out = [];
2072+
buf = this._buffer;
2073+
for ( i = 0; i < this._length; i++ ) {
2074+
out.push( getComplex64( buf, i ).toString() );
2075+
}
2076+
return out.join( ',' );
2077+
});
2078+
20462079

20472080
// EXPORTS //
20482081

test/test.to_string.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 tape = require( 'tape' );
24+
var hasOwnProp = require( '@stdlib/assert-has-own-property' );
25+
var isFunction = require( '@stdlib/assert-is-function' );
26+
var Complex64Array = require( './../lib' );
27+
28+
29+
// TESTS //
30+
31+
tape( 'main export is a function', function test( t ) {
32+
t.ok( true, __filename );
33+
t.strictEqual( typeof Complex64Array, 'function', 'main export is a function' );
34+
t.end();
35+
});
36+
37+
tape( 'attached to the prototype of the main export is a `toString` method', function test( t ) {
38+
t.strictEqual( hasOwnProp( Complex64Array.prototype, 'toString' ), true, 'has property' );
39+
t.strictEqual( isFunction( Complex64Array.prototype.toString ), true, 'has method' );
40+
t.end();
41+
});
42+
43+
tape( 'the method throws an error if invoked with a `this` context which is not a complex number array instance', function test( t ) {
44+
var values;
45+
var arr;
46+
var i;
47+
48+
arr = new Complex64Array( 5 );
49+
50+
values = [
51+
'5',
52+
5,
53+
NaN,
54+
true,
55+
false,
56+
null,
57+
void 0,
58+
{},
59+
[],
60+
function noop() {}
61+
];
62+
for ( i = 0; i < values.length; i++ ) {
63+
t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
64+
}
65+
t.end();
66+
67+
function badValue( value ) {
68+
return function badValue() {
69+
return arr.toString.call( value );
70+
};
71+
}
72+
});
73+
74+
tape( 'the method returns an empty string if invoked on an empty array', function test( t ) {
75+
var str;
76+
var arr;
77+
78+
arr = new Complex64Array();
79+
str = arr.toString();
80+
81+
t.strictEqual( str, '', 'returns expected value' );
82+
t.end();
83+
});
84+
85+
tape( 'the method returns a string representation of a complex number array', function test( t ) {
86+
var expected;
87+
var str;
88+
var arr;
89+
90+
arr = new Complex64Array( [ 1, 2, -3, -4 ] );
91+
expected = '1 + 2i,-3 - 4i';
92+
93+
str = arr.toString();
94+
95+
t.strictEqual( str, expected, 'returns expected value' );
96+
t.end();
97+
});

0 commit comments

Comments
 (0)