From b90284398beb4ba9b6df7e492a736e06af28a8c2 Mon Sep 17 00:00:00 2001 From: vinayak Date: Sat, 24 Feb 2024 21:06:30 +0530 Subject: [PATCH 1/4] feat(lib/node_modules/@stdlib/iter/cartesian-square): added iterCartesianSquare --- .../@stdlib/iter/cartesian-square/README.md | 133 ++++++++++++++++++ .../cartesian-square/benchmark/benchmark.js | 59 ++++++++ .../iter/cartesian-square/examples/index.js | 35 +++++ .../iter/cartesian-square/lib/index.js | 45 ++++++ .../@stdlib/iter/cartesian-square/lib/main.js | 115 +++++++++++++++ .../iter/cartesian-square/package.json | 70 +++++++++ .../iter/cartesian-square/test/test.js | 91 ++++++++++++ 7 files changed, 548 insertions(+) create mode 100644 lib/node_modules/@stdlib/iter/cartesian-square/README.md create mode 100644 lib/node_modules/@stdlib/iter/cartesian-square/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/iter/cartesian-square/examples/index.js create mode 100644 lib/node_modules/@stdlib/iter/cartesian-square/lib/index.js create mode 100644 lib/node_modules/@stdlib/iter/cartesian-square/lib/main.js create mode 100644 lib/node_modules/@stdlib/iter/cartesian-square/package.json create mode 100644 lib/node_modules/@stdlib/iter/cartesian-square/test/test.js diff --git a/lib/node_modules/@stdlib/iter/cartesian-square/README.md b/lib/node_modules/@stdlib/iter/cartesian-square/README.md new file mode 100644 index 000000000000..3eb33acb9d12 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-square/README.md @@ -0,0 +1,133 @@ + + +# iterCartesianSquare + +> Create an iterator which generates the Cartesian square of an input array-like object. + +
+ +
+ +
+ +## Usage + +```javascript +var iterCartesianSquare = require( '@stdlib/iter/cartesian-square' ); +``` + +#### iterCartesianSquare( x ) + +Returns an iterator which generates the Cartesian Square of an input array-like object. + +```javascript +var x = ['a', 'b', 'c']; +var pair; +var cartesianSquare = iterCartesianSquare( x, n ); + +for ( pair of cartesianSquare ) { + console.log( pair ); +} + +``` + +
+ + + + + +
+ +## Notes + +The function expects only one argument x, an array-like object. +The returned iterator will generate all possible combinations from the input array x. + +
+ + + + + +
+ +## Examples + + + + +```javascript +// Example : Generating Cartesian square of an array +var x = [1, 2, 3]; +var cartesianSquare = iterCartesianSquare( x ); +var pair; + +for ( pair of cartesianSquare ) { + console.log( pair ); +} + +// Output: +// [ 1, 1 ] +// [ 1, 2 ] +// [ 1, 3 ] +// [ 2, 1 ] +// [ 2, 2 ] +// [ 2, 3 ] +// [ 3, 1 ] +// [ 3, 2 ] +// [ 3, 3 ] + +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/iter/cartesian-square/benchmark/benchmark.js b/lib/node_modules/@stdlib/iter/cartesian-square/benchmark/benchmark.js new file mode 100644 index 000000000000..86b7a1f37c0b --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-square/benchmark/benchmark.js @@ -0,0 +1,59 @@ + +/** +* @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 pkg = require('./../package.json').name; +var iterCartesianSquare = require('./../lib'); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var iterator; + var arr = ['a', 'b', 'c']; + var v; + var i; + + // Run the benchmark... + b.tic(); + for (i = 0; i < b.iterations; i++) { + // Reset the iterator: + iterator = iterCartesianSquare(arr); + v = iterator.next(); + + // Iterate over the Cartesian square: + while (!v.done) { + if (v.done) { + b.fail('should iterate over all elements'); + } + v = iterator.next(); + } + } + b.toc(); + if (v.done) { + b.pass('benchmark finished'); + } else { + b.fail('should have finished iteration'); + } + b.end(); +}); diff --git a/lib/node_modules/@stdlib/iter/cartesian-square/examples/index.js b/lib/node_modules/@stdlib/iter/cartesian-square/examples/index.js new file mode 100644 index 000000000000..a1aff130c192 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-square/examples/index.js @@ -0,0 +1,35 @@ + +/** +* @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'; + +var iterCartesianSquare = require( './../lib' ); + +// Create an array: +var arr = [ 'a', 'b', 'c' ]; + +// Create an iterator: +var iter = iterCartesianSquare( arr ); + +// Iterate over the Cartesian square: +var v = iter.next(); +while ( !v.done ) { + console.log( v.value ); + v = iter.next(); +} diff --git a/lib/node_modules/@stdlib/iter/cartesian-square/lib/index.js b/lib/node_modules/@stdlib/iter/cartesian-square/lib/index.js new file mode 100644 index 000000000000..db3130789d0a --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-square/lib/index.js @@ -0,0 +1,45 @@ +/** +* @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'; + +/** +* Create an iterator which returns Cartesian square. +* +* @module @stdlib/iter/cartesian-square +* +* @example +* var iterCartesianSquare = require( '@stdlib/iter/cartesian-square' ); +* +* var inputArray = [1, 2, 3]; +* var iterator = iterCartesianSquare( inputArray ); +* +* var result; +* while (!(result = iterator.next()).done) { +* console.log(result.value); +* } +*/ + +// MODULES // + +var main = require('./main.js'); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/iter/cartesian-square/lib/main.js b/lib/node_modules/@stdlib/iter/cartesian-square/lib/main.js new file mode 100644 index 000000000000..f7ef51fefacd --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-square/lib/main.js @@ -0,0 +1,115 @@ +/** +* @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 setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var iteratorSymbol = require( '@stdlib/symbol/iterator' ); +var floor = require( '@stdlib/math/base/special/floor' ); + + +// MAIN // + +/** +* Returns an iterator which generates the Cartesian square of an input array-like object. +* +* @param {ArrayLike} arr - input array +* @throws {TypeError} must provide an array-like object +* @returns {Iterator} iterator +* +* @example +* var iter = iterCartesianSquare( [ 'a', 'b', 'c' ] ); +* +* var v = iter.next().value; +* // returns [ 'a', 'a' ] +* +* v = iter.next().value; +* // returns [ 'a', 'b' ] +* +* // ... +*/ +function iterCartesianSquare( arr ) { + var iter; + var FLG; + var i; + + if ( !Array.isArray( arr ) ) { + throw new TypeError( 'invalid argument. Must provide an array-like object. Value: `' + arr + '`.' ); + } + iter = {}; + FLG = false; + i = 0; + setReadOnly( iter, 'next', next ); + setReadOnly( iter, 'return', finish ); + + if ( iteratorSymbol ) { + setReadOnly( iter, iteratorSymbol, factory ); + } + return iter; + + /** + * Returns an iterator protocol-compliant object containing the next iterated value. +* @private +* @returns {Object} iterator result +*/ + function next() { + var v; + if ( FLG ) { + return { + 'done': true + }; + } + v = [ arr[ floor( i / arr.length ) ], arr[ i % arr.length ] ]; + i += 1; + if ( i >= arr.length * arr.length ) { + FLG = true; + } + return { + 'value': v, + 'done': false + }; + } + + /** + * Finishes an iterator. +* @private +* @returns {Object} iterator result +*/ + function finish() { + FLG = true; + return { + 'done': true + }; + } + + /** + * Returns a new iterator. +* @private +* @returns {Iterator} iterator +*/ + function factory() { + return iterCartesianSquare( arr ); + } +} + + +// EXPORTS // + +module.exports = iterCartesianSquare; diff --git a/lib/node_modules/@stdlib/iter/cartesian-square/package.json b/lib/node_modules/@stdlib/iter/cartesian-square/package.json new file mode 100644 index 000000000000..22067bf53683 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-square/package.json @@ -0,0 +1,70 @@ +{ + "name": "@stdlib/iter/cartesian-square", + "version": "0.0.0", + "description": "Create an iterator which generates the Cartesian square of an input array-like object.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "linspace", + "linear", + "sequence", + "monotonic", + "arange", + "range", + "iterator", + "iterate", + "iteration", + "iter" + ] +} \ No newline at end of file diff --git a/lib/node_modules/@stdlib/iter/cartesian-square/test/test.js b/lib/node_modules/@stdlib/iter/cartesian-square/test/test.js new file mode 100644 index 000000000000..76e681cbf5a0 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-square/test/test.js @@ -0,0 +1,91 @@ +/** +* @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 iterCartesianSquare = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof iterCartesianSquare, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if the first argument is not an array-like object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + true, + false, + null, + undefined, + {}, + function namedFunction() {}, + NaN, + Infinity, + -Infinity + ]; + + function badValue(value) { + return function thrower() { + iterCartesianSquare(value); + }; + } + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue(values[i]), TypeError, 'throws an error when provided ' + values[i] ); + } + t.end(); +}); + +tape( 'the function returns an iterator which generates the Cartesian square of an input array', function test( t ) { + var expected; + var actual; + var iter; + + iter = iterCartesianSquare( [ 'a', 'b' ] ); + + expected = [ 'a', 'a' ]; + actual = iter.next().value; + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 'a', 'b' ]; + actual = iter.next().value; + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 'b', 'a' ]; + actual = iter.next().value; + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 'b', 'b' ]; + actual = iter.next().value; + t.deepEqual( actual, expected, 'returns expected value' ); + + t.ok( iter.next().done, 'returns expected value' ); + + t.end(); +}); From deb1b07f745c3ad2d67cbbc61be390aa6a6cf242 Mon Sep 17 00:00:00 2001 From: vinayak Date: Sun, 25 Feb 2024 10:07:49 +0530 Subject: [PATCH 2/4] feat(/lib/node_modules/@stdlib/iter/cartesian-square): added docs folder Signed-off-by: vinayak --- .../iter/cartesian-square/docs/repl.txt | 33 ++++++++++ .../cartesian-square/docs/types/index.d.ts | 60 +++++++++++++++++++ .../iter/cartesian-square/docs/types/test.ts | 43 +++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 lib/node_modules/@stdlib/iter/cartesian-square/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/iter/cartesian-square/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/iter/cartesian-square/docs/types/test.ts diff --git a/lib/node_modules/@stdlib/iter/cartesian-square/docs/repl.txt b/lib/node_modules/@stdlib/iter/cartesian-square/docs/repl.txt new file mode 100644 index 000000000000..b7df89050c12 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-square/docs/repl.txt @@ -0,0 +1,33 @@ +{{alias}}( x ) + Returns an iterator which generates the Cartesian square of an input + array-like object. If an environment supports Symbol.iterator, the returned + iterator is iterable. + + Parameters + ---------- + x : collection + Input array-like object. + + Returns + ------- + iterator: Object + Iterator. + + iterator.next(): Function + Returns an iterator protocol-compliant object containing the next + iterated value (if one exists) and a boolean flag indicating whether the + iterator is finished. + + iterator.return( [value] ): Function + Finishes an iterator and returns a provided value. + + Examples + -------- + > var it = {{alias}}( [ '1', '2', '3' ] ) + > var v = it.next().value + [ '1', '1' ] + > v = it.next().value + [ '1', '2' ] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/iter/cartesian-square/docs/types/index.d.ts b/lib/node_modules/@stdlib/iter/cartesian-square/docs/types/index.d.ts new file mode 100644 index 000000000000..3d940d8b46bc --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-square/docs/types/index.d.ts @@ -0,0 +1,60 @@ +/* +* @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. +*/ + +// TypeScript Version: 4.1 + +/// + +import { Iterator as Iter, IterableIterator } from '@stdlib/types/iter'; +import { ArrayLike } from '@stdlib/types/array'; + +// Define a union type representing both iterable and non-iterable iterators: +type Iterator = Iter | IterableIterator; + +/** +* Returns an iterator which generates the Cartesian square of an input array-like object. +* +* @param x - Input array-like object. +* @throws {TypeError} first argument must be an array-like object +* @returns iterator +* +* @example +* var iterCartesianSquare = require( '@stdlib/iter/cartesian-square' ); +* +* var iterator = iterCartesianSquare( [ 'a', 'b', 'c' ] ); +* // returns +* +* var v = iterator.next().value; +* // returns [ 'a', 'a' ] +* +* v = iterator.next().value; +* // returns [ 'a', 'b' ] +* +* v = iterator.next().value; +* // returns [ 'a', 'c' ] +* +* // ... +* +* var bool = iterator.next().done; +* // returns true +*/ +declare function iterCartesianSquare( x: ArrayLike | Iterable ): Iterator; + +// EXPORTS // + +export = iterCartesianSquare; diff --git a/lib/node_modules/@stdlib/iter/cartesian-square/docs/types/test.ts b/lib/node_modules/@stdlib/iter/cartesian-square/docs/types/test.ts new file mode 100644 index 000000000000..690a8d7fd55e --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-square/docs/types/test.ts @@ -0,0 +1,43 @@ +/* +* @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. +*/ + +import iterCartesianSquare = require( './index' ); + +// TESTS // + +// The function returns an iterator... +{ + iterCartesianSquare( [ 'a', 'b', 'c' ] ); // $ExpectType Iterator + iterCartesianSquare( [ 1, 2, 3 ] ); // $ExpectType Iterator +} + +// The compiler throws an error if the function is provided an argument which is not an array-like object... +{ + iterCartesianSquare( 5 ); // $ExpectError + iterCartesianSquare( true ); // $ExpectError + iterCartesianSquare( false ); // $ExpectError + iterCartesianSquare( null ); // $ExpectError + iterCartesianSquare( {} ); // $ExpectError + iterCartesianSquare( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided insufficient arguments... +{ + iterCartesianSquare(); // $ExpectError +} + From e0c038c9f79b2206c9fde1fe0f8d33b82b6f3865 Mon Sep 17 00:00:00 2001 From: vinayak Date: Sat, 2 Mar 2024 16:12:33 +0530 Subject: [PATCH 3/4] fix(lib/node_modules/@stdlib/iter/cartesian-square): resolved issues with the package Signed-off-by: vinayak --- .../@stdlib/iter/cartesian-square/README.md | 32 ++++++++------ .../cartesian-square/benchmark/benchmark.js | 32 +++++++------- .../iter/cartesian-square/docs/repl.txt | 8 ++-- .../cartesian-square/docs/types/index.d.ts | 3 +- .../@stdlib/iter/cartesian-square/lib/main.js | 35 +++++++++------ .../iter/cartesian-square/test/test.js | 44 ++++++++++++++++++- 6 files changed, 107 insertions(+), 47 deletions(-) diff --git a/lib/node_modules/@stdlib/iter/cartesian-square/README.md b/lib/node_modules/@stdlib/iter/cartesian-square/README.md index 3eb33acb9d12..72e921748cd2 100644 --- a/lib/node_modules/@stdlib/iter/cartesian-square/README.md +++ b/lib/node_modules/@stdlib/iter/cartesian-square/README.md @@ -39,13 +39,22 @@ var iterCartesianSquare = require( '@stdlib/iter/cartesian-square' ); Returns an iterator which generates the Cartesian Square of an input array-like object. ```javascript -var x = ['a', 'b', 'c']; -var pair; -var cartesianSquare = iterCartesianSquare( x, n ); +var it = iterCartesianSquare( [ 1, 2, 3 ] ); -for ( pair of cartesianSquare ) { - console.log( pair ); -} +var v = it.next().value; +// returns [1, 1] + +v = it.next().value; +// returns [1, 2] + +v = it.next().value; +// returns [1, 3] + +v = it.next().value; +// returns [2, 1] + +var bool = it.next().done; +// returns false ``` @@ -57,11 +66,6 @@ for ( pair of cartesianSquare ) {
-## Notes - -The function expects only one argument x, an array-like object. -The returned iterator will generate all possible combinations from the input array x. -
@@ -81,8 +85,10 @@ var x = [1, 2, 3]; var cartesianSquare = iterCartesianSquare( x ); var pair; -for ( pair of cartesianSquare ) { - console.log( pair ); +pair = cartesianSquare.next(); +while ( !pair.done ) { + console.log( pair.value ); + pair = cartesianSquare.next(); } // Output: diff --git a/lib/node_modules/@stdlib/iter/cartesian-square/benchmark/benchmark.js b/lib/node_modules/@stdlib/iter/cartesian-square/benchmark/benchmark.js index 86b7a1f37c0b..92d3a45e6a3a 100644 --- a/lib/node_modules/@stdlib/iter/cartesian-square/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/iter/cartesian-square/benchmark/benchmark.js @@ -22,6 +22,8 @@ // MODULES // var bench = require('@stdlib/bench'); +var ceil = require('@stdlib/math/base/special/ceil'); +var sqrt = require('@stdlib/math/base/special/sqrt'); var pkg = require('./../package.json').name; var iterCartesianSquare = require('./../lib'); @@ -30,30 +32,30 @@ var iterCartesianSquare = require('./../lib'); bench( pkg, function benchmark( b ) { var iterator; - var arr = ['a', 'b', 'c']; + var arr; var v; var i; - // Run the benchmark... + // Create an array with one more element than the square root of b.iterations: + arr = new Array( ceil(sqrt(b.iterations)) + 1 ).fill( 0 ); + + // Create the iterator: + iterator = iterCartesianSquare( arr ); + b.tic(); - for (i = 0; i < b.iterations; i++) { - // Reset the iterator: - iterator = iterCartesianSquare(arr); + for ( i = 0; i < b.iterations; i++ ) { v = iterator.next(); - - // Iterate over the Cartesian square: - while (!v.done) { - if (v.done) { - b.fail('should iterate over all elements'); - } - v = iterator.next(); + if ( v.done ) { + b.fail( 'should not exhaust the iterator' ); + break; } } b.toc(); - if (v.done) { - b.pass('benchmark finished'); + + if ( v.done ) { + b.fail('should not exhaust the iterator'); } else { - b.fail('should have finished iteration'); + b.pass('benchmark finished'); } b.end(); }); diff --git a/lib/node_modules/@stdlib/iter/cartesian-square/docs/repl.txt b/lib/node_modules/@stdlib/iter/cartesian-square/docs/repl.txt index b7df89050c12..9ff1b460b088 100644 --- a/lib/node_modules/@stdlib/iter/cartesian-square/docs/repl.txt +++ b/lib/node_modules/@stdlib/iter/cartesian-square/docs/repl.txt @@ -1,11 +1,13 @@ {{alias}}( x ) Returns an iterator which generates the Cartesian square of an input - array-like object. If an environment supports Symbol.iterator, the returned - iterator is iterable. + array-like object. + + If an environment supports Symbol.iterator, the returned iterator is + iterable. Parameters ---------- - x : collection + x : ArrayLikeObject Input array-like object. Returns diff --git a/lib/node_modules/@stdlib/iter/cartesian-square/docs/types/index.d.ts b/lib/node_modules/@stdlib/iter/cartesian-square/docs/types/index.d.ts index 3d940d8b46bc..ac7576856fb0 100644 --- a/lib/node_modules/@stdlib/iter/cartesian-square/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/iter/cartesian-square/docs/types/index.d.ts @@ -53,7 +53,8 @@ type Iterator = Iter | IterableIterator; * var bool = iterator.next().done; * // returns true */ -declare function iterCartesianSquare( x: ArrayLike | Iterable ): Iterator; +declare function iterCartesianSquare( x: ArrayLike ): Iterator; + // EXPORTS // diff --git a/lib/node_modules/@stdlib/iter/cartesian-square/lib/main.js b/lib/node_modules/@stdlib/iter/cartesian-square/lib/main.js index f7ef51fefacd..61fb276f3cdd 100644 --- a/lib/node_modules/@stdlib/iter/cartesian-square/lib/main.js +++ b/lib/node_modules/@stdlib/iter/cartesian-square/lib/main.js @@ -23,6 +23,8 @@ var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); var iteratorSymbol = require( '@stdlib/symbol/iterator' ); var floor = require( '@stdlib/math/base/special/floor' ); +var isCollection = require('@stdlib/assert/is-collection'); +var resolveGetter = require( '@stdlib/array/base/resolve-getter' ); // MAIN // @@ -30,7 +32,7 @@ var floor = require( '@stdlib/math/base/special/floor' ); /** * Returns an iterator which generates the Cartesian square of an input array-like object. * -* @param {ArrayLike} arr - input array +* @param {Collection} arr - input array * @throws {TypeError} must provide an array-like object * @returns {Iterator} iterator * @@ -48,11 +50,15 @@ var floor = require( '@stdlib/math/base/special/floor' ); function iterCartesianSquare( arr ) { var iter; var FLG; + var get; var i; - if ( !Array.isArray( arr ) ) { + if ( !isCollection( arr ) ) { throw new TypeError( 'invalid argument. Must provide an array-like object. Value: `' + arr + '`.' ); } + + get = resolveGetter( arr ); + iter = {}; FLG = false; i = 0; @@ -63,22 +69,23 @@ function iterCartesianSquare( arr ) { setReadOnly( iter, iteratorSymbol, factory ); } return iter; - /** * Returns an iterator protocol-compliant object containing the next iterated value. -* @private -* @returns {Object} iterator result -*/ + * @private + * @returns {Object} iterator result + */ function next() { var v; + var N; if ( FLG ) { return { 'done': true }; } - v = [ arr[ floor( i / arr.length ) ], arr[ i % arr.length ] ]; + N = arr.length * arr.length; + v = [get(arr, floor(i / arr.length)), get(arr, i % arr.length)]; i += 1; - if ( i >= arr.length * arr.length ) { + if ( i >= N ) { FLG = true; } return { @@ -89,9 +96,9 @@ function iterCartesianSquare( arr ) { /** * Finishes an iterator. -* @private -* @returns {Object} iterator result -*/ + * @private + * @returns {Object} iterator result + */ function finish() { FLG = true; return { @@ -101,9 +108,9 @@ function iterCartesianSquare( arr ) { /** * Returns a new iterator. -* @private -* @returns {Iterator} iterator -*/ + * @private + * @returns {Iterator} iterator + */ function factory() { return iterCartesianSquare( arr ); } diff --git a/lib/node_modules/@stdlib/iter/cartesian-square/test/test.js b/lib/node_modules/@stdlib/iter/cartesian-square/test/test.js index 76e681cbf5a0..dfaa67e9b4ac 100644 --- a/lib/node_modules/@stdlib/iter/cartesian-square/test/test.js +++ b/lib/node_modules/@stdlib/iter/cartesian-square/test/test.js @@ -85,7 +85,49 @@ tape( 'the function returns an iterator which generates the Cartesian square of actual = iter.next().value; t.deepEqual( actual, expected, 'returns expected value' ); - t.ok( iter.next().done, 'returns expected value' ); + t.strictEqual( iter.next().done, true, 'returns expected value' ); t.end(); }); + +tape('the finish function returns an object with done property set to true', function test(t) { + var iterator = iterCartesianSquare([1, 2]); + var result; + iterator.next(); // consume a value to ensure the iterator is not at the start + result = iterator.return(); // call the finish function + t.deepEqual(result, { + 'done': true + }, 'returns expected value'); + t.end(); +}); + +tape('the factory function returns a new iterator', function test(t) { + var iterator1 = iterCartesianSquare([1, 2]); + var iterator2 = iterator1[Symbol.iterator](); // call the factory function + t.notEqual(iterator1, iterator2, 'returns new iterator'); + t.end(); +}); + +tape('the factory function returns an iterator that generates the same sequence', function test(t) { + var iterator1 = iterCartesianSquare([1, 2]); + var iterator2 = iterator1[Symbol.iterator](); // call the factory function + var result1 = []; + var result2 = []; + var next1; + var next2; + + next1 = iterator1.next(); + while (!next1.done) { + result1.push(next1.value); + next1 = iterator1.next(); + } + + next2 = iterator2.next(); + while (!next2.done) { + result2.push(next2.value); + next2 = iterator2.next(); + } + + t.deepEqual(result1, result2, 'returns iterator that generates the same sequence'); + t.end(); +}); From d44610d49208492cd8c57fded4e393f9c6a52c7b Mon Sep 17 00:00:00 2001 From: vinayak Date: Sat, 2 Mar 2024 16:17:09 +0530 Subject: [PATCH 4/4] fix(lib/node_modules/@stdlib/iter/cartesian-square): resolved issues with package Signed-off-by: vinayak --- lib/node_modules/@stdlib/iter/cartesian-square/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/node_modules/@stdlib/iter/cartesian-square/README.md b/lib/node_modules/@stdlib/iter/cartesian-square/README.md index 72e921748cd2..530ba7ac8d9f 100644 --- a/lib/node_modules/@stdlib/iter/cartesian-square/README.md +++ b/lib/node_modules/@stdlib/iter/cartesian-square/README.md @@ -80,6 +80,8 @@ var bool = it.next().done; ```javascript +var iterCartesianSquare = require( '@stdlib/iter/cartesian-square' ); + // Example : Generating Cartesian square of an array var x = [1, 2, 3]; var cartesianSquare = iterCartesianSquare( x );