From 46ab581247a5511b850fdb07dad990f21bc02eb2 Mon Sep 17 00:00:00 2001 From: Jaysukh-409 Date: Tue, 5 Mar 2024 15:50:23 +0530 Subject: [PATCH 01/14] feat: add values method to array/complex64 --- .../@stdlib/array/complex64/README.md | 42 +++++++ .../complex64/benchmark/benchmark.values.js | 51 ++++++++ .../benchmark/benchmark.values.length.js | 103 +++++++++++++++ .../array/complex64/docs/types/index.d.ts | 38 ++++++ .../@stdlib/array/complex64/lib/main.js | 80 ++++++++++++ .../array/complex64/test/test.values.js | 118 ++++++++++++++++++ 6 files changed, 432 insertions(+) create mode 100644 lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.values.js create mode 100644 lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.values.length.js create mode 100644 lib/node_modules/@stdlib/array/complex64/test/test.values.js diff --git a/lib/node_modules/@stdlib/array/complex64/README.md b/lib/node_modules/@stdlib/array/complex64/README.md index 338d8b40f132..e3b931cb722b 100644 --- a/lib/node_modules/@stdlib/array/complex64/README.md +++ b/lib/node_modules/@stdlib/array/complex64/README.md @@ -2222,6 +2222,48 @@ var str = arr.toString(); // returns '1 + 1i,2 - 2i,3 + 3i' ``` + + +#### Complex128Array.prototype.values() + +Returns a new array iterator object that iterates the value of each item in a typed array. + +```javascript +var realf = require( '@stdlib/complex/realf' ); +var imagf = require( '@stdlib/complex/imagf' ); +var arr = new Complex64Array( 2 ); + +arr.set( [ 1.0, -1.0 ], 0 ); +arr.set( [ 2.0, -2.0 ], 1 ); + +var iter = arr.values(); + +var v = iter.next().value; +// returns + +var re = realf( v ); +// returns 1.0 + +var im = imagf( v ); +// returns -1.0 + +v = iter.next().value; +// returns + +re = realf( v ); +// returns 2.0 + +im = imagf( v ); +// returns -2.0 + +var bool = iter.next().done; +// returns true +``` + +The returned iterator protocol-compliant object has the following property: + +- **next**: function which returns an iterator protocol-compliant object containing the next iterated value (if one exists) assigned to a `value` property and a `done` property having a `boolean` value indicating whether the iterator is finished. + #### Complex128Array.prototype.with( index, value ) diff --git a/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.values.js b/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.values.js new file mode 100644 index 000000000000..a7af1cdb77d4 --- /dev/null +++ b/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.values.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isIteratorLike = require( '@stdlib/assert/is-iterator-like' ); +var pkg = require( './../package.json' ).name; +var Complex64Array = require( './../lib' ); + + +// MAIN // + +bench( pkg+':values', function benchmark( b ) { + var iter; + var arr; + var i; + + arr = new Complex64Array( [ 1.0, -1.0, 2.0, -2.0 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + iter = arr.values(); + if ( typeof iter !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isIteratorLike( iter ) ) { + b.fail( 'should return an iterator protocol-compliant object' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.values.length.js b/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.values.length.js new file mode 100644 index 000000000000..245a6f46e952 --- /dev/null +++ b/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.values.length.js @@ -0,0 +1,103 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var Complex64 = require( '@stdlib/complex/float32' ); +var isIteratorLike = require('@stdlib/assert/is-iterator-like'); +var pkg = require( './../package.json' ).name; +var Complex64Array = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr; + var i; + + arr = []; + for ( i = 0; i < len; i++ ) { + arr.push( new Complex64( i, i ) ); + } + arr = new Complex64Array( arr ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var iter; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + iter = arr.values(); + if ( typeof iter !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isIteratorLike( iter ) ) { + b.fail( 'should return an iterator protocol-compliant object' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':values:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts index 9cd6b7d9eab3..7d68ca39f376 100644 --- a/lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts @@ -1260,6 +1260,44 @@ declare class Complex64Array implements Complex64ArrayInterface { */ toString(): string; + /** + * Returns a new array iterator object that iterates the value of each item in a typed array. + * + * @returns iterator + * + * @example + * var realf = require( '@stdlib/complex/realf' ); + * var imagf = require( '@stdlib/complex/imagf' ); + * var arr = new Complex64Array( 2 ); + * + * arr.set( [ 1.0, -1.0 ], 0 ); + * arr.set( [ 2.0, -2.0 ], 1 ); + * + * var iter = arr.values(); + * + * var v = iter.next().value; + * // returns + * + * var re = realf( v ); + * // returns 1.0 + * + * var im = imagf( v ); + * // returns -1.0 + * + * v = iter.next().value; + * // returns + * + * re = realf( v ); + * // returns 2.0 + * + * im = imagf( v ); + * // returns -2.0 + * + * var bool = iter.next().done; + * // returns true + */ + values(): Iterator; + /** * Returns a new typed array with the element at a provided index replaced with a provided value. * diff --git a/lib/node_modules/@stdlib/array/complex64/lib/main.js b/lib/node_modules/@stdlib/array/complex64/lib/main.js index b0845abb1c4d..b3311954f817 100644 --- a/lib/node_modules/@stdlib/array/complex64/lib/main.js +++ b/lib/node_modules/@stdlib/array/complex64/lib/main.js @@ -2499,6 +2499,86 @@ setReadOnly( Complex64Array.prototype, 'toString', function toString() { return out.join( ',' ); }); +/** +* Returns a new array iterator object that iterates the value of each item in a typed array. +* +* @name values +* @memberof Complex64Array.prototype +* @type {Function} +* @throws {TypeError} `this` must be a complex number array +* @returns {Iterator} iterator +* +* @example +* var realf = require( '@stdlib/complex/realf' ); +* var imagf = require( '@stdlib/complex/imagf' ); +* var arr = new Complex64Array( 2 ); +* +* arr.set( [ 1.0, -1.0 ], 0 ); +* arr.set( [ 2.0, -2.0 ], 1 ); +* +* var iter = arr.values(); +* +* var v = iter.next().value; +* // returns +* +* var re = realf( v ); +* // returns 1.0 +* +* var im = imagf( v ); +* // returns -1.0 +* +* v = iter.next().value; +* // returns +* +* re = realf( v ); +* // returns 2.0 +* +* im = imagf( v ); +* // returns -2.0 +* +* var bool = iter.next().done; +* // returns true +*/ +setReadOnly( Complex64Array.prototype, 'values', function values() { + var iter; + var FLG; + var buf; + var len; + var i; + + if ( !isComplexArray( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a complex number array.' ); + } + buf = this._buffer; + len = this._length; + i = -1; + FLG = true; + iter = {}; + setReadOnly( iter, 'next', next ); + return iter; + + /** + * Returns an iterator protocol-compliant object. + * + * @private + * @returns {Object} iterator protocol-compliant object + */ + function next() { + i += 1; + if ( FLG && i < len ) { + return { + 'value': getComplex64( buf, i ), + 'done': false + }; + } + FLG = false; + return { + 'value': void 0, + 'done': true + }; + } +}); + /** * Returns a new typed array with the element at a provided index replaced with a provided value. * diff --git a/lib/node_modules/@stdlib/array/complex64/test/test.values.js b/lib/node_modules/@stdlib/array/complex64/test/test.values.js new file mode 100644 index 000000000000..a3d64c4a2173 --- /dev/null +++ b/lib/node_modules/@stdlib/array/complex64/test/test.values.js @@ -0,0 +1,118 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var Complex64 = require( '@stdlib/complex/float32' ); +var realf = require( '@stdlib/complex/realf' ); +var imagf = require( '@stdlib/complex/imagf' ); +var Complex64Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Complex64Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is an `values` method for returning boolean indicating whether all elements pass a test', function test( t ) { + t.strictEqual( hasOwnProp( Complex64Array.prototype, 'values' ), true, 'has property' ); + t.strictEqual( isFunction( Complex64Array.prototype.values ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a complex number array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Complex64Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.values.call( value ); + }; + } +}); + +tape( 'the method returns an iterator protocol-compliant object', function test( t ) { + var expected; + var iter; + var arr; + var i; + var r; + var e; + + arr = new Complex64Array( [ 1.0, -1.0, 2.0, -2.0 ] ); + expected = [ + { + 'value': new Complex64( 1.0, -1.0 ), + 'done': false + }, + { + 'value': new Complex64( 2.0, -2.0 ), + 'done': false + }, + { + 'value': void 0, + 'done': true + } + ]; + iter = arr.values(); + + t.strictEqual( typeof iter, 'object', 'returns an object' ); + t.strictEqual( typeof iter.next, 'function', 'has next method' ); + + for ( i = 0; i < expected.length; i++ ) { + r = iter.next(); + e = expected[ i ]; + if ( e.value === void 0 ) { + t.deepEqual( r, e, 'returns expected value' ); + } else { + t.strictEqual( realf( r.value ), realf( e.value ), 'returns expected value' ); + t.strictEqual( imagf( r.value ), imagf( e.value ), 'returns expected value' ); + t.strictEqual( r.done, e.done, 'returns expected value' ); + } + } + + t.end(); +}); From d8ac3d409b238b476fdd1907f341cdc831b0f219 Mon Sep 17 00:00:00 2001 From: Athan Date: Wed, 6 Mar 2024 13:14:50 -0800 Subject: [PATCH 02/14] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/complex64/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/complex64/README.md b/lib/node_modules/@stdlib/array/complex64/README.md index e3b931cb722b..b7ba4ef4ba14 100644 --- a/lib/node_modules/@stdlib/array/complex64/README.md +++ b/lib/node_modules/@stdlib/array/complex64/README.md @@ -2226,7 +2226,7 @@ var str = arr.toString(); #### Complex128Array.prototype.values() -Returns a new array iterator object that iterates the value of each item in a typed array. +Returns an iterator for iterating over each value in a typed array. ```javascript var realf = require( '@stdlib/complex/realf' ); From 2ec8bd0a9924136444449c84dec289bfdb404f99 Mon Sep 17 00:00:00 2001 From: Jaysukh-409 Date: Thu, 7 Mar 2024 17:28:40 +0530 Subject: [PATCH 03/14] feat: updated implementation and test --- .../array/complex64/docs/types/index.d.ts | 4 +- .../@stdlib/array/complex64/lib/main.js | 24 ++++++-- .../array/complex64/test/test.values.js | 57 ++++++++++++++++++- 3 files changed, 78 insertions(+), 7 deletions(-) diff --git a/lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts index 7d68ca39f376..4f8f588c2b05 100644 --- a/lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts @@ -22,7 +22,7 @@ /// -import { Iterator as Iter, IterableIterator } from '@stdlib/types/iter'; +import { Iterator as Iter, IterableIterator, TypedIterator } from '@stdlib/types/iter'; import { ArrayLike, RealOrComplexTypedArray, Complex64Array as Complex64ArrayInterface } from '@stdlib/types/array'; import { ComplexLike, Complex64 } from '@stdlib/types/complex'; import ArrayBuffer = require( '@stdlib/array/buffer' ); @@ -1296,7 +1296,7 @@ declare class Complex64Array implements Complex64ArrayInterface { * var bool = iter.next().done; * // returns true */ - values(): Iterator; + values(): TypedIterator; /** * Returns a new typed array with the element at a provided index replaced with a provided value. diff --git a/lib/node_modules/@stdlib/array/complex64/lib/main.js b/lib/node_modules/@stdlib/array/complex64/lib/main.js index b3311954f817..037c90b0bd5f 100644 --- a/lib/node_modules/@stdlib/array/complex64/lib/main.js +++ b/lib/node_modules/@stdlib/array/complex64/lib/main.js @@ -2541,24 +2541,30 @@ setReadOnly( Complex64Array.prototype, 'toString', function toString() { */ setReadOnly( Complex64Array.prototype, 'values', function values() { var iter; + var self; + var len; var FLG; var buf; - var len; var i; - if ( !isComplexArray( this ) ) { throw new TypeError( 'invalid invocation. `this` is not a complex number array.' ); } + buf = this._buffer; len = this._length; - i = -1; FLG = true; + self = this; + i = -1; iter = {}; setReadOnly( iter, 'next', next ); + + if ( ITERATOR_SYMBOL ) { + setReadOnly( iter, ITERATOR_SYMBOL, factory ); + } return iter; /** - * Returns an iterator protocol-compliant object. + * Returns an iterator protocol-compliant object containing the next iterated value. * * @private * @returns {Object} iterator protocol-compliant object @@ -2577,6 +2583,16 @@ setReadOnly( Complex64Array.prototype, 'values', function values() { 'done': true }; } + + /** + * Returns a new iterator. + * + * @private + * @returns {Iterator} iterator + */ + function factory() { + return self.values(); + } }); /** diff --git a/lib/node_modules/@stdlib/array/complex64/test/test.values.js b/lib/node_modules/@stdlib/array/complex64/test/test.values.js index a3d64c4a2173..f62112853938 100644 --- a/lib/node_modules/@stdlib/array/complex64/test/test.values.js +++ b/lib/node_modules/@stdlib/array/complex64/test/test.values.js @@ -21,11 +21,13 @@ // MODULES // var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); var hasOwnProp = require( '@stdlib/assert/has-own-property' ); var isFunction = require( '@stdlib/assert/is-function' ); var Complex64 = require( '@stdlib/complex/float32' ); var realf = require( '@stdlib/complex/realf' ); var imagf = require( '@stdlib/complex/imagf' ); +var ITERATOR_SYMBOL = require( '@stdlib/symbol/iterator' ); var Complex64Array = require( './../lib' ); @@ -37,7 +39,7 @@ tape( 'main export is a function', function test( t ) { t.end(); }); -tape( 'attached to the prototype of the main export is an `values` method for returning boolean indicating whether all elements pass a test', function test( t ) { +tape( 'attached to the prototype of the main export is an `values` method for returning iterator object that iterates the value of each item in a typed array', function test( t ) { t.strictEqual( hasOwnProp( Complex64Array.prototype, 'values' ), true, 'has property' ); t.strictEqual( isFunction( Complex64Array.prototype.values ), true, 'has method' ); t.end(); @@ -116,3 +118,56 @@ tape( 'the method returns an iterator protocol-compliant object', function test( t.end(); }); + +tape( 'if an environment supports `Symbol.iterator`, the method returns an iterable', function test( t ) { + var Complex64Array; + var arr; + var buf; + var it1; + var it2; + var v1; + var v2; + var i; + + Complex64Array = proxyquire( './../lib/main.js', { + '@stdlib/symbol/iterator': '__ITERATOR_SYMBOL__' + }); + + buf = [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0, 4.0, -4.0 ]; + arr = new Complex64Array( buf ); + + it1 = arr.values(); + t.strictEqual( typeof it1[ '__ITERATOR_SYMBOL__' ], 'function', 'has method' ); + t.strictEqual( it1[ '__ITERATOR_SYMBOL__' ].length, 0, 'has zero arity' ); + + it2 = it1[ '__ITERATOR_SYMBOL__' ](); + t.strictEqual( typeof it2, 'object', 'returns an object' ); + t.strictEqual( typeof it2.next, 'function', 'has `next` method' ); + + for ( i = 0; i < arr.length; i++ ) { + v1 = it1.next().value; + v2 = it2.next().value; + t.strictEqual( realf( v1 ), realf( v2 ), 'returns expected value' ); + t.strictEqual( imagf( v1 ), imagf( v2 ), 'returns expected value' ); + } + t.end(); +}); + +tape( 'if an environment does not support `Symbol.iterator`, the method does not return an "iterable"', function test( t ) { + var Complex64Array; + var arr; + var buf; + var it; + + Complex64Array = proxyquire( './../lib/main.js', { + '@stdlib/symbol/iterator': false + }); + + buf = [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0, 4.0, -4.0 ]; + arr = new Complex64Array( buf ); + + it = arr.values(); + t.strictEqual( it[ ITERATOR_SYMBOL ], void 0, 'does not have property' ); + + t.end(); +}); From 52589d8584c0157845e213772e2e22238df5cb41 Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 12 Mar 2024 15:57:06 -0700 Subject: [PATCH 04/14] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts index 4f8f588c2b05..beaa41b6cb6d 100644 --- a/lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts @@ -1261,7 +1261,7 @@ declare class Complex64Array implements Complex64ArrayInterface { toString(): string; /** - * Returns a new array iterator object that iterates the value of each item in a typed array. + * Returns an iterator for iterating over each value in a typed array. * * @returns iterator * From e25a21a573e90941f80e1e1246bbd58303eb213b Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 12 Mar 2024 15:57:32 -0700 Subject: [PATCH 05/14] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/complex64/lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/complex64/lib/main.js b/lib/node_modules/@stdlib/array/complex64/lib/main.js index 037c90b0bd5f..0c58a14cc316 100644 --- a/lib/node_modules/@stdlib/array/complex64/lib/main.js +++ b/lib/node_modules/@stdlib/array/complex64/lib/main.js @@ -2500,7 +2500,7 @@ setReadOnly( Complex64Array.prototype, 'toString', function toString() { }); /** -* Returns a new array iterator object that iterates the value of each item in a typed array. +* Returns an iterator for iterating over each value in a typed array. * * @name values * @memberof Complex64Array.prototype From bc6c394392ef37f16fb74a6c1c179503298ae4c3 Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 12 Mar 2024 16:07:31 -0700 Subject: [PATCH 06/14] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/complex64/test/test.values.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/array/complex64/test/test.values.js b/lib/node_modules/@stdlib/array/complex64/test/test.values.js index f62112853938..81fb2d3ff9ca 100644 --- a/lib/node_modules/@stdlib/array/complex64/test/test.values.js +++ b/lib/node_modules/@stdlib/array/complex64/test/test.values.js @@ -39,7 +39,7 @@ tape( 'main export is a function', function test( t ) { t.end(); }); -tape( 'attached to the prototype of the main export is an `values` method for returning iterator object that iterates the value of each item in a typed array', function test( t ) { +tape( 'attached to the prototype of the main export is a `values` method', function test( t ) { t.strictEqual( hasOwnProp( Complex64Array.prototype, 'values' ), true, 'has property' ); t.strictEqual( isFunction( Complex64Array.prototype.values ), true, 'has method' ); t.end(); @@ -95,7 +95,6 @@ tape( 'the method returns an iterator protocol-compliant object', function test( 'done': false }, { - 'value': void 0, 'done': true } ]; From e94ea671361664a56584bc9973298f36ac1b92c2 Mon Sep 17 00:00:00 2001 From: Jaysukh-409 Date: Sat, 13 Apr 2024 14:56:13 +0530 Subject: [PATCH 07/14] feat: apply suggestion --- .../@stdlib/array/complex64/lib/main.js | 23 ++++- .../array/complex64/test/test.values.js | 89 ++++++++++++++++++- 2 files changed, 109 insertions(+), 3 deletions(-) diff --git a/lib/node_modules/@stdlib/array/complex64/lib/main.js b/lib/node_modules/@stdlib/array/complex64/lib/main.js index 037c90b0bd5f..4d1c6bd00632 100644 --- a/lib/node_modules/@stdlib/array/complex64/lib/main.js +++ b/lib/node_modules/@stdlib/array/complex64/lib/main.js @@ -2557,6 +2557,7 @@ setReadOnly( Complex64Array.prototype, 'values', function values() { i = -1; iter = {}; setReadOnly( iter, 'next', next ); + setReadOnly( iter, 'return', end ); if ( ITERATOR_SYMBOL ) { setReadOnly( iter, ITERATOR_SYMBOL, factory ); @@ -2577,9 +2578,27 @@ setReadOnly( Complex64Array.prototype, 'values', function values() { 'done': false }; } - FLG = false; return { - 'value': void 0, + 'done': true + }; + } + + /** + * Finishes an iterator. + * + * @private + * @param {*} [value] - value to return + * @returns {Object} iterator protocol-compliant object + */ + function end( value ) { + FLG = true; + if ( arguments.length > 0 ) { + return { + 'value': value, + 'done': true + }; + } + return { 'done': true }; } diff --git a/lib/node_modules/@stdlib/array/complex64/test/test.values.js b/lib/node_modules/@stdlib/array/complex64/test/test.values.js index f62112853938..374b2c513565 100644 --- a/lib/node_modules/@stdlib/array/complex64/test/test.values.js +++ b/lib/node_modules/@stdlib/array/complex64/test/test.values.js @@ -95,7 +95,6 @@ tape( 'the method returns an iterator protocol-compliant object', function test( 'done': false }, { - 'value': void 0, 'done': true } ]; @@ -171,3 +170,91 @@ tape( 'if an environment does not support `Symbol.iterator`, the method does not t.end(); }); + +tape( 'the method returns an iterator which does not iterate over empty arrays', function test( t ) { + var expected; + var arr; + var it; + var i; + var v; + + arr = new Complex64Array(); + expected = [ + { + 'done': true + }, + { + 'done': true + }, + { + 'done': true + } + ]; + + it = arr.values(); + t.strictEqual( it.next.length, 0, 'has zero arity' ); + + for ( i = 0; i < expected.length; i++ ) { + v = it.next(); + t.deepEqual( v, expected[ i ], 'returns expected value' ); + } + t.end(); +}); + +tape( 'the returned iterator has a `return` method for closing an iterator (no argument)', function test( t ) { + var arr; + var it; + var v; + + arr = new Complex64Array( [ 1.0, -1.0, 2.0, -2.0 ] ); + it = arr.values(); + + v = it.next(); + t.strictEqual( realf( v.value ), 1.0, 'returns expected value' ); + t.strictEqual( imagf( v.value ), -1.0, 'returns expected value' ); + t.strictEqual( v.done, false, 'returns expected value' ); + + v = it.next(); + t.strictEqual( realf( v.value ), 2.0, 'returns expected value' ); + t.strictEqual( imagf( v.value ), -2.0, 'returns expected value' ); + t.strictEqual( v.done, false, 'returns expected value' ); + + v = it.return(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the returned iterator has a `return` method for closing an iterator (argument)', function test( t ) { + var arr; + var it; + var v; + + arr = new Complex64Array( [ 1.0, -1.0, 2.0, -2.0 ] ); + it = arr.values(); + + v = it.next(); + t.strictEqual( realf( v.value ), 1.0, 'returns expected value' ); + t.strictEqual( imagf( v.value ), -1.0, 'returns expected value' ); + t.strictEqual( v.done, false, 'returns expected value' ); + + v = it.next(); + t.strictEqual( realf( v.value ), 2.0, 'returns expected value' ); + t.strictEqual( imagf( v.value ), -2.0, 'returns expected value' ); + t.strictEqual( v.done, false, 'returns expected value' ); + + v = it.return( 'beep' ); + t.strictEqual( v.value, 'beep', 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + t.end(); +}); From d0adae6ff7c7e82bed15f8715b7d2babe1f9f070 Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Sun, 14 Apr 2024 02:24:40 -0700 Subject: [PATCH 08/14] bench: update description --- .../@stdlib/array/complex64/benchmark/benchmark.values.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.values.js b/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.values.js index a7af1cdb77d4..e241e0dd6b86 100644 --- a/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.values.js +++ b/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.values.js @@ -28,7 +28,7 @@ var Complex64Array = require( './../lib' ); // MAIN // -bench( pkg+':values', function benchmark( b ) { +bench( pkg+':values:len=2', function benchmark( b ) { var iter; var arr; var i; From 263453af2f29945b6b800bc9b9ed07f0113cc548 Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Sun, 14 Apr 2024 02:31:05 -0700 Subject: [PATCH 09/14] refactor: improve type specificity for `entries` --- lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts index beaa41b6cb6d..88057114d88d 100644 --- a/lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts @@ -459,7 +459,7 @@ declare class Complex64Array implements Complex64ArrayInterface { * var bool = it.next().done; * // returns true */ - entries(): Iterator; + entries(): TypedIterator<[number, Complex64]>; /** * Tests whether all elements in an array pass a test implemented by a predicate function. From 5b9ee13468052163e55169cd504baf10d00c85fb Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Sun, 14 Apr 2024 02:39:46 -0700 Subject: [PATCH 10/14] fix: handle early iterator return --- .../@stdlib/array/complex64/lib/main.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/node_modules/@stdlib/array/complex64/lib/main.js b/lib/node_modules/@stdlib/array/complex64/lib/main.js index 09d7414297ea..dae2617c330a 100644 --- a/lib/node_modules/@stdlib/array/complex64/lib/main.js +++ b/lib/node_modules/@stdlib/array/complex64/lib/main.js @@ -2549,12 +2549,14 @@ setReadOnly( Complex64Array.prototype, 'values', function values() { if ( !isComplexArray( this ) ) { throw new TypeError( 'invalid invocation. `this` is not a complex number array.' ); } - + self = this; buf = this._buffer; len = this._length; - FLG = true; - self = this; + + // Initialize an iteration index: i = -1; + + // Create an iterator protocol-compliant object: iter = {}; setReadOnly( iter, 'next', next ); setReadOnly( iter, 'return', end ); @@ -2572,14 +2574,14 @@ setReadOnly( Complex64Array.prototype, 'values', function values() { */ function next() { i += 1; - if ( FLG && i < len ) { + if ( FLG || i >= len ) { return { - 'value': getComplex64( buf, i ), - 'done': false + 'done': true }; } return { - 'done': true + 'value': getComplex64( buf, i ), + 'done': false }; } @@ -2592,7 +2594,7 @@ setReadOnly( Complex64Array.prototype, 'values', function values() { */ function end( value ) { FLG = true; - if ( arguments.length > 0 ) { + if ( arguments.length ) { return { 'value': value, 'done': true From 20d859c306202a2f86ec3c329f811789afd5671b Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Sun, 14 Apr 2024 02:41:26 -0700 Subject: [PATCH 11/14] refactor: update `entries` to use internal helper function --- .../@stdlib/array/complex64/lib/main.js | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/lib/node_modules/@stdlib/array/complex64/lib/main.js b/lib/node_modules/@stdlib/array/complex64/lib/main.js index dae2617c330a..5f817fe37d4d 100644 --- a/lib/node_modules/@stdlib/array/complex64/lib/main.js +++ b/lib/node_modules/@stdlib/array/complex64/lib/main.js @@ -763,23 +763,21 @@ setReadOnly( Complex64Array.prototype, 'copyWithin', function copyWithin( target * // returns true */ setReadOnly( Complex64Array.prototype, 'entries', function entries() { - var buffer; var self; var iter; var len; + var buf; var FLG; var i; - var j; if ( !isComplexArray( this ) ) { throw new TypeError( 'invalid invocation. `this` is not a complex number array.' ); } self = this; - buffer = this._buffer; + buf = this._buffer; len = this._length; - // Initialize the iteration indices: + // Initialize an iteration index: i = -1; - j = -2; // Create an iterator protocol-compliant object: iter = {}; @@ -798,17 +796,14 @@ setReadOnly( Complex64Array.prototype, 'entries', function entries() { * @returns {Object} iterator protocol-compliant object */ function next() { - var z; i += 1; if ( FLG || i >= len ) { return { 'done': true }; } - j += 2; - z = new Complex64( buffer[ j ], buffer[ j+1 ] ); return { - 'value': [ i, z ], + 'value': [ i, getComplex64( buf, i ) ], 'done': false }; } From 7c05771f2673e3ade92f61bb3567e8d8652fb029 Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Sun, 14 Apr 2024 02:58:09 -0700 Subject: [PATCH 12/14] test: refactor and reorganize test cases --- .../array/complex64/test/test.values.js | 138 ++++++++++-------- 1 file changed, 75 insertions(+), 63 deletions(-) diff --git a/lib/node_modules/@stdlib/array/complex64/test/test.values.js b/lib/node_modules/@stdlib/array/complex64/test/test.values.js index d3c374d90751..993787e5c1a3 100644 --- a/lib/node_modules/@stdlib/array/complex64/test/test.values.js +++ b/lib/node_modules/@stdlib/array/complex64/test/test.values.js @@ -24,6 +24,7 @@ var tape = require( 'tape' ); var proxyquire = require( 'proxyquire' ); var hasOwnProp = require( '@stdlib/assert/has-own-property' ); var isFunction = require( '@stdlib/assert/is-function' ); +var isComplex64 = require( '@stdlib/assert/is-complex64' ); var Complex64 = require( '@stdlib/complex/float32' ); var realf = require( '@stdlib/complex/realf' ); var imagf = require( '@stdlib/complex/imagf' ); @@ -78,8 +79,8 @@ tape( 'the method throws an error if invoked with a `this` context which is not tape( 'the method returns an iterator protocol-compliant object', function test( t ) { var expected; - var iter; var arr; + var it; var i; var r; var e; @@ -98,17 +99,18 @@ tape( 'the method returns an iterator protocol-compliant object', function test( 'done': true } ]; - iter = arr.values(); + it = arr.values(); - t.strictEqual( typeof iter, 'object', 'returns an object' ); - t.strictEqual( typeof iter.next, 'function', 'has next method' ); + t.strictEqual( typeof it, 'object', 'returns an object' ); + t.strictEqual( typeof it.next, 'function', 'has next method' ); for ( i = 0; i < expected.length; i++ ) { - r = iter.next(); + r = it.next(); e = expected[ i ]; if ( e.value === void 0 ) { t.deepEqual( r, e, 'returns expected value' ); } else { + t.strictEqual( isComplex64( r.value ), true, 'returns expected value' ); t.strictEqual( realf( r.value ), realf( e.value ), 'returns expected value' ); t.strictEqual( imagf( r.value ), imagf( e.value ), 'returns expected value' ); t.strictEqual( r.done, e.done, 'returns expected value' ); @@ -118,59 +120,6 @@ tape( 'the method returns an iterator protocol-compliant object', function test( t.end(); }); -tape( 'if an environment supports `Symbol.iterator`, the method returns an iterable', function test( t ) { - var Complex64Array; - var arr; - var buf; - var it1; - var it2; - var v1; - var v2; - var i; - - Complex64Array = proxyquire( './../lib/main.js', { - '@stdlib/symbol/iterator': '__ITERATOR_SYMBOL__' - }); - - buf = [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0, 4.0, -4.0 ]; - arr = new Complex64Array( buf ); - - it1 = arr.values(); - t.strictEqual( typeof it1[ '__ITERATOR_SYMBOL__' ], 'function', 'has method' ); - t.strictEqual( it1[ '__ITERATOR_SYMBOL__' ].length, 0, 'has zero arity' ); - - it2 = it1[ '__ITERATOR_SYMBOL__' ](); - t.strictEqual( typeof it2, 'object', 'returns an object' ); - t.strictEqual( typeof it2.next, 'function', 'has `next` method' ); - - for ( i = 0; i < arr.length; i++ ) { - v1 = it1.next().value; - v2 = it2.next().value; - t.strictEqual( realf( v1 ), realf( v2 ), 'returns expected value' ); - t.strictEqual( imagf( v1 ), imagf( v2 ), 'returns expected value' ); - } - t.end(); -}); - -tape( 'if an environment does not support `Symbol.iterator`, the method does not return an "iterable"', function test( t ) { - var Complex64Array; - var arr; - var buf; - var it; - - Complex64Array = proxyquire( './../lib/main.js', { - '@stdlib/symbol/iterator': false - }); - - buf = [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0, 4.0, -4.0 ]; - arr = new Complex64Array( buf ); - - it = arr.values(); - t.strictEqual( it[ ITERATOR_SYMBOL ], void 0, 'does not have property' ); - - t.end(); -}); - tape( 'the method returns an iterator which does not iterate over empty arrays', function test( t ) { var expected; var arr; @@ -178,7 +127,7 @@ tape( 'the method returns an iterator which does not iterate over empty arrays', var i; var v; - arr = new Complex64Array(); + arr = new Complex64Array( [] ); expected = [ { 'done': true @@ -190,9 +139,10 @@ tape( 'the method returns an iterator which does not iterate over empty arrays', 'done': true } ]; - it = arr.values(); - t.strictEqual( it.next.length, 0, 'has zero arity' ); + + t.strictEqual( typeof it, 'object', 'returns an object' ); + t.strictEqual( typeof it.next, 'function', 'has next method' ); for ( i = 0; i < expected.length; i++ ) { v = it.next(); @@ -206,7 +156,7 @@ tape( 'the returned iterator has a `return` method for closing an iterator (no a var it; var v; - arr = new Complex64Array( [ 1.0, -1.0, 2.0, -2.0 ] ); + arr = new Complex64Array( [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0 ] ); it = arr.values(); v = it.next(); @@ -227,6 +177,10 @@ tape( 'the returned iterator has a `return` method for closing an iterator (no a t.strictEqual( v.value, void 0, 'returns expected value' ); t.strictEqual( v.done, true, 'returns expected value' ); + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + t.end(); }); @@ -235,7 +189,7 @@ tape( 'the returned iterator has a `return` method for closing an iterator (argu var it; var v; - arr = new Complex64Array( [ 1.0, -1.0, 2.0, -2.0 ] ); + arr = new Complex64Array( [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0 ] ); it = arr.values(); v = it.next(); @@ -256,5 +210,63 @@ tape( 'the returned iterator has a `return` method for closing an iterator (argu t.strictEqual( v.value, void 0, 'returns expected value' ); t.strictEqual( v.done, true, 'returns expected value' ); + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if an environment supports `Symbol.iterator`, the method returns an iterable', function test( t ) { + var Complex64Array; + var arr; + var buf; + var it1; + var it2; + var v1; + var v2; + var i; + + Complex64Array = proxyquire( './../lib/main.js', { + '@stdlib/symbol/iterator': '__ITERATOR_SYMBOL__' + }); + + buf = [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0, 4.0, -4.0 ]; + arr = new Complex64Array( buf ); + + it1 = arr.values(); + t.strictEqual( typeof it1[ '__ITERATOR_SYMBOL__' ], 'function', 'has method' ); + t.strictEqual( it1[ '__ITERATOR_SYMBOL__' ].length, 0, 'has zero arity' ); + + it2 = it1[ '__ITERATOR_SYMBOL__' ](); + t.strictEqual( typeof it2, 'object', 'returns an object' ); + t.strictEqual( typeof it2.next, 'function', 'has `next` method' ); + t.strictEqual( typeof it2.return, 'function', 'has `return` method' ); + + for ( i = 0; i < arr.length; i++ ) { + v1 = it1.next().value; + v2 = it2.next().value; + t.strictEqual( realf( v1 ), realf( v2 ), 'returns expected value' ); + t.strictEqual( imagf( v1 ), imagf( v2 ), 'returns expected value' ); + } + t.end(); +}); + +tape( 'if an environment does not support `Symbol.iterator`, the method does not return an "iterable"', function test( t ) { + var Complex64Array; + var arr; + var buf; + var it; + + Complex64Array = proxyquire( './../lib/main.js', { + '@stdlib/symbol/iterator': false + }); + + buf = [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0, 4.0, -4.0 ]; + arr = new Complex64Array( buf ); + + it = arr.values(); + t.strictEqual( it[ ITERATOR_SYMBOL ], void 0, 'does not have property' ); + t.end(); }); From 4c7e4412de419c344cd1f371ce53635217b963ac Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Sun, 14 Apr 2024 03:03:27 -0700 Subject: [PATCH 13/14] docs: document property and update copy for `entries` --- lib/node_modules/@stdlib/array/complex64/README.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/array/complex64/README.md b/lib/node_modules/@stdlib/array/complex64/README.md index b7ba4ef4ba14..77555753572d 100644 --- a/lib/node_modules/@stdlib/array/complex64/README.md +++ b/lib/node_modules/@stdlib/array/complex64/README.md @@ -798,6 +798,11 @@ var bool = it.next().done; // returns true ``` +The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties: + +- **next**: function which returns an [iterator][mdn-iterator-protocol] protocol-compliant object containing the next iterated value (if one exists) assigned to a `value` property and a `done` property having a `boolean` value indicating whether the [iterator][mdn-iterator-protocol] is finished. +- **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object. + #### Complex64Array.prototype.every( predicate\[, thisArg] ) @@ -2260,9 +2265,10 @@ var bool = iter.next().done; // returns true ``` -The returned iterator protocol-compliant object has the following property: +The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties: -- **next**: function which returns an iterator protocol-compliant object containing the next iterated value (if one exists) assigned to a `value` property and a `done` property having a `boolean` value indicating whether the iterator is finished. +- **next**: function which returns an [iterator][mdn-iterator-protocol] protocol-compliant object containing the next iterated value (if one exists) assigned to a `value` property and a `done` property having a `boolean` value indicating whether the [iterator][mdn-iterator-protocol] is finished. +- **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object. @@ -2394,6 +2400,8 @@ logEach( '%s', out );