diff --git a/lib/node_modules/@stdlib/iter/cuany-by/README.md b/lib/node_modules/@stdlib/iter/cuany-by/README.md new file mode 100644 index 00000000000..4d5a558dadf --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cuany-by/README.md @@ -0,0 +1,202 @@ + + +# iterCuAnyBy + +> Create an iterator which cumulatively tests whether at least one iterated value passes a test implemented by a predicate function. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var iterCuAnyBy = require( '@stdlib/iter/cuany-by' ); +``` + +#### iterCuAnyBy( iterator, predicate\[, thisArg] ) + +Returns an [iterator][mdn-iterator-protocol] which cumulatively tests whether at least one iterated value passes a test implemented by a `predicate` function. + +```javascript +var array2iterator = require( '@stdlib/array/to-iterator' ); + +function predicate( v ) { + return ( v > 0 ); +} + +var arr = array2iterator( [ 0, 0, 0, 1, 0 ] ); + +var it = iterCuAnyBy( arr, predicate ); + +var v = it.next().value; +// returns false + +v = it.next().value; +// returns false + +v = it.next().value; +// returns false + +v = it.next().value; +// returns true + +v = it.next().value; +// returns true + +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. + +A `predicate` function is provided two arguments: + +- **value**: iterated value +- **index**: iteration index (zero-based) + +To set the `predicate` function execution context, provide a `thisArg`. + + + +```javascript +var array2iterator = require( '@stdlib/array/to-iterator' ); + +function predicate( v ) { + this.count += 1; + return ( v < 0 ); +} + +var ctx = { + 'count': 0 +}; + +var it = iterCuAnyBy( array2iterator( [ 1, 2, -1, 4 ] ), predicate, ctx ); +// returns + +var v = it.next().value; +// returns false + +v = it.next().value; +// returns false + +v = it.next().value; +// returns true + +v = it.next().value; +// returns true + +var count = ctx.count; +// returns 3 +``` + + + + + + + +
+ +## Notes + +- A `predicate` function is invoked for each iterated value until the first truthy `predicate` function return value. Upon encountering the first truthy return value, the returned iterator ceases to invoke the `predicate` function and returns `true` for each subsequent iterated value of the provided input `iterator`. + +
+ + + + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/iter/randu' ); +var iterCuAnyBy = require( '@stdlib/iter/cuany-by' ); + +function threshold( r ) { + return ( r > 0.95 ); +} + +// Create an iterator which generates uniformly distributed pseudorandom numbers: +var opts = { + 'iter': 100 +}; +var riter = randu( opts ); + +// Create an iterator which cumulatively tests whether at least one iterated value passes a test implemented by a predicate function: +var it = iterCuAnyBy( riter, threshold ); + +// Perform manual iteration... +var r; +while ( true ) { + r = it.next(); + if ( r.done ) { + break; + } + console.log( r.value ); +} +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/iter/cuany-by/benchmark/benchmark.js b/lib/node_modules/@stdlib/iter/cuany-by/benchmark/benchmark.js new file mode 100644 index 00000000000..8305898f3aa --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cuany-by/benchmark/benchmark.js @@ -0,0 +1,89 @@ +/** +* @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 iterConstant = require( '@stdlib/iter/constant' ); +var isIteratorLike = require( '@stdlib/assert/is-iterator-like' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var iterCuAnyBy = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Predicate function. +* +* @private +* @param {*} value - value +* @returns {boolean} result +*/ +function predicate( value ) { + return ( value < 0 ); +} + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var iter; + var it; + var i; + + it = iterConstant( 3.14 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + iter = iterCuAnyBy( it, predicate ); + 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(); +}); + +bench( pkg+'::iteration', function benchmark( b ) { + var iter; + var v; + var i; + + iter = iterCuAnyBy( iterConstant( 3.14 ), predicate ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = iter.next().value; + if ( !isBoolean( v ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( v ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/iter/cuany-by/docs/repl.txt b/lib/node_modules/@stdlib/iter/cuany-by/docs/repl.txt new file mode 100644 index 00000000000..963c58fc713 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cuany-by/docs/repl.txt @@ -0,0 +1,64 @@ + +{{alias}}( iterator, predicate[, thisArg] ) + Returns an iterator which cumulatively tests whether at least one iterated + value passes a test implemented by a predicate function. + + If an environment supports Symbol.iterator and a provided iterator is + iterable, the returned iterator is iterable. + + The predicate function is provided two arguments: + + - value: iterated value + - index: iteration index + + A predicate function is invoked for each iterated value until the first + truthy predicate function return value. Upon encountering the first truthy + return value, the returned iterator ceases to invoke the predicate function + and returns `true` for each subsequent iterated value of the provided input + iterator. + + If provided an iterator which does not return any iterated values, the + function returns an iterator which is also empty. + + Parameters + ---------- + iterator: Object + Input iterator. + + predicate: Function + The test function. + + thisArg: any (optional) + Execution context. + + 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 arr = {{alias:@stdlib/array/to-iterator}}( [ 0, 0, 0, 1, 0 ] ); + > function fcn( v ) { return ( v > 0 ); }; + > var it = {{alias}}( arr, fcn ); + > var v = it.next().value + false + > v = it.next().value + false + > v = it.next().value + false + > v = it.next().value + true + > v = it.next().value + true + + See Also + -------- diff --git a/lib/node_modules/@stdlib/iter/cuany-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/iter/cuany-by/docs/types/index.d.ts new file mode 100644 index 00000000000..07d61e3cfb6 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cuany-by/docs/types/index.d.ts @@ -0,0 +1,98 @@ +/* +* @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 { TypedIterator, TypedIterableIterator } from '@stdlib/types/iter'; + +// Define a union type representing both iterable and non-iterable iterators: +type Iterator = TypedIterator | TypedIterableIterator; + +/** +* Checks whether an iterated value passes a test. +* +* @returns boolean indicating whether an iterated value passes a test +*/ +type Nullary = ( this: U ) => boolean; + +/** +* Checks whether an iterated value passes a test. +* +* @param value - iterated value +* @returns boolean indicating whether an iterated value passes a test +*/ +type Unary = ( this: U, value: T ) => boolean; + +/** +* Checks whether an iterated value passes a test. +* +* @param value - iterated value +* @param index - iteration index +* @returns boolean indicating whether an iterated value passes a test +*/ +type Binary = ( this: U, value: T, index: number ) => boolean; + +/** +* Checks whether an iterated value passes a test. +* +* @param value - iterated value +* @param index - iteration index +* @returns boolean indicating whether an iterated value passes a test +*/ +type Predicate = Nullary | Unary | Binary; + +/** +* Returns an iterator which cumulatively tests whether at least one iterated value passes a test implemented by a predicate function. +* +* @param iterator - source iterator +* @param predicate - predicate function +* @param thisArg - execution context +* @returns iterator +* +* @example +* var array2iterator = require( '@stdlib/array/to-iterator' ); +* +* function isPositive( v ) { +* return ( v > 0 ); +* } +* +* var it = iterCuAnyBy( array2iterator( [ 0, 0, 0, 1, 0 ] ), isPositive ); +* +* var v = it.next().value; +* // returns false +* +* v = it.next().value; +* // returns false +* +* v = it.next().value; +* // returns false +* +* v = it.next().value; +* // returns true +* +* v = it.next().value; +* // returns true +*/ +declare function iterCuAnyBy( iterator: Iterator, predicate: Predicate, thisArg?: ThisParameterType> ): Iterator; + + +// EXPORTS // + +export = iterCuAnyBy ; diff --git a/lib/node_modules/@stdlib/iter/cuany-by/docs/types/test.ts b/lib/node_modules/@stdlib/iter/cuany-by/docs/types/test.ts new file mode 100644 index 00000000000..63f44165e16 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cuany-by/docs/types/test.ts @@ -0,0 +1,109 @@ +/* +* @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 { TypedIterator, TypedIteratorResult } from '@stdlib/types/iter'; +import iterCuAnyBy = require( './index' ); + +/** +* Implements the iterator protocol `next` method. +* +* @returns iterator protocol-compliant object +*/ +function next(): TypedIteratorResult { + return { + 'value': 2.0, + 'done': false + }; +} + +/** +* Returns an iterator protocol-compliant object. +* +* @returns iterator protocol-compliant object +*/ +function iterator(): TypedIterator { + return { + 'next': next + }; +} + +/** +* Predicate function. +* +* @param _v - iterated value +* @param i - iteration index +* @returns a boolean +*/ +function predicate1( _v: any, i: number ): boolean { + return ( i > 10 ); +} + +/** +* Predicate function. +* +* @param v - iterated value +* @returns a boolean +*/ +function predicate2( v: any ): boolean { + return ( v !== v ); +} + + +// TESTS // + +// The function returns an iterator... +{ + iterCuAnyBy( iterator(), predicate1 ); // $ExpectType Iterator + iterCuAnyBy( iterator(), predicate2 ); // $ExpectType Iterator + iterCuAnyBy( iterator(), predicate1, {} ); // $ExpectType Iterator + iterCuAnyBy( iterator(), predicate2, {} ); // $ExpectType Iterator + iterCuAnyBy( iterator(), predicate1, null ); // $ExpectType Iterator + iterCuAnyBy( iterator(), predicate2, null ); // $ExpectType Iterator +} + +// The compiler throws an error if the function is provided a first argument which is not an iterator protocol-compliant object... +{ + iterCuAnyBy( '5', predicate1 ); // $ExpectError + iterCuAnyBy( 5, predicate1 ); // $ExpectError + iterCuAnyBy( true, predicate1 ); // $ExpectError + iterCuAnyBy( false, predicate1 ); // $ExpectError + iterCuAnyBy( null, predicate1 ); // $ExpectError + iterCuAnyBy( undefined, predicate1 ); // $ExpectError + iterCuAnyBy( [], predicate1 ); // $ExpectError + iterCuAnyBy( {}, predicate1 ); // $ExpectError + iterCuAnyBy( ( x: number ): number => x, predicate1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a predicate function... +{ + iterCuAnyBy( iterator(), '5' ); // $ExpectError + iterCuAnyBy( iterator(), 5 ); // $ExpectError + iterCuAnyBy( iterator(), true ); // $ExpectError + iterCuAnyBy( iterator(), false ); // $ExpectError + iterCuAnyBy( iterator(), null ); // $ExpectError + iterCuAnyBy( iterator(), undefined ); // $ExpectError + iterCuAnyBy( iterator(), [] ); // $ExpectError + iterCuAnyBy( iterator(), {} ); // $ExpectError + iterCuAnyBy( iterator(), ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided insufficient arguments... +{ + iterCuAnyBy(); // $ExpectError + iterCuAnyBy( iterator() ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/iter/cuany-by/examples/index.js b/lib/node_modules/@stdlib/iter/cuany-by/examples/index.js new file mode 100644 index 00000000000..fa74098fc6c --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cuany-by/examples/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'; + +var randu = require( '@stdlib/random/iter/randu' ); +var iterCuAnyBy = require( './../lib' ); + +function threshold( r ) { + return ( r > 0.95 ); +} + +// Create an iterator which generates uniformly distributed pseudorandom numbers: +var opts = { + 'iter': 100 +}; +var riter = randu( opts ); + +// Create an iterator which cumulatively tests whether at least one iterated value passes a test implemented by a predicate function: +var it = iterCuAnyBy( riter, threshold ); + +// Perform manual iteration... +var r; +while ( true ) { + r = it.next(); + if ( r.done ) { + break; + } + console.log( r.value ); +} diff --git a/lib/node_modules/@stdlib/iter/cuany-by/lib/index.js b/lib/node_modules/@stdlib/iter/cuany-by/lib/index.js new file mode 100644 index 00000000000..30e1bee7b40 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cuany-by/lib/index.js @@ -0,0 +1,65 @@ +/** +* @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 cumulatively tests whether at least one iterated value passes a test implemented by a predicate function. +* +* @module @stdlib/iter/cuany-by +* +* @example +* var array2iterator = require( '@stdlib/array/to-iterator' ); +* var iterCuAnyBy = require( '@stdlib/iter/cuany-by' ); +* +* function isPositive( value ) { +* return ( value > 0 ); +* } +* +* var arr = array2iterator( [ 0, 0, 0, 1, 0 ] ); +* +* var it = iterCuNoneBy( arr, isPositive ); +* +* var v = it.next().value; +* // returns false +* +* v = it.next().value; +* // returns false +* +* v = it.next().value; +* // returns false +* +* v = it.next().value; +* // returns true +* +* v = it.next().value; +* // returns true +* +* var bool = it.next().done; +* // returns true +*/ + + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/iter/cuany-by/lib/main.js b/lib/node_modules/@stdlib/iter/cuany-by/lib/main.js new file mode 100644 index 00000000000..79a52fe1816 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cuany-by/lib/main.js @@ -0,0 +1,158 @@ +/** +* @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 isFunction = require( '@stdlib/assert/is-function' ); +var isIteratorLike = require( '@stdlib/assert/is-iterator-like' ); +var iteratorSymbol = require( '@stdlib/symbol/iterator' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Returns an iterator which cumulatively tests whether at least one iterated value passes a test implemented by a predicate function. +* +* @param {Iterator} iterator - input iterator +* @param {Function} predicate - predicate function +* @param {*} [thisArg] - execution context +* @throws {TypeError} first argument must be an iterator +* @throws {TypeError} second argument must be a predicate function +* @returns {Iterator} iterator +* +* @example +* var array2iterator = require( '@stdlib/array/to-iterator' ); +* +* function isPositive( value ) { +* return ( value > 0 ); +* } +* +* var arr = array2iterator( [ 0, 0, 0, 1, 0 ] ); +* +* var it = iterCuAnyBy( arr, isPositive ); +* +* var v = it.next().value; +* // returns false +* +* v = it.next().value; +* // returns false +* +* v = it.next().value; +* // returns false +* +* v = it.next().value; +* // returns true +* +* v = it.next().value; +* // returns true +* +* var bool = it.next().done; +* // returns true +*/ +function iterCuAnyBy( iterator, predicate, thisArg ) { + var value; + var iter; + var FLG; + var i; + if ( !isIteratorLike( iterator ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an iterator. Value: `%s`.', iterator ) ); + } + if ( !isFunction( predicate ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be a function. Value: `%s`.', predicate ) ); + } + value = false; + i = -1; + + // Create an iterator protocol-compliant object: + iter = {}; + setReadOnly( iter, 'next', next ); + setReadOnly( iter, 'return', end ); + + // If an environment supports `Symbol.iterator`, make the iterator iterable: + if ( iteratorSymbol && isFunction( iterator[ iteratorSymbol ] ) ) { + setReadOnly( iter, iteratorSymbol, factory ); + } + return iter; + + /** + * Returns an iterator protocol-compliant object containing the next iterated value. + * + * @private + * @returns {Object} iterator protocol-compliant object + */ + function next() { + var v; + if ( FLG ) { + return { + 'done': true + }; + } + v = iterator.next(); + if ( v.done ) { + FLG = true; + return v; + } + i += 1; + if ( !value && predicate.call( thisArg, v.value, i ) ) { + value = true; + } + return { + 'value': value, + 'done': false + }; + } + + /** + * Finishes an iterator. + * + * @private + * @param {*} [value] - value to return + * @returns {Object} iterator protocol-compliant object + */ + function end( value ) { + FLG = true; + if ( arguments.length ) { + return { + 'value': value, + 'done': true + }; + } + return { + 'done': true + }; + } + + /** + * Returns a new iterator. + * + * @private + * @returns {Iterator} iterator + */ + function factory() { + return iterCuAnyBy( iterator[ iteratorSymbol ](), predicate, thisArg ); + } +} + + +// EXPORTS // + +module.exports = iterCuAnyBy; diff --git a/lib/node_modules/@stdlib/iter/cuany-by/package.json b/lib/node_modules/@stdlib/iter/cuany-by/package.json new file mode 100644 index 00000000000..04231925dd3 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cuany-by/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/iter/cuany-by", + "version": "0.0.0", + "description": "Create an iterator which cumulatively tests whether at least one iterated value passes a test implemented by a predicate function.", + "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", + "test", + "any", + "iterate", + "iterator", + "iter", + "validate" + ] +} diff --git a/lib/node_modules/@stdlib/iter/cuany-by/test/test.js b/lib/node_modules/@stdlib/iter/cuany-by/test/test.js new file mode 100644 index 00000000000..47a93b6717b --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cuany-by/test/test.js @@ -0,0 +1,365 @@ +/** +* @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 proxyquire = require( 'proxyquire' ); +var array2iterator = require( '@stdlib/array/to-iterator' ); +var randu = require( '@stdlib/random/iter/randu' ); +var iteratorSymbol = require( '@stdlib/symbol/iterator' ); +var noop = require( '@stdlib/utils/noop' ); +var iterCuAnyBy = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Predicate function. +* +* @private +* @param {*} value - value +* @returns {boolean} result +*/ +function predicate( value ) { + return ( value > 0 ); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof iterCuAnyBy, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if not provided an iterator', function test( t ) { + var values; + var i; + + 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 a type error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + iterCuAnyBy( value, noop ); + }; + } +}); + +tape( 'the function throws an error if not provided a predicate function as a second argument', function test( t ) { + var values; + var i; + + values = [ + '5', + -5, + 0, + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + iterCuAnyBy( array2iterator( [ 1, 2, 3 ] ), value ); + }; + } +}); + +tape( 'the function returns an iterator protocol-compliant object', function test( t ) { + var arr; + var it; + var r; + var i; + + arr = array2iterator( [ 0, 0, 1, 0, 1 ] ); + it = iterCuAnyBy( arr, predicate ); + for ( i = 0; i < 5; i++ ) { + r = it.next(); + t.equal( typeof r.value, 'boolean', 'returns expected value' ); + t.equal( typeof r.done, 'boolean', 'returns expected value' ); + } + t.equal( typeof r.done, 'boolean', 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an "empty" iterator, the function returns an iterator which is also empty', function test( t ) { + var arr; + var it; + var v; + + arr = array2iterator( [] ); + it = iterCuAnyBy( arr, predicate ); + + v = it.next(); + t.strictEqual( v.done, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an iterator which cumulatively tests whether at least one iterated value passes a test implemented by a predicate function', function test( t ) { + var expected; + var actual; + var values; + var it; + var i; + + values = [ 0, 0, 1, 1, 0, 0 ]; + expected = [ + { + 'value': false, + 'done': false + }, + { + 'value': false, + 'done': false + }, + { + 'value': true, + 'done': false + }, + { + 'value': true, + 'done': false + }, + { + 'value': true, + 'done': false + }, + { + 'value': true, + 'done': false + }, + { + 'done': true + } + ]; + + it = iterCuAnyBy( array2iterator( values ), predicate ); + + actual = []; + for ( i = 0; i < expected.length; i++ ) { + actual.push( it.next() ); + } + t.deepEqual( actual, expected, 'returns expected values' ); + t.end(); +}); + +tape( 'the returned iterator has a `return` method for closing an iterator (no argument)', function test( t ) { + var it; + var r; + + it = iterCuAnyBy( randu(), predicate ); + + r = it.next(); + t.equal( typeof r.value, 'boolean', 'returns expected value' ); + t.equal( r.done, false, 'returns expected value' ); + + r = it.next(); + t.equal( typeof r.value, 'boolean', 'returns expected value' ); + t.equal( r.done, false, 'returns expected value' ); + + r = it.return(); + t.equal( r.value, void 0, 'returns expected value' ); + t.equal( r.done, true, 'returns expected value' ); + + r = it.next(); + t.equal( r.value, void 0, 'returns expected value' ); + t.equal( r.done, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the returned iterator has a `return` method for closing an iterator (argument)', function test( t ) { + var it; + var r; + + it = iterCuAnyBy( randu(), predicate ); + + r = it.next(); + t.equal( typeof r.value, 'boolean', 'returns expected value' ); + t.equal( r.done, false, 'returns expected value' ); + + r = it.next(); + t.equal( typeof r.value, 'boolean', 'returns expected value' ); + t.equal( r.done, false, 'returns expected value' ); + + r = it.return( 'foo' ); + t.equal( r.value, 'foo', 'returns expected value' ); + t.equal( r.done, true, 'returns expected value' ); + + r = it.next(); + t.equal( r.value, void 0, 'returns expected value' ); + t.equal( r.done, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if an environment supports `Symbol.iterator` and the provided iterator is iterable, the returned iterator is iterable', function test( t ) { + var iterCuAnyBy; + var opts; + var rand; + var it1; + var it2; + var i; + + iterCuAnyBy = proxyquire( './../lib/main.js', { + '@stdlib/symbol/iterator': '__ITERATOR_SYMBOL__' + }); + + opts = { + 'seed': 12345 + }; + rand = randu( opts ); + rand[ '__ITERATOR_SYMBOL__' ] = factory; + + it1 = iterCuAnyBy( rand, predicate ); + t.equal( typeof it1[ '__ITERATOR_SYMBOL__' ], 'function', 'has method' ); + t.equal( it1[ '__ITERATOR_SYMBOL__' ].length, 0, 'has zero arity' ); + + it2 = it1[ '__ITERATOR_SYMBOL__' ](); + t.equal( typeof it2, 'object', 'returns an object' ); + t.equal( typeof it2.next, 'function', 'has method' ); + t.equal( typeof it2.return, 'function', 'has method' ); + + for ( i = 0; i < 100; i++ ) { + t.equal( it2.next().value, it1.next().value, 'returns expected value' ); + } + t.end(); + + function factory() { + return randu( opts ); + } +}); + +tape( 'if an environment does not support `Symbol.iterator`, the returned iterator is not "iterable"', function test( t ) { + var iterCuAnyBy; + var it; + + iterCuAnyBy = proxyquire( './../lib/main.js', { + '@stdlib/symbol/iterator': false + }); + + it = iterCuAnyBy( randu(), predicate); + t.equal( it[ iteratorSymbol ], void 0, 'does not have property' ); + + t.end(); +}); + +tape( 'if a provided iterator is not iterable, the returned iterator is not iterable', function test( t ) { + var iterCuAnyBy; + var rand; + var it; + + iterCuAnyBy = proxyquire( './../lib/main.js', { + '@stdlib/symbol/iterator': '__ITERATOR_SYMBOL__' + }); + + rand = randu(); + rand[ '__ITERATOR_SYMBOL__' ] = null; + + it = iterCuAnyBy( rand, predicate ); + t.equal( it[ iteratorSymbol ], void 0, 'does not have property' ); + + t.end(); +}); + +tape( 'the function supports specifying the predicate function execution context', function test( t ) { + var expected; + var actual; + var values; + var ctx; + var it; + var i; + + ctx = { + 'count': 0 + }; + + values = [ -1, -3, 2, 0, 1 ]; + expected = [ + { + 'value': false, + 'done': false + }, + { + 'value': false, + 'done': false + }, + { + 'value': true, + 'done': false + }, + { + 'value': true, + 'done': false + }, + { + 'value': true, + 'done': false + }, + { + 'done': true + } + ]; + + it = iterCuAnyBy( array2iterator( values ), predicate, ctx ); + t.equal( it.next.length, 0, 'has zero arity' ); + + actual = []; + for ( i = 0; i < expected.length; i++ ) { + actual.push( it.next() ); + } + t.deepEqual( actual, expected, 'returns expected values' ); + t.equal( ctx.count, 3, 'returns expected value' ); + t.end(); + + function predicate( v ) { + this.count += 1; // eslint-disable-line no-invalid-this + return ( v > 0 ); + } +}); diff --git a/lib/node_modules/@stdlib/iter/cuevery-by/README.md b/lib/node_modules/@stdlib/iter/cuevery-by/README.md new file mode 100644 index 00000000000..4cf25c970db --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cuevery-by/README.md @@ -0,0 +1,202 @@ + + +# iterCuEveryBy + +> Create an iterator which cumulatively tests whether every iterated value passes a test implemented by a predicate function. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var iterCuEveryBy = require( '@stdlib/iter/cuevery-by' ); +``` + +#### iterCuEveryBy( iterator, predicate\[, thisArg] ) + +Returns an [iterator][mdn-iterator-protocol] which cumulatively tests whether every iterated value passes a test implemented by a `predicate` function. + +```javascript +var array2iterator = require( '@stdlib/array/to-iterator' ); + +function predicate( v ) { + return ( v > 0 ); +} + +var arr = array2iterator( [ 1, 1, 1, 0, 1 ] ); + +var it = iterCuEveryBy( arr, predicate ); + +var v = it.next().value; +// returns true + +v = it.next().value; +// returns true + +v = it.next().value; +// returns true + +v = it.next().value; +// returns false + +v = it.next().value; +// returns false + +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. + +A `predicate` function is provided two arguments: + +- **value**: iterated value +- **index**: iteration index (zero-based) + +To set the `predicate` function execution context, provide a `thisArg`. + + + +```javascript +var array2iterator = require( '@stdlib/array/to-iterator' ); + +function predicate( v ) { + this.count += 1; + return ( v > 0 ); +} + +var ctx = { + 'count': 0 +}; + +var it = iterCuEveryBy( array2iterator( [ 1, 2, 3, 4 ] ), predicate, ctx ); +// returns + +var v = it.next().value; +// returns true + +v = it.next().value; +// returns true + +v = it.next().value; +// returns true + +v = it.next().value; +// returns true + +var count = ctx.count; +// returns 4 +``` + + + + + + + +
+ +## Notes + +- A `predicate` function is invoked for each iterated value until the first falsy `predicate` function return value. Upon encountering the first falsy return value, the returned iterator ceases to invoke the `predicate` function and returns `false` for each subsequent iterated value of the provided input `iterator`. + +
+ + + + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/iter/randu' ); +var iterCuEveryBy = require( '@stdlib/iter/cuevery-by' ); + +function threshold( r ) { + return ( r > 0.05 ); +} + +// Create an iterator which generates uniformly distributed pseudorandom numbers: +var opts = { + 'iter': 100 +}; +var riter = randu( opts ); + +// Create an iterator which cumulatively tests whether every iterated value passes a test: +var it = iterCuEveryBy( riter, threshold ); + +// Perform manual iteration... +var r; +while ( true ) { + r = it.next(); + if ( r.done ) { + break; + } + console.log( r.value ); +} +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/iter/cuevery-by/benchmark/benchmark.js b/lib/node_modules/@stdlib/iter/cuevery-by/benchmark/benchmark.js new file mode 100644 index 00000000000..63a4037b3de --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cuevery-by/benchmark/benchmark.js @@ -0,0 +1,89 @@ +/** +* @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 iterConstant = require( '@stdlib/iter/constant' ); +var isIteratorLike = require( '@stdlib/assert/is-iterator-like' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var iterCuEveryBy = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Predicate function. +* +* @private +* @param {*} value - value +* @returns {boolean} result +*/ +function predicate( value ) { + return ( value > 0 ); +} + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var iter; + var it; + var i; + + it = iterConstant( 3.14 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + iter = iterCuEveryBy( it, predicate ); + 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(); +}); + +bench( pkg+'::iteration', function benchmark( b ) { + var iter; + var v; + var i; + + iter = iterCuEveryBy( iterConstant( 3.14 ), predicate ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = iter.next().value; + if ( !isBoolean( v ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( v ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/iter/cuevery-by/docs/repl.txt b/lib/node_modules/@stdlib/iter/cuevery-by/docs/repl.txt new file mode 100644 index 00000000000..34f203364bf --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cuevery-by/docs/repl.txt @@ -0,0 +1,64 @@ + +{{alias}}( iterator, predicate[, thisArg] ) + Returns an iterator which cumulatively tests whether every iterated value + passes a test implemented by a predicate function. + + If an environment supports Symbol.iterator and a provided iterator is + iterable, the returned iterator is iterable. + + The predicate function is provided two arguments: + + - value: iterated value + - index: iteration index + + A predicate function is invoked for each iterated value until the first + falsy predicate function return value. Upon encountering the first falsy + return value, the returned iterator ceases to invoke the predicate function + and returns `false` for each subsequent iterated value of the provided input + iterator. + + If provided an iterator which does not return any iterated values, the + function returns an iterator which is also empty. + + Parameters + ---------- + iterator: Object + Input iterator. + + predicate: Function + The test function. + + thisArg: any (optional) + Execution context. + + 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 arr = {{alias:@stdlib/array/to-iterator}}( [ 1, 1, 1, 0, 1 ] ); + > function fcn( v ) { return ( v > 0 ); }; + > var it = {{alias}}( arr, fcn ); + > var v = it.next().value + true + > v = it.next().value + true + > v = it.next().value + true + > v = it.next().value + false + > v = it.next().value + false + + See Also + -------- diff --git a/lib/node_modules/@stdlib/iter/cuevery-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/iter/cuevery-by/docs/types/index.d.ts new file mode 100644 index 00000000000..34b5672200c --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cuevery-by/docs/types/index.d.ts @@ -0,0 +1,98 @@ +/* +* @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 { TypedIterator, TypedIterableIterator } from '@stdlib/types/iter'; + +// Define a union type representing both iterable and non-iterable iterators: +type Iterator = TypedIterator | TypedIterableIterator; + +/** +* Checks whether an iterated value passes a test. +* +* @returns boolean indicating whether an iterated value passes a test +*/ +type Nullary = ( this: U ) => boolean; + +/** +* Checks whether an iterated value passes a test. +* +* @param value - iterated value +* @returns boolean indicating whether an iterated value passes a test +*/ +type Unary = ( this: U, value: T ) => boolean; + +/** +* Checks whether an iterated value passes a test. +* +* @param value - iterated value +* @param index - iteration index +* @returns boolean indicating whether an iterated value passes a test +*/ +type Binary = ( this: U, value: T, index: number ) => boolean; + +/** +* Checks whether an iterated value passes a test. +* +* @param value - iterated value +* @param index - iteration index +* @returns boolean indicating whether an iterated value passes a test +*/ +type Predicate = Nullary | Unary | Binary; + +/** +* Returns an iterator which cumulatively tests whether every iterated value passes a test implemented by a predicate function. +* +* @param iterator - source iterator +* @param predicate - predicate function +* @param thisArg - execution context +* @returns iterator +* +* @example +* var array2iterator = require( '@stdlib/array/to-iterator' ); +* +* function isPositive( v ) { +* return ( v > 0 ); +* } +* +* var it = iterCuEveryBy( array2iterator( [ 1, 1, 1, 0, 1 ] ), isPositive ); +* +* var v = it.next().value; +* // returns true +* +* v = it.next().value; +* // returns true +* +* v = it.next().value; +* // returns true +* +* v = it.next().value; +* // returns false +* +* v = it.next().value; +* // returns false +*/ +declare function iterCuEveryBy( iterator: Iterator, predicate: Predicate, thisArg?: ThisParameterType> ): Iterator; + + +// EXPORTS // + +export = iterCuEveryBy ; diff --git a/lib/node_modules/@stdlib/iter/cuevery-by/docs/types/test.ts b/lib/node_modules/@stdlib/iter/cuevery-by/docs/types/test.ts new file mode 100644 index 00000000000..d756e029c94 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cuevery-by/docs/types/test.ts @@ -0,0 +1,109 @@ +/* +* @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 { TypedIterator, TypedIteratorResult } from '@stdlib/types/iter'; +import iterCuEveryBy = require( './index' ); + +/** +* Implements the iterator protocol `next` method. +* +* @returns iterator protocol-compliant object +*/ +function next(): TypedIteratorResult { + return { + 'value': 2.0, + 'done': false + }; +} + +/** +* Returns an iterator protocol-compliant object. +* +* @returns iterator protocol-compliant object +*/ +function iterator(): TypedIterator { + return { + 'next': next + }; +} + +/** +* Predicate function. +* +* @param _v - iterated value +* @param i - iteration index +* @returns a boolean +*/ +function predicate1( _v: any, i: number ): boolean { + return ( i > 10 ); +} + +/** +* Predicate function. +* +* @param v - iterated value +* @returns a boolean +*/ +function predicate2( v: any ): boolean { + return ( v !== v ); +} + + +// TESTS // + +// The function returns an iterator... +{ + iterCuEveryBy( iterator(), predicate1 ); // $ExpectType Iterator + iterCuEveryBy( iterator(), predicate2 ); // $ExpectType Iterator + iterCuEveryBy( iterator(), predicate1, {} ); // $ExpectType Iterator + iterCuEveryBy( iterator(), predicate2, {} ); // $ExpectType Iterator + iterCuEveryBy( iterator(), predicate1, null ); // $ExpectType Iterator + iterCuEveryBy( iterator(), predicate2, null ); // $ExpectType Iterator +} + +// The compiler throws an error if the function is provided a first argument which is not an iterator protocol-compliant object... +{ + iterCuEveryBy( '5', predicate1 ); // $ExpectError + iterCuEveryBy( 5, predicate1 ); // $ExpectError + iterCuEveryBy( true, predicate1 ); // $ExpectError + iterCuEveryBy( false, predicate1 ); // $ExpectError + iterCuEveryBy( null, predicate1 ); // $ExpectError + iterCuEveryBy( undefined, predicate1 ); // $ExpectError + iterCuEveryBy( [], predicate1 ); // $ExpectError + iterCuEveryBy( {}, predicate1 ); // $ExpectError + iterCuEveryBy( ( x: number ): number => x, predicate1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a predicate function... +{ + iterCuEveryBy( iterator(), '5' ); // $ExpectError + iterCuEveryBy( iterator(), 5 ); // $ExpectError + iterCuEveryBy( iterator(), true ); // $ExpectError + iterCuEveryBy( iterator(), false ); // $ExpectError + iterCuEveryBy( iterator(), null ); // $ExpectError + iterCuEveryBy( iterator(), undefined ); // $ExpectError + iterCuEveryBy( iterator(), [] ); // $ExpectError + iterCuEveryBy( iterator(), {} ); // $ExpectError + iterCuEveryBy( iterator(), ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided insufficient arguments... +{ + iterCuEveryBy(); // $ExpectError + iterCuEveryBy( iterator() ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/iter/cuevery-by/examples/index.js b/lib/node_modules/@stdlib/iter/cuevery-by/examples/index.js new file mode 100644 index 00000000000..ccc710f03ac --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cuevery-by/examples/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'; + +var randu = require( '@stdlib/random/iter/randu' ); +var iterCuEveryBy = require( './../lib' ); + +function threshold( r ) { + return ( r > 0.05 ); +} + +// Create an iterator which generates uniformly distributed pseudorandom numbers: +var opts = { + 'iter': 100 +}; +var riter = randu( opts ); + +// Create an iterator which cumulatively tests whether every iterated value passes a test: +var it = iterCuEveryBy( riter, threshold ); + +// Perform manual iteration... +var r; +while ( true ) { + r = it.next(); + if ( r.done ) { + break; + } + console.log( r.value ); +} diff --git a/lib/node_modules/@stdlib/iter/cuevery-by/lib/index.js b/lib/node_modules/@stdlib/iter/cuevery-by/lib/index.js new file mode 100644 index 00000000000..83db4b7e121 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cuevery-by/lib/index.js @@ -0,0 +1,65 @@ +/** +* @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 cumulatively tests whether every iterated value passes a test implemented by a predicate function. +* +* @module @stdlib/iter/cuevery-by +* +* @example +* var array2iterator = require( '@stdlib/array/to-iterator' ); +* var iterCuEveryBy = require( '@stdlib/iter/cuevery-by' ); +* +* function isPositive( value ) { +* return ( value > 0 ); +* } +* +* var arr = array2iterator( [ 1, 1, 1, 0, 1 ] ); +* +* var it = iterCuEveryBy( arr, isPositive ); +* +* var v = it.next().value; +* // returns true +* +* v = it.next().value; +* // returns true +* +* v = it.next().value; +* // returns true +* +* v = it.next().value; +* // returns false +* +* v = it.next().value; +* // returns false +* +* var bool = it.next().done; +* // returns true +*/ + + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/iter/cuevery-by/lib/main.js b/lib/node_modules/@stdlib/iter/cuevery-by/lib/main.js new file mode 100644 index 00000000000..56a68824e91 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cuevery-by/lib/main.js @@ -0,0 +1,158 @@ +/** +* @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 isFunction = require( '@stdlib/assert/is-function' ); +var isIteratorLike = require( '@stdlib/assert/is-iterator-like' ); +var iteratorSymbol = require( '@stdlib/symbol/iterator' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Returns an iterator which cumulatively tests whether every iterated value passes a test implemented by a predicate function. +* +* @param {Iterator} iterator - input iterator +* @param {Function} predicate - predicate function +* @param {*} [thisArg] - execution context +* @throws {TypeError} first argument must be an iterator +* @throws {TypeError} second argument must be a predicate function +* @returns {Iterator} iterator +* +* @example +* var array2iterator = require( '@stdlib/array/to-iterator' ); +* +* function isPositive( value ) { +* return ( value > 0 ); +* } +* +* var arr = array2iterator( [ 1, 1, 1, 0, 1 ] ); +* +* var it = iterCuEveryBy( arr, isPositive ); +* +* var v = it.next().value; +* // returns true +* +* v = it.next().value; +* // returns true +* +* v = it.next().value; +* // returns true +* +* v = it.next().value; +* // returns false +* +* v = it.next().value; +* // returns false +* +* var bool = it.next().done; +* // returns true +*/ +function iterCuEveryBy( iterator, predicate, thisArg ) { + var value; + var iter; + var FLG; + var i; + if ( !isIteratorLike( iterator ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an iterator. Value: `%s`.', iterator ) ); + } + if ( !isFunction( predicate ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be a function. Value: `%s`.', predicate ) ); + } + value = true; + i = -1; + + // Create an iterator protocol-compliant object: + iter = {}; + setReadOnly( iter, 'next', next ); + setReadOnly( iter, 'return', end ); + + // If an environment supports `Symbol.iterator`, make the iterator iterable: + if ( iteratorSymbol && isFunction( iterator[ iteratorSymbol ] ) ) { + setReadOnly( iter, iteratorSymbol, factory ); + } + return iter; + + /** + * Returns an iterator protocol-compliant object containing the next iterated value. + * + * @private + * @returns {Object} iterator protocol-compliant object + */ + function next() { + var v; + if ( FLG ) { + return { + 'done': true + }; + } + v = iterator.next(); + if ( v.done ) { + FLG = true; + return v; + } + i += 1; + if ( value && !predicate.call( thisArg, v.value, i ) ) { + value = false; + } + return { + 'value': value, + 'done': false + }; + } + + /** + * Finishes an iterator. + * + * @private + * @param {*} [value] - value to return + * @returns {Object} iterator protocol-compliant object + */ + function end( value ) { + FLG = true; + if ( arguments.length ) { + return { + 'value': value, + 'done': true + }; + } + return { + 'done': true + }; + } + + /** + * Returns a new iterator. + * + * @private + * @returns {Iterator} iterator + */ + function factory() { + return iterCuEveryBy( iterator[ iteratorSymbol ](), predicate, thisArg ); + } +} + + +// EXPORTS // + +module.exports = iterCuEveryBy; diff --git a/lib/node_modules/@stdlib/iter/cuevery-by/package.json b/lib/node_modules/@stdlib/iter/cuevery-by/package.json new file mode 100644 index 00000000000..4aef4d53cae --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cuevery-by/package.json @@ -0,0 +1,68 @@ +{ + "name": "@stdlib/iter/cuevery-by", + "version": "0.0.0", + "description": "Create an iterator which cumulatively tests whether every iterated value passes a test implemented by a predicate function.", + "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", + "none", + "every", + "all", + "cuevery", + "iterator", + "iterate", + "iteration", + "iter" + ] +} diff --git a/lib/node_modules/@stdlib/iter/cuevery-by/test/test.js b/lib/node_modules/@stdlib/iter/cuevery-by/test/test.js new file mode 100644 index 00000000000..7b0ae0c7cc6 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cuevery-by/test/test.js @@ -0,0 +1,364 @@ +/** +* @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 proxyquire = require( 'proxyquire' ); +var array2iterator = require( '@stdlib/array/to-iterator' ); +var randu = require( '@stdlib/random/iter/randu' ); +var iteratorSymbol = require( '@stdlib/symbol/iterator' ); +var noop = require( '@stdlib/utils/noop' ); +var iterCuEveryBy = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Predicate function. +* +* @private +* @param {*} value - value +* @returns {boolean} result +*/ +function predicate( value ) { + return ( value > 0 ); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof iterCuEveryBy, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if not provided an iterator', function test( t ) { + var values; + var i; + + 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 a type error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + iterCuEveryBy( value, noop ); + }; + } +}); + +tape( 'the function throws an error if not provided a predicate function as a second argument', function test( t ) { + var values; + var i; + + values = [ + '5', + -5, + 0, + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + iterCuEveryBy( array2iterator( [ 1, 2, 3 ] ), value ); + }; + } +}); + +tape( 'the function returns an iterator protocol-compliant object', function test( t ) { + var arr; + var it; + var r; + var i; + + arr = array2iterator( [ 0, 0, 1, 0, 1 ] ); + it = iterCuEveryBy( arr, predicate ); + for ( i = 0; i < 5; i++ ) { + r = it.next(); + t.equal( typeof r.value, 'boolean', 'returns expected value' ); + t.equal( typeof r.done, 'boolean', 'returns expected value' ); + } + t.equal( typeof r.done, 'boolean', 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an "empty" iterator, the function returns an iterator which is also empty', function test( t ) { + var arr; + var it; + var v; + + arr = array2iterator( [] ); + it = iterCuEveryBy( arr, predicate ); + + v = it.next(); + t.strictEqual( v.done, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an iterator which cumulatively tests whether every iterated value passes a test implemented by a predicate function', function test( t ) { + var expected; + var actual; + var values; + var it; + var i; + + values = [ 1, 1, 0, 0, 0, 0 ]; + expected = [ + { + 'value': true, + 'done': false + }, + { + 'value': true, + 'done': false + }, + { + 'value': false, + 'done': false + }, + { + 'value': false, + 'done': false + }, + { + 'value': false, + 'done': false + }, + { + 'value': false, + 'done': false + }, + { + 'done': true + } + ]; + + it = iterCuEveryBy( array2iterator( values ), predicate ); + + actual = []; + for ( i = 0; i < expected.length; i++ ) { + actual.push( it.next() ); + } + t.deepEqual( actual, expected, 'returns expected values' ); + t.end(); +}); + +tape( 'the returned iterator has a `return` method for closing an iterator (no argument)', function test( t ) { + var it; + var r; + + it = iterCuEveryBy( randu(), predicate ); + + r = it.next(); + t.equal( typeof r.value, 'boolean', 'returns expected value' ); + t.equal( r.done, false, 'returns expected value' ); + + r = it.next(); + t.equal( typeof r.value, 'boolean', 'returns expected value' ); + t.equal( r.done, false, 'returns expected value' ); + + r = it.return(); + t.equal( r.value, void 0, 'returns expected value' ); + t.equal( r.done, true, 'returns expected value' ); + + r = it.next(); + t.equal( r.value, void 0, 'returns expected value' ); + t.equal( r.done, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the returned iterator has a `return` method for closing an iterator (argument)', function test( t ) { + var it; + var r; + + it = iterCuEveryBy( randu(), predicate ); + + r = it.next(); + t.equal( typeof r.value, 'boolean', 'returns expected value' ); + t.equal( r.done, false, 'returns expected value' ); + + r = it.next(); + t.equal( typeof r.value, 'boolean', 'returns expected value' ); + t.equal( r.done, false, 'returns expected value' ); + + r = it.return( 'foo' ); + t.equal( r.value, 'foo', 'returns expected value' ); + t.equal( r.done, true, 'returns expected value' ); + + r = it.next(); + t.equal( r.value, void 0, 'returns expected value' ); + t.equal( r.done, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if an environment supports `Symbol.iterator` and the provided iterator is iterable, the returned iterator is iterable', function test( t ) { + var iterCuEveryBy; + var opts; + var rand; + var it1; + var it2; + var i; + + iterCuEveryBy = proxyquire( './../lib/main.js', { + '@stdlib/symbol/iterator': '__ITERATOR_SYMBOL__' + }); + + opts = { + 'seed': 12345 + }; + rand = randu( opts ); + rand[ '__ITERATOR_SYMBOL__' ] = factory; + + it1 = iterCuEveryBy( rand, predicate ); + t.equal( typeof it1[ '__ITERATOR_SYMBOL__' ], 'function', 'has method' ); + t.equal( it1[ '__ITERATOR_SYMBOL__' ].length, 0, 'has zero arity' ); + + it2 = it1[ '__ITERATOR_SYMBOL__' ](); + t.equal( typeof it2, 'object', 'returns an object' ); + t.equal( typeof it2.next, 'function', 'has method' ); + t.equal( typeof it2.return, 'function', 'has method' ); + + for ( i = 0; i < 100; i++ ) { + t.equal( it2.next().value, it1.next().value, 'returns expected value' ); + } + t.end(); + + function factory() { + return randu( opts ); + } +}); + +tape( 'if an environment does not support `Symbol.iterator`, the returned iterator is not "iterable"', function test( t ) { + var iterCuEveryBy; + var it; + + iterCuEveryBy = proxyquire( './../lib/main.js', { + '@stdlib/symbol/iterator': false + }); + + it = iterCuEveryBy( randu(), predicate); + t.equal( it[ iteratorSymbol ], void 0, 'does not have property' ); + + t.end(); +}); + +tape( 'if a provided iterator is not iterable, the returned iterator is not iterable', function test( t ) { + var iterCuEveryBy; + var rand; + var it; + + iterCuEveryBy = proxyquire( './../lib/main.js', { + '@stdlib/symbol/iterator': '__ITERATOR_SYMBOL__' + }); + + rand = randu(); + rand[ '__ITERATOR_SYMBOL__' ] = null; + + it = iterCuEveryBy( rand, predicate ); + t.equal( it[ iteratorSymbol ], void 0, 'does not have property' ); + + t.end(); +}); +tape( 'the function supports specifying the predicate function execution context', function test( t ) { + var expected; + var actual; + var values; + var ctx; + var it; + var i; + + ctx = { + 'count': 0 + }; + + values = [ 1, 3, -2, 0, 1 ]; + expected = [ + { + 'value': true, + 'done': false + }, + { + 'value': true, + 'done': false + }, + { + 'value': false, + 'done': false + }, + { + 'value': false, + 'done': false + }, + { + 'value': false, + 'done': false + }, + { + 'done': true + } + ]; + + it = iterCuEveryBy( array2iterator( values ), predicate, ctx ); + t.equal( it.next.length, 0, 'has zero arity' ); + + actual = []; + for ( i = 0; i < expected.length; i++ ) { + actual.push( it.next() ); + } + t.deepEqual( actual, expected, 'returns expected values' ); + t.equal( ctx.count, 3, 'returns expected value' ); + t.end(); + + function predicate( v ) { + this.count += 1; // eslint-disable-line no-invalid-this + return ( v > 0 ); + } +}); diff --git a/lib/node_modules/@stdlib/iter/cuevery/README.md b/lib/node_modules/@stdlib/iter/cuevery/README.md new file mode 100644 index 00000000000..65ce2dc0d87 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cuevery/README.md @@ -0,0 +1,159 @@ + + +# iterCuEvery + +> Create an [iterator][mdn-iterator-protocol] which cumulatively tests whether every iterated value is truthy. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var iterCuEvery = require( '@stdlib/iter/cuevery' ); +``` + +#### iterCuEvery( iterator ) + +Returns an [iterator][mdn-iterator-protocol] which cumulatively tests whether every iterated value is truthy. + +```javascript +var array2iterator = require( '@stdlib/array/to-iterator' ); + +var arr = array2iterator( [ 1, 1, 1, 0, 1 ] ); + +var it = iterCuEvery( arr ); +// returns + +var v = it.next().value; +// returns true + +v = it.next().value; +// returns true + +v = it.next().value; +// returns true + +v = it.next().value; +// returns false + +v = it.next().value; +// returns false + +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. + + + + + + + +
+ +
+ + + + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/iter/randu' ); +var iterMap = require( '@stdlib/iter/map' ); +var iterCuEvery = require( '@stdlib/iter/cuevery' ); + +function threshold( r ) { + return ( r > 0.1 ); +} + +// Create an iterator which generates uniformly distributed pseudorandom numbers: +var opts = { + 'iter': 100 +}; +var riter = randu( opts ); + +// Create an iterator which applies a threshold to generated numbers: +var miter = iterMap( riter, threshold ); + +// Create an iterator which cumulatively tests whether every iterated value is truthy: +var it = iterCuEvery( miter ); + +// Perform manual iteration... +var r; +while ( true ) { + r = it.next(); + if ( r.done ) { + break; + } + console.log( r.value ); +} +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/iter/cuevery/benchmark/benchmark.js b/lib/node_modules/@stdlib/iter/cuevery/benchmark/benchmark.js new file mode 100644 index 00000000000..35343b2cc42 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cuevery/benchmark/benchmark.js @@ -0,0 +1,75 @@ +/** +* @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 iterConstant = require( '@stdlib/iter/constant' ); +var isIteratorLike = require( '@stdlib/assert/is-iterator-like' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var iterCuEvery = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var iter; + var it; + var i; + + it = iterConstant( 3.14 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + iter = iterCuEvery( it ); + 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(); +}); + +bench( pkg+'::iteration', function benchmark( b ) { + var iter; + var v; + var i; + + iter = iterCuEvery( iterConstant( 3.14 ) ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = iter.next().value; + if ( !isBoolean( v ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( v ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/iter/cuevery/docs/repl.txt b/lib/node_modules/@stdlib/iter/cuevery/docs/repl.txt new file mode 100644 index 00000000000..68af81a19c7 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cuevery/docs/repl.txt @@ -0,0 +1,44 @@ + +{{alias}}( iterator ) + Returns an iterator which cumulatively tests whether every iterated value is + truthy. + + If an environment supports Symbol.iterator and a provided iterator is + iterable, the returned iterator is iterable. + + Parameters + ---------- + iterator: Object + Input iterator. + + 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 arr = {{alias:@stdlib/array/to-iterator}}( [ 1, 1, 1, 0, 1 ] ); + > var it = {{alias}}( arr ); + > var v = it.next().value + true + > v = it.next().value + true + > v = it.next().value + true + > v = it.next().value + false + > v = it.next().value + false + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/iter/cuevery/docs/types/index.d.ts b/lib/node_modules/@stdlib/iter/cuevery/docs/types/index.d.ts new file mode 100644 index 00000000000..202ab2a41c7 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cuevery/docs/types/index.d.ts @@ -0,0 +1,64 @@ +/* +* @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'; + +// Define a union type representing both iterable and non-iterable iterators: +type Iterator = Iter | IterableIterator; + +/** +* Returns an iterator which cumulatively tests whether every iterated value is truthy. +* +* @param iterator - input iterator +* @returns iterator +* +* @example +* var array2iterator = require( '@stdlib/array/to-iterator' ); +* +* var arr = array2iterator( [ true, true, true, false, true ] ); +* +* var it = iterCuEvery( arr ); +* +* var v = it.next().value; +* returns true +* +* v = it.next().value; +* returns true +* +* v = it.next().value; +* returns true +* +* v = it.next().value; +* returns false +* +* v = it.next().value; +* returns false +* +* var bool = it.next().done; +* returns true +*/ +declare function iterCuEvery( iterator: Iterator ): Iterator; + + +// EXPORTS // + +export = iterCuEvery; diff --git a/lib/node_modules/@stdlib/iter/cuevery/docs/types/test.ts b/lib/node_modules/@stdlib/iter/cuevery/docs/types/test.ts new file mode 100644 index 00000000000..297181ab950 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cuevery/docs/types/test.ts @@ -0,0 +1,69 @@ +/* +* @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 { Iterator, IteratorResult } from '@stdlib/types/iter'; +import iterCuEvery = require( './index' ); + +/** +* Implements the iterator protocol `next` method. +* +* @returns iterator protocol-compliant object +*/ +function next(): IteratorResult { + return { + 'value': true, + 'done': false + }; +} + +/** +* Returns an iterator protocol-compliant object. +* +* @returns iterator protocol-compliant object +*/ +function iterator(): Iterator { + return { + 'next': next + }; +} + + +// TESTS // + +// The function returns an iterator... +{ + iterCuEvery( iterator() ); // $ExpectType Iterator +} + +// The compiler throws an error if the function is provided a value other than an iterator protocol-compliant object... +{ + iterCuEvery( '5' ); // $ExpectError + iterCuEvery( 5 ); // $ExpectError + iterCuEvery( true ); // $ExpectError + iterCuEvery( false ); // $ExpectError + iterCuEvery( null ); // $ExpectError + iterCuEvery( undefined ); // $ExpectError + iterCuEvery( [] ); // $ExpectError + iterCuEvery( {} ); // $ExpectError + iterCuEvery( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided insufficient arguments... +{ + iterCuEvery(); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/iter/cuevery/examples/index.js b/lib/node_modules/@stdlib/iter/cuevery/examples/index.js new file mode 100644 index 00000000000..aa23bb62973 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cuevery/examples/index.js @@ -0,0 +1,49 @@ +/** +* @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 randu = require( '@stdlib/random/iter/randu' ); +var iterMap = require( '@stdlib/iter/map' ); +var iterCuEvery = require( './../lib' ); + +function threshold( r ) { + return ( r > 0.1 ); +} + +// Create an iterator which generates uniformly distributed pseudorandom numbers: +var opts = { + 'iter': 100 +}; +var riter = randu( opts ); + +// Create an iterator which applies a threshold to generated numbers: +var miter = iterMap( riter, threshold ); + +// Create an iterator which cumulatively tests whether every iterated value is truthy: +var it = iterCuEvery( miter ); + +// Perform manual iteration... +var r; +while ( true ) { + r = it.next(); + if ( r.done ) { + break; + } + console.log( r.value ); +} diff --git a/lib/node_modules/@stdlib/iter/cuevery/lib/index.js b/lib/node_modules/@stdlib/iter/cuevery/lib/index.js new file mode 100644 index 00000000000..cca0d28e523 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cuevery/lib/index.js @@ -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. +*/ + +'use strict'; + +/** +* Create an iterator which cumulatively tests whether every iterated value is truthy. +* +* @module @stdlib/iter/cuevery +* +* @example +* var array2iterator = require( '@stdlib/array/to-iterator' ); +* var iterCuEvery = require( '@stdlib/iter/cuevery' ); +* +* var arr = array2iterator( [ true, true, false, true, false ] ); +* +* var it = iterCuEvery( arr ); +* +* var v = it.next().value; +* // returns true +* +* v = it.next().value; +* // returns true +* +* v = it.next().value; +* // returns false +* +* v = it.next().value; +* // returns false +* +* v = it.next().value; +* // returns false +* +* var bool = it.next().done; +* // returns true +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/iter/cuevery/lib/main.js b/lib/node_modules/@stdlib/iter/cuevery/lib/main.js new file mode 100644 index 00000000000..37f000d2ebb --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cuevery/lib/main.js @@ -0,0 +1,145 @@ +/** +* @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 isFunction = require( '@stdlib/assert/is-function' ); +var isIteratorLike = require( '@stdlib/assert/is-iterator-like' ); +var iteratorSymbol = require( '@stdlib/symbol/iterator' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Returns an iterator which cumulatively tests whether every iterated value is truthy. +* +* @param {Iterator} iterator - input iterator +* @throws {TypeError} first argument must be an iterator +* @returns {Iterator} iterator +* +* @example +* var array2iterator = require( '@stdlib/array/to-iterator' ); +* +* var arr = array2iterator( [ 1, 1, 0, 1, 0 ] ); +* +* var it = iterCuEvery( arr ); +* +* var v = it.next().value; +* // returns true +* +* v = it.next().value; +* // returns true +* +* v = it.next().value; +* // returns false +* +* v = it.next().value; +* // returns false +* +* v = it.next().value; +* // returns false +* +* var bool = it.next().done; +* // returns true +*/ +function iterCuEvery( iterator ) { + var value; + var iter; + var FLG; + if ( !isIteratorLike( iterator ) ) { + throw new TypeError( format( 'invalid argument. Must provide an iterator. Value: `%s`.', iterator ) ); + } + value = true; + + // Create an iterator protocol-compliant object: + iter = {}; + setReadOnly( iter, 'next', next ); + setReadOnly( iter, 'return', end ); + + // If an environment supports `Symbol.iterator` and the provided iterator is iterable, make the iterator iterable: + if ( iteratorSymbol && isFunction( iterator[ iteratorSymbol ] ) ) { + setReadOnly( iter, iteratorSymbol, factory ); + } + return iter; + + /** + * Returns an iterator protocol-compliant object containing the next iterated value. + * + * @private + * @returns {Object} iterator protocol-compliant object + */ + function next() { + var v; + if ( FLG ) { + return { + 'done': true + }; + } + v = iterator.next(); + if ( v.done ) { + FLG = true; + return v; + } + if ( !v.value ) { + value = false; + } + return { + 'value': value, + 'done': false + }; + } + + /** + * Finishes an iterator. + * + * @private + * @param {*} [value] - value to return + * @returns {Object} iterator protocol-compliant object + */ + function end( value ) { + FLG = true; + if ( arguments.length ) { + return { + 'value': value, + 'done': true + }; + } + return { + 'done': true + }; + } + + /** + * Returns a new iterator. + * + * @private + * @returns {Iterator} iterator + */ + function factory() { + return iterCuEvery( iterator[ iteratorSymbol ]() ); + } +} + + +// EXPORTS // + +module.exports = iterCuEvery; diff --git a/lib/node_modules/@stdlib/iter/cuevery/package.json b/lib/node_modules/@stdlib/iter/cuevery/package.json new file mode 100644 index 00000000000..0966ca87f90 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cuevery/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/iter/cuevery", + "version": "0.0.0", + "description": "Create an iterator which cumulatively tests whether every iterated value is truthy.", + "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", + "test", + "every", + "iterate", + "iterator", + "iter", + "validate" + ] +} diff --git a/lib/node_modules/@stdlib/iter/cuevery/test/test.js b/lib/node_modules/@stdlib/iter/cuevery/test/test.js new file mode 100644 index 00000000000..fd18bf673f8 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cuevery/test/test.js @@ -0,0 +1,322 @@ +/** +* @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 proxyquire = require( 'proxyquire' ); +var array2iterator = require( '@stdlib/array/to-iterator' ); +var randu = require( '@stdlib/random/iter/randu' ); +var iteratorSymbol = require( '@stdlib/symbol/iterator' ); +var iterCuEvery = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof iterCuEvery, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if not provided an iterator protocol-compliant object', function test( t ) { + var values; + var i; + + 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 a type error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + iterCuEvery( value ); + }; + } +}); + +tape( 'the function returns an iterator protocol-compliant object', function test( t ) { + var arr; + var it; + var r; + var i; + + arr = array2iterator( [ 0, 0, 1, 0, 1 ] ); + it = iterCuEvery( arr ); + for ( i = 0; i < 5; i++ ) { + r = it.next(); + t.equal( typeof r.value, 'boolean', 'returns expected value' ); + t.equal( typeof r.done, 'boolean', 'returns expected value' ); + } + t.equal( typeof r.done, 'boolean', 'returns expected value' ); + t.end(); +}); + +tape( 'if no upstream iterator values are falsy, the function returns an iterator protocol-compliant object which returns all truthy values', function test( t ) { + var expected; + var actual; + var values; + var it; + var i; + + values = [ 1, 1, 1, 1 ]; + expected = [ + { + 'value': true, + 'done': false + }, + { + 'value': true, + 'done': false + }, + { + 'value': true, + 'done': false + }, + { + 'value': true, + 'done': false + }, + { + 'done': true + } + ]; + + it = iterCuEvery( array2iterator( values ) ); + + actual = []; + for ( i = 0; i < expected.length; i++ ) { + actual.push( it.next() ); + } + t.deepEqual( actual, expected, 'returns expected values' ); + t.end(); +}); + +tape( 'if at least one upstream iterator value is falsy, the function returns an iterator protocol-compliant object which returns falsy values upon encountering the first falsy value', function test( t ) { + var expected; + var actual; + var values; + var it; + var i; + + values = [ 1, 1, 0, 1 ]; + expected = [ + { + 'value': true, + 'done': false + }, + { + 'value': true, + 'done': false + }, + { + 'value': false, + 'done': false + }, + { + 'value': false, + 'done': false + }, + { + 'done': true + } + ]; + + it = iterCuEvery( array2iterator( values ) ); + + actual = []; + for ( i = 0; i < expected.length; i++ ) { + actual.push( it.next() ); + } + t.deepEqual( actual, expected, 'returns expected values' ); + t.end(); +}); + +tape( 'if all upstream iterator values are truthy, the function returns an iterator protocol-compliant object which always returns truthy values', function test( t ) { + var expected; + var actual; + var values; + var it; + var i; + + values = [ 1, 1, 1, 1 ]; + expected = [ + { + 'value': true, + 'done': false + }, + { + 'value': true, + 'done': false + }, + { + 'value': true, + 'done': false + }, + { + 'value': true, + 'done': false + }, + { + 'done': true + } + ]; + + it = iterCuEvery( array2iterator( values ) ); + + actual = []; + for ( i = 0; i < expected.length; i++ ) { + actual.push( it.next() ); + } + t.deepEqual( actual, expected, 'returns expected values' ); + t.end(); +}); + +tape( 'the returned iterator has a `return` method for closing an iterator (no argument)', function test( t ) { + var it; + var r; + + it = iterCuEvery( randu() ); + + r = it.next(); + t.equal( typeof r.value, 'boolean', 'returns expected value' ); + t.equal( r.done, false, 'returns expected value' ); + + r = it.next(); + t.equal( typeof r.value, 'boolean', 'returns expected value' ); + t.equal( r.done, false, 'returns expected value' ); + + r = it.return(); + t.equal( r.value, void 0, 'returns expected value' ); + t.equal( r.done, true, 'returns expected value' ); + + r = it.next(); + t.equal( r.value, void 0, 'returns expected value' ); + t.equal( r.done, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the returned iterator has a `return` method for closing an iterator (argument)', function test( t ) { + var it; + var r; + + it = iterCuEvery( randu() ); + + r = it.next(); + t.equal( typeof r.value, 'boolean', 'returns expected value' ); + t.equal( r.done, false, 'returns expected value' ); + + r = it.next(); + t.equal( typeof r.value, 'boolean', 'returns expected value' ); + t.equal( r.done, false, 'returns expected value' ); + + r = it.return( 'finished' ); + t.equal( r.value, 'finished', 'returns expected value' ); + t.equal( r.done, true, 'returns expected value' ); + + r = it.next(); + t.equal( r.value, void 0, 'returns expected value' ); + t.equal( r.done, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if an environment supports `Symbol.iterator` and the provided iterator is iterable, the returned iterator is iterable', function test( t ) { + var iterCuEvery; + var opts; + var rand; + var it1; + var it2; + var i; + + iterCuEvery = proxyquire( './../lib/main.js', { + '@stdlib/symbol/iterator': '__ITERATOR_SYMBOL__' + }); + + opts = { + 'seed': 12345 + }; + rand = randu( opts ); + rand[ '__ITERATOR_SYMBOL__' ] = factory; + + it1 = iterCuEvery( rand ); + t.equal( typeof it1[ '__ITERATOR_SYMBOL__' ], 'function', 'has method' ); + t.equal( it1[ '__ITERATOR_SYMBOL__' ].length, 0, 'has zero arity' ); + + it2 = it1[ '__ITERATOR_SYMBOL__' ](); + t.equal( typeof it2, 'object', 'returns an object' ); + t.equal( typeof it2.next, 'function', 'has method' ); + t.equal( typeof it2.return, 'function', 'has method' ); + + for ( i = 0; i < 100; i++ ) { + t.equal( it2.next().value, it1.next().value, 'returns expected value' ); + } + t.end(); + + function factory() { + return randu( opts ); + } +}); + +tape( 'if an environment does not support `Symbol.iterator`, the returned iterator is not "iterable"', function test( t ) { + var iterCuEvery; + var it; + + iterCuEvery = proxyquire( './../lib/main.js', { + '@stdlib/symbol/iterator': false + }); + + it = iterCuEvery( randu() ); + t.equal( it[ iteratorSymbol ], void 0, 'does not have property' ); + + t.end(); +}); + +tape( 'if a provided iterator is not iterable, the returned iterator is not iterable', function test( t ) { + var iterCuEvery; + var rand; + var it; + + iterCuEvery = proxyquire( './../lib/main.js', { + '@stdlib/symbol/iterator': '__ITERATOR_SYMBOL__' + }); + + rand = randu(); + rand[ '__ITERATOR_SYMBOL__' ] = null; + + it = iterCuEvery( rand ); + t.equal( it[ iteratorSymbol ], void 0, 'does not have property' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/iter/cunone-by/lib/main.js b/lib/node_modules/@stdlib/iter/cunone-by/lib/main.js index d93c22dc47c..8ee2c6b00b3 100644 --- a/lib/node_modules/@stdlib/iter/cunone-by/lib/main.js +++ b/lib/node_modules/@stdlib/iter/cunone-by/lib/main.js @@ -111,6 +111,7 @@ function iterCuNoneBy( iterator, predicate, thisArg ) { FLG = true; return v; } + i += 1; if ( value && predicate.call( thisArg, v.value, i ) ) { value = false; } diff --git a/lib/node_modules/@stdlib/iter/cusome-by/README.md b/lib/node_modules/@stdlib/iter/cusome-by/README.md new file mode 100644 index 00000000000..95fee4c4755 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cusome-by/README.md @@ -0,0 +1,207 @@ + + +# iterCuSomeBy + +> Create an iterator which cumulatively tests whether at least `n` iterated values pass a test implemented by a predicate function. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var iterCuSomeBy = require( '@stdlib/iter/cusome-by' ); +``` + +#### iterCuSomeBy( iterator, n, predicate\[, thisArg] ) + +Returns an [iterator][mdn-iterator-protocol] which cumulatively tests whether at least `n` iterated values pass a test implemented by a `predicate` function. + +```javascript +var array2iterator = require( '@stdlib/array/to-iterator' ); + +function isPositive( v ) { + return ( v > 0 ); +} + +var arr = array2iterator( [ 0, 0, 0, 1, 1 ] ); + +var it = iterCuSomeBy( arr, 2, isPositive ); + +var v = it.next().value; +// returns false + +v = it.next().value; +// returns false + +v = it.next().value; +// returns false + +v = it.next().value; +// returns false + +v = it.next().value; +// returns true + +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. + +A `predicate` function is provided two arguments: + +- **value**: iterated value +- **index**: iteration index (zero-based) + +To set the `predicate` function execution context, provide a `thisArg`. + + + +```javascript +var array2iterator = require( '@stdlib/array/to-iterator' ); + +function predicate( v ) { + this.count += 1; + return ( v > 0 ); +} + +var arr = array2iterator( [ 0, 0, 1, 1, 1 ] ); + +var ctx = { + 'count': 0 +}; + +var it = iterCuSomeBy( arr, 3, predicate, ctx ); +// returns + +var v = it.next().value; +// returns false + +v = it.next().value; +// returns false + +v = it.next().value; +// returns false + +v = it.next().value; +// returns false + +v = it.next().value; +// returns true + +var count = ctx.count; +// returns 5 +``` + + + + + + + +
+ +## Notes + +- A `predicate` function is invoked for each iterated value until the `nth` truthy `predicate` function return value. The returned iterator continues iterating until it reaches the end of the input iterator, even after the condition is met. + +
+ + + + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/iter/randu' ); +var iterCuSomeBy = require( '@stdlib/iter/cusome-by' ); + +function threshold( r ) { + return ( r > 0.95 ); +} + +// Create an iterator which generates uniformly distributed pseudorandom numbers: +var opts = { + 'iter': 100 +}; +var riter = randu( opts ); + +// Create an iterator which tracks whether at least two values have exceeded the threshold: +var it = iterCuSomeBy( riter, 2, threshold ); + +// Perform manual iteration... +var r; +while ( true ) { + r = it.next(); + if ( r.done ) { + break; + } + console.log( r.value ); +} +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/iter/cusome-by/benchmark/benchmark.js b/lib/node_modules/@stdlib/iter/cusome-by/benchmark/benchmark.js new file mode 100644 index 00000000000..85100e3659b --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cusome-by/benchmark/benchmark.js @@ -0,0 +1,93 @@ +/** +* @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 iterConstant = require( '@stdlib/iter/constant' ); +var isIteratorLike = require( '@stdlib/assert/is-iterator-like' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var iterCuSomeBy = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Predicate function. +* +* @private +* @param {*} value - value +* @returns {boolean} result +*/ + +// MAIN // + +bench( pkg, function benchmark( b ) { + var iter; + var it; + var i; + + function predicate( value ) { + return ( value < 0 ); + } + + it = iterConstant( 3.14 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + iter = iterCuSomeBy( it, 2, predicate ); + 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(); +}); + +bench( pkg+'::iteration', function benchmark( b ) { + var iter; + var v; + var i; + + function predicate( value ) { + return ( value < 0 ); + } + + iter = iterCuSomeBy( iterConstant( 3.14 ), 2, predicate ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = iter.next().value; + if ( !isBoolean( v ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( v ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/iter/cusome-by/docs/repl.txt b/lib/node_modules/@stdlib/iter/cusome-by/docs/repl.txt new file mode 100644 index 00000000000..a2ac3c5d8f3 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cusome-by/docs/repl.txt @@ -0,0 +1,67 @@ + +{{alias}}( iterator, n, predicate[, thisArg] ) + Returns an iterator which cumulatively tests whether at least `n` iterated + values pass a test implemented by a predicate function. + + If an environment supports Symbol.iterator and a provided iterator is + iterable, the returned iterator is iterable. + + The predicate function is provided two arguments: + + - value: iterated value + - index: iteration index + + A predicate function is invoked for each iterated value until the n + truthy predicate function return value. Upon encountering the nth truthy + return value, the returned iterator ceases to invoke the predicate function + and returns `true` for each subsequent iterated value of the provided input + iterator. + + If provided an iterator which does not return any iterated values, the + function returns an iterator which is also empty. + + Parameters + ---------- + iterator: Object + Input iterator. + + n: integer + Number of successful values to check for. + + predicate: Function + The test function. + + thisArg: any (optional) + Execution context. + + 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 arr = {{alias:@stdlib/array/to-iterator}}( [ 0, 0, 0, 1, 1 ] ); + > function fcn( v ) { return ( v > 0 ); }; + > var it = {{alias}}( arr, 2, fcn ); + > var v = it.next().value + false + > v = it.next().value + false + > v = it.next().value + false + > v = it.next().value + false + > v = it.next().value + true + + See Also + -------- diff --git a/lib/node_modules/@stdlib/iter/cusome-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/iter/cusome-by/docs/types/index.d.ts new file mode 100644 index 00000000000..5751a84d315 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cusome-by/docs/types/index.d.ts @@ -0,0 +1,99 @@ +/* +* @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 { TypedIterator, TypedIterableIterator } from '@stdlib/types/iter'; + +// Define a union type representing both iterable and non-iterable iterators: +type Iterator = TypedIterator | TypedIterableIterator; + +/** +* Checks whether an iterated value passes a test. +* +* @returns boolean indicating whether an iterated value passes a test +*/ +type Nullary = ( this: U ) => boolean; + +/** +* Checks whether an iterated value passes a test. +* +* @param value - iterated value +* @returns boolean indicating whether an iterated value passes a test +*/ +type Unary = ( this: U, value: T ) => boolean; + +/** +* Checks whether an iterated value passes a test. +* +* @param value - iterated value +* @param index - iteration index +* @returns boolean indicating whether an iterated value passes a test +*/ +type Binary = ( this: U, value: T, index: number ) => boolean; + +/** +* Checks whether an iterated value passes a test. +* +* @param value - iterated value +* @param index - iteration index +* @returns boolean indicating whether an iterated value passes a test +*/ +type Predicate = Nullary | Unary | Binary; + +/** +* Returns an iterator which cumulatively tests whether at least `n` iterated values pass a test implemented by a predicate function. +* +* @param iterator - source iterator +* @param n - minimum number of truthy elements +* @param predicate - predicate function +* @param thisArg - execution context +* @returns iterator +* +* @example +* var array2iterator = require( '@stdlib/array/to-iterator' ); +* +* function isPositive( v ) { +* return ( v > 0 ); +* } +* +* var it = iterCuSomeBy( array2iterator( [ 0, 0, 0, 1, 1 ] ), 2, isPositive ); +* +* var v = it.next().value; +* // returns false +* +* v = it.next().value; +* // returns false +* +* v = it.next().value; +* // returns false +* +* v = it.next().value; +* // returns false +* +* v = it.next().value; +* // returns true +*/ +declare function iterCuSomeBy( iterator: Iterator, n: number, predicate: Predicate, thisArg?: ThisParameterType> ): Iterator; + + +// EXPORTS // + +export = iterCuSomeBy; diff --git a/lib/node_modules/@stdlib/iter/cusome-by/docs/types/test.ts b/lib/node_modules/@stdlib/iter/cusome-by/docs/types/test.ts new file mode 100644 index 00000000000..fb3ff0120c4 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cusome-by/docs/types/test.ts @@ -0,0 +1,122 @@ +/* +* @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 { TypedIterator, TypedIteratorResult } from '@stdlib/types/iter'; +import iterCuSomeBy = require( './index' ); + +/** +* Implements the iterator protocol `next` method. +* +* @returns iterator protocol-compliant object +*/ +function next(): TypedIteratorResult { + return { + 'value': 2.0, + 'done': false + }; +} + +/** +* Returns an iterator protocol-compliant object. +* +* @returns iterator protocol-compliant object +*/ +function iterator(): TypedIterator { + return { + 'next': next + }; +} + +/** +* Predicate function. +* +* @param _v - iterated value +* @param i - iteration index +* @returns a boolean +*/ +function predicate1( _v: any, i: number ): boolean { + return ( i > 10 ); +} + +/** +* Predicate function. +* +* @param v - iterated value +* @returns a boolean +*/ +function predicate2( v: any ): boolean { + return ( v !== v ); +} + + +// TESTS // + +// The function returns an iterator... +{ + iterCuSomeBy( iterator(), 3, predicate1 ); // $ExpectType Iterator + iterCuSomeBy( iterator(), 3, predicate2 ); // $ExpectType Iterator + iterCuSomeBy( iterator(), 3, predicate1, {} ); // $ExpectType Iterator + iterCuSomeBy( iterator(), 3, predicate2, {} ); // $ExpectType Iterator + iterCuSomeBy( iterator(), 3, predicate1, null ); // $ExpectType Iterator + iterCuSomeBy( iterator(), 3, predicate2, null ); // $ExpectType Iterator +} + +// The compiler throws an error if the function is provided a first argument which is not an iterator protocol-compliant object... +{ + iterCuSomeBy( '5', 3, predicate1 ); // $ExpectError + iterCuSomeBy( 5, 3, predicate1 ); // $ExpectError + iterCuSomeBy( true, 3, predicate1 ); // $ExpectError + iterCuSomeBy( false, 3, predicate1 ); // $ExpectError + iterCuSomeBy( null, 3, predicate1 ); // $ExpectError + iterCuSomeBy( undefined, 3, predicate1 ); // $ExpectError + iterCuSomeBy( [], 3, predicate1 ); // $ExpectError + iterCuSomeBy( {}, 3, predicate1 ); // $ExpectError + iterCuSomeBy( ( x: number ): number => x, 3, predicate1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a number... +{ + iterCuSomeBy( iterator(), '5', predicate1 ); // $ExpectError + iterCuSomeBy( iterator(), true, predicate1 ); // $ExpectError + iterCuSomeBy( iterator(), false, predicate1 ); // $ExpectError + iterCuSomeBy( iterator(), null, predicate1 ); // $ExpectError + iterCuSomeBy( iterator(), undefined, predicate1 ); // $ExpectError + iterCuSomeBy( iterator(), [], predicate1 ); // $ExpectError + iterCuSomeBy( iterator(), {}, predicate1 ); // $ExpectError + iterCuSomeBy( iterator(), ( x: number ): number => x, predicate1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not a predicate function... +{ + iterCuSomeBy( iterator(), 3, '5' ); // $ExpectError + iterCuSomeBy( iterator(), 3, 5 ); // $ExpectError + iterCuSomeBy( iterator(), 3, true ); // $ExpectError + iterCuSomeBy( iterator(), 3, false ); // $ExpectError + iterCuSomeBy( iterator(), 3, null ); // $ExpectError + iterCuSomeBy( iterator(), 3, undefined ); // $ExpectError + iterCuSomeBy( iterator(), 3, [] ); // $ExpectError + iterCuSomeBy( iterator(), 3, {} ); // $ExpectError + iterCuSomeBy( iterator(), 3, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided insufficient arguments... +{ + iterCuSomeBy(); // $ExpectError + iterCuSomeBy( iterator() ); // $ExpectError + iterCuSomeBy( iterator(), 3 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/iter/cusome-by/examples/index.js b/lib/node_modules/@stdlib/iter/cusome-by/examples/index.js new file mode 100644 index 00000000000..b1e304103f6 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cusome-by/examples/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'; + +var randu = require( '@stdlib/random/iter/randu' ); +var iterCuSomeBy = require( './../lib' ); + +function threshold( r ) { + return ( r >= 0.95 ); +} + +// Create an iterator which generates uniformly distributed pseudorandom numbers: +var opts = { + 'iter': 100 +}; +var riter = randu( opts ); + +// Create an iterator which tracks whether at least two values have exceeded the threshold: +var it = iterCuSomeBy( riter, 2, threshold ); + +// Perform manual iteration... +var r; +while ( true ) { + r = it.next(); + if ( r.done ) { + break; + } + console.log( r.value ); +} diff --git a/lib/node_modules/@stdlib/iter/cusome-by/lib/index.js b/lib/node_modules/@stdlib/iter/cusome-by/lib/index.js new file mode 100644 index 00000000000..7811880d97c --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cusome-by/lib/index.js @@ -0,0 +1,65 @@ +/** +* @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 cumulatively tests whether at least `n` iterated values pass a test implemented by a predicate function. +* +* @module @stdlib/iter/cusome-by +* +* @example +* var array2iterator = require( '@stdlib/array/to-iterator' ); +* var iterCuSomeBy = require( '@stdlib/iter/cusome-by' ); +* +* function isPositive( value ) { +* return ( value > 0 ); +* } +* +* var arr = array2iterator( [ 0, 0, 0, 1, 1 ] ); +* +* var it = iterCuSomeBy( arr, 2, isPositive ); +* +* var v = it.next().value; +* // returns false +* +* v = it.next().value; +* // returns false +* +* v = it.next().value; +* // returns false +* +* v = it.next().value; +* // returns false +* +* v = it.next().value; +* // returns true +* +* var bool = it.next().done; +* // returns true +*/ + + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/iter/cusome-by/lib/main.js b/lib/node_modules/@stdlib/iter/cusome-by/lib/main.js new file mode 100644 index 00000000000..293cabd74e5 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cusome-by/lib/main.js @@ -0,0 +1,174 @@ +/** +* @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 isIteratorLike = require( '@stdlib/assert/is-iterator-like' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive; +var format = require( '@stdlib/string/format' ); +var iteratorSymbol = require( '@stdlib/symbol/iterator' ); + + +// MAIN // + +/** +* Returns an iterator which cumulatively tests whether at least `n` iterated values pass a test implemented by a predicate function. +* +* @param {Iterator} iterator - input iterator +* @param {number} n - the number of items +* @param {Function} predicate - predicate function +* @param {*} [thisArg] - execution context +* @throws {TypeError} first argument must be an iterator +* @throws {TypeError} second argument must be a positive integer +* @throws {TypeError} third argument must be a predicate function +* @returns {Iterator} iterator +* +* @example +* var array2iterator = require( '@stdlib/array/to-iterator' ); +* +* function isPositive( value ) { +* return ( value > 0 ); +* } +* +* var arr = array2iterator( [ 0, 0, 0, 1, 1 ] ); +* +* var it = iterCuSomeBy( arr, 2, isPositive ); +* +* var v = it.next().value; +* // returns false +* +* v = it.next().value; +* // returns false +* +* v = it.next().value; +* // returns false +* +* v = it.next().value; +* // returns false +* +* v = it.next().value; +* // returns true +* +* var bool = it.next().done; +* // returns true +*/ +function iterCuSomeBy( iterator, n, predicate, thisArg ) { + var count; + var value; + var iter; + var FLG; + var i; + if ( !isIteratorLike( iterator ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an iterator. Value: `%s`.', iterator ) ); + } + if ( !isPositiveInteger( n ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be a positive integer. Value: `%s`.', n ) ); + } + if ( !isFunction( predicate ) ) { + throw new TypeError( format( 'invalid argument. Third argument must be a function. Value: `%s`.', predicate ) ); + } + count = 0; + i = -1; + value = false; + + // Create an iterator protocol-compliant object: + iter = {}; + setReadOnly( iter, 'next', next ); + setReadOnly( iter, 'return', end ); + + // If an environment supports `Symbol.iterator` and the provided iterator is iterable, make the iterator iterable: + if ( iteratorSymbol && isFunction( iterator[ iteratorSymbol ] ) ) { + setReadOnly( iter, iteratorSymbol, factory ); + } + return iter; + + /** + * Returns an iterator protocol-compliant object containing the next iterated value. + * + * @private + * @param {*} [value] - value to return + * @returns {Object} iterator protocol-compliant object + */ + function next() { + var v; + if ( FLG ) { + return { + 'done': true + }; + } + v = iterator.next(); + if ( v.done ) { + FLG = true; + return v; + } + i += 1; + if ( !value && predicate.call( thisArg, v.value, i ) ) { + count += 1; + if ( count === n ) { + value = true; + return { + 'value': true, + 'done': false + }; + } + } + return { + 'value': value, + 'done': false + }; + } + + /** + * Finishes an iterator. + * + * @private + * @param {*} [value] - value to return + * @returns {Object} iterator protocol-compliant object + */ + function end( value ) { + FLG = true; + if ( arguments.length ) { + return { + 'value': value, + 'done': true + }; + } + return { + 'done': true + }; + } + + /** + * Returns a new iterator. + * + * @private + * @returns {Iterator} iterator + */ + function factory() { + return iterCuSomeBy( iterator[ iteratorSymbol ](), n, predicate, thisArg ); + } +} + + +// EXPORTS // + +module.exports = iterCuSomeBy; diff --git a/lib/node_modules/@stdlib/iter/cusome-by/package.json b/lib/node_modules/@stdlib/iter/cusome-by/package.json new file mode 100644 index 00000000000..baddd3c9508 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cusome-by/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/iter/cusome-by", + "version": "0.0.0", + "description": "Create an iterator which cumulatively tests whether at least `n` iterated values pass a test implemented by a predicate function.", + "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", + "test", + "some", + "any", + "every", + "all", + "iterate", + "iterator", + "iter", + "validate", + "predicate", + "cumulative", + "cusome", + "transform" + ] +} diff --git a/lib/node_modules/@stdlib/iter/cusome-by/test/test.js b/lib/node_modules/@stdlib/iter/cusome-by/test/test.js new file mode 100644 index 00000000000..cc8f1df03d3 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cusome-by/test/test.js @@ -0,0 +1,377 @@ +/** +* @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 array2iterator = require( '@stdlib/array/to-iterator' ); +var noop = require( '@stdlib/utils/noop' ); +var iterCuSomeBy = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Predicate function. +* +* @private +* @param {*} value - value +* @returns {boolean} result +*/ +function predicate( value ) { + return ( value > 0 ); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof iterCuSomeBy, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if not provided an iterator', function test( t ) { + var values; + var i; + + 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 a type error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + iterCuSomeBy( value, noop ); + }; + } +}); + +tape( 'the function throws an error if not provided a positive integer as a second argument', function test( t ) { + var values; + var i; + + values = [ + '5', + -5, + 0, + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + iterCuSomeBy( array2iterator( [ 1, 2, 3 ] ), value, noop ); + }; + } +}); + +tape( 'the function throws an error if not provided a predicate function as a third argument', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + iterCuSomeBy( array2iterator( [ 1, 2, 3 ] ), 3, value ); + }; + } +}); + +tape( 'the function returns an iterator protocol-compliant object', function test( t ) { + var arr; + var it; + var r; + var i; + + arr = array2iterator( [ 0, 0, 1, 0, 1 ] ); + it = iterCuSomeBy( arr, 3, predicate ); + for ( i = 0; i < 5; i++ ) { + r = it.next(); + t.equal( typeof r.value, 'boolean', 'returns expected value' ); + t.equal( typeof r.done, 'boolean', 'returns expected value' ); + } + t.equal( typeof r.done, 'boolean', 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an "empty" iterator, the function returns an iterator which is also empty', function test( t ) { + var arr; + var it; + var v; + + arr = array2iterator( [] ); + it = iterCuSomeBy( arr, 3, predicate ); + + v = it.next(); + t.strictEqual( v.done, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an iterator which cumulatively tests whether at least `n` iterated values pass a test', function test( t ) { + var expected; + var actual; + var values; + var it; + var i; + + values = [ 0, 0, 1, 1, 0, 0 ]; + expected = [ + { + 'value': false, + 'done': false + }, + { + 'value': false, + 'done': false + }, + { + 'value': false, + 'done': false + }, + { + 'value': true, + 'done': false + }, + { + 'value': true, + 'done': false + }, + { + 'value': true, + 'done': false + }, + { + 'done': true + } + ]; + + it = iterCuSomeBy( array2iterator( values ), 2, predicate ); + + actual = []; + for ( i = 0; i < expected.length; i++ ) { + actual.push( it.next() ); + } + t.deepEqual( actual, expected, 'returns expected values' ); + t.end(); +}); + +tape( 'the function returns an iterator which cumulatively tests whether at least `n` iterated values pass a test', function test( t ) { + var expected; + var values; + var it; + var v; + var i; + + values = [ 0, 0, 1, 1, 0, 1 ]; + expected = [ false, false, false, true, true, true ]; + + it = iterCuSomeBy( array2iterator( values ), 2, predicate ); + t.equal( typeof it.next, 'function', 'has next method' ); + + for ( i = 0; i < values.length; i++ ) { + v = it.next(); + t.equal( v.value, expected[i], 'returns expected value' ); + t.equal( v.done, false, 'returns expected value' ); + } + v = it.next(); + t.equal( v.value, undefined, 'returns expected value' ); + t.equal( v.done, true, 'returns expected value' ); + + values = [ 0, 0, 1, 1, 0, 1 ]; + expected = [ false, false, false, false, false, true ]; + + it = iterCuSomeBy( array2iterator( values ), 3, predicate ); + t.equal( typeof it.next, 'function', 'has next method' ); + + for ( i = 0; i < values.length; i++ ) { + v = it.next(); + t.equal( v.value, expected[i], 'returns expected value' ); + t.equal( v.done, false, 'returns expected value' ); + } + v = it.next(); + t.equal( v.value, undefined, 'returns expected value' ); + t.equal( v.done, true, 'returns expected value' ); + + t.end(); + + function predicate( v ) { + return ( v > 0 ); + } +}); + +tape( 'the returned iterator has a `return` method for closing an iterator (no argument)', function test( t ) { + var it; + var r; + + it = iterCuSomeBy( array2iterator( [ 1, 1, 1, 0, 0 ] ), 3, predicate ); + + r = it.next(); + t.equal( typeof r.value, 'boolean', 'returns a boolean' ); + t.equal( r.done, false, 'returns expected value' ); + + r = it.next(); + t.equal( typeof r.value, 'boolean', 'returns a boolean' ); + t.equal( r.done, false, 'returns expected value' ); + + r = it.next(); + t.equal( typeof r.value, 'boolean', 'returns expected value' ); + t.equal( r.done, false, 'returns expected value' ); + + r = it.next(); + t.equal( typeof r.value, 'boolean', 'returns expected value' ); + t.equal( r.done, false, 'returns expected value' ); + + r = it.next(); + t.equal( typeof r.value, 'boolean', 'returns expected value' ); + t.equal( r.done, false, 'returns expected value' ); + + r = it.next(); + t.equal( r.done, true, 'returns expected value' ); + + t.end(); + + function predicate( v ) { + return ( v > 0 ); + } +}); + +tape( 'the returned iterator has a `return` method for closing an iterator (argument)', function test( t ) { + var it; + var r; + + it = iterCuSomeBy( array2iterator( [ 1, 1, 1, 0, 0 ] ), 3, predicate ); + + r = it.next(); + t.equal( typeof r.value, 'boolean', 'returns a boolean' ); + t.equal( r.done, false, 'returns expected value' ); + + r = it.next(); + t.equal( typeof r.value, 'boolean', 'returns a boolean' ); + t.equal( r.done, false, 'returns expected value' ); + + r = it.return( 'finished' ); + t.equal( r.value, 'finished', 'returns expected value' ); + t.equal( r.done, true, 'returns expected value' ); + + r = it.next(); + t.equal( r.value, void 0, 'returns expected value' ); + t.equal( r.done, true, 'returns expected value' ); + + r = it.next(); + t.equal( r.value, void 0, 'returns expected value' ); + t.equal( r.done, true, 'returns expected value' ); + + t.end(); + + function predicate( v ) { + return ( v > 0 ); + } +}); + +tape( 'if an environment supports `Symbol.iterator`, the returned iterator is iterable', function test( t ) { + var iterCuSomeBy; + var it1; + var it2; + var i; + + iterCuSomeBy = require( './../lib' ); + + it1 = iterCuSomeBy( array2iterator( [ 1, 1, 0, 0, 1 ] ), 3, predicate ); + t.equal( typeof it1[ Symbol.iterator ], 'function', 'has method' ); + + it2 = it1[ Symbol.iterator ](); + t.equal( typeof it2, 'object', 'returns an object' ); + t.equal( typeof it2.next, 'function', 'has method' ); + t.equal( typeof it2.return, 'function', 'has method' ); + + for ( i = 0; i < 6; i++ ) { + t.equal( it2.next().value, it1.next().value, 'returns expected value' ); + } + t.end(); + + function predicate( v ) { + return ( v > 0 ); + } +}); + +tape( 'the function supports providing an execution context', function test( t ) { + var ctx; + var it; + var i; + + ctx = { + 'count': 0 + }; + + it = iterCuSomeBy( array2iterator( [ 1, 1, 0, 1, 1 ] ), 3, predicate, ctx ); + for ( i = 0; i < 5; i++ ) { + it.next(); + } + + t.strictEqual( ctx.count, 4, 'returns expected value' ); + t.end(); + + function predicate( v ) { + this.count += 1; // eslint-disable-line no-invalid-this + return ( v > 0 ); + } +});