From 88b80ebf2f695cce2917d70fc33cf7b4d84d6bb6 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 21 Jun 2024 18:40:56 +0000 Subject: [PATCH] Auto-generated commit --- CHANGELOG.md | 2 + bool/README.md | 104 ++++++++++++ bool/benchmark/benchmark.every.js | 55 +++++++ bool/benchmark/benchmark.every.length.js | 116 ++++++++++++++ bool/benchmark/benchmark.some.js | 55 +++++++ bool/benchmark/benchmark.some.length.js | 117 ++++++++++++++ bool/docs/repl.txt | 68 +++++++- bool/docs/types/index.d.ts | 46 ++++++ bool/lib/main.js | 90 +++++++++++ bool/test/test.every.js | 173 ++++++++++++++++++++ bool/test/test.some.js | 195 +++++++++++++++++++++++ 11 files changed, 1020 insertions(+), 1 deletion(-) create mode 100644 bool/benchmark/benchmark.every.js create mode 100644 bool/benchmark/benchmark.every.length.js create mode 100644 bool/benchmark/benchmark.some.js create mode 100644 bool/benchmark/benchmark.some.length.js create mode 100644 bool/test/test.every.js create mode 100644 bool/test/test.some.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b0b429f..d7384f63 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1215,6 +1215,7 @@ This release closes the following issue: ##### Features +- [`42c67e7`](https://github.com/stdlib-js/stdlib/commit/42c67e76cdf919e4e43ff9333d9acc6177eb5558) - add `every` and `some` methods to `array/bool` [(#2421)](https://github.com/stdlib-js/stdlib/pull/2421) - [`0b3db04`](https://github.com/stdlib-js/stdlib/commit/0b3db0401bd16df7aeccb500d8280c280a394762) - add `toSorted` method to `array/bool` [(#2413)](https://github.com/stdlib-js/stdlib/pull/2413) - [`de50d0a`](https://github.com/stdlib-js/stdlib/commit/de50d0af5f4b9a466a87be81da737fdbed48dbf3) - add `reverse` and `toReversed` methods to `array/bool` [(#2390)](https://github.com/stdlib-js/stdlib/pull/2390) - [`5cd4a70`](https://github.com/stdlib-js/stdlib/commit/5cd4a70beaa7663d2a822b0922b3fb3cc6ec539f) - add `findIndex` and `findLastIndex` methods to `array/bool` [(#2384)](https://github.com/stdlib-js/stdlib/pull/2384) @@ -2163,6 +2164,7 @@ A total of 13 people contributed to this release. Thank you to the following con
+- [`42c67e7`](https://github.com/stdlib-js/stdlib/commit/42c67e76cdf919e4e43ff9333d9acc6177eb5558) - **feat:** add `every` and `some` methods to `array/bool` [(#2421)](https://github.com/stdlib-js/stdlib/pull/2421) _(by Jaysukh Makvana, Athan Reines)_ - [`d301be9`](https://github.com/stdlib-js/stdlib/commit/d301be9e2cabe07efe219c00d10aebd15e0673e7) - **fix:** ensure support for real-to-complex casting in boolean and mask array indexing _(by Athan Reines)_ - [`0b727a5`](https://github.com/stdlib-js/stdlib/commit/0b727a5d922225d23693e456b5f7b5b82f63750a) - **feat:** add `mskput` to namespace _(by Athan Reines)_ - [`201ce11`](https://github.com/stdlib-js/stdlib/commit/201ce11a0985502cfc82891fda3fe3b2d656afef) - **feat:** add `array/mskput` _(by Athan Reines)_ diff --git a/bool/README.md b/bool/README.md index 9e133144..96088469 100644 --- a/bool/README.md +++ b/bool/README.md @@ -334,6 +334,58 @@ var len = arr.length; // returns 4 ``` + + +#### BooleanArray.prototype.every( predicate\[, thisArg] ) + +Returns a boolean indicating whether all elements pass a test. + +```javascript +function predicate( v ) { + return v === true; +} + +var arr = new BooleanArray( 3 ); + +arr.set( true, 0 ); +arr.set( true, 1 ); +arr.set( true, 2 ); + +var bool = arr.every( predicate ); +// returns true +``` + +The `predicate` function is provided three arguments: + +- **value**: current array element. +- **index**: current array element index. +- **arr**: the array on which this method was called. + +To set the function execution context, provide a `thisArg`. + +```javascript +function predicate( v ) { + this.count += 1; + return v === true; +} + +var arr = new BooleanArray( 3 ); + +var context = { + 'count': 0 +}; + +arr.set( true, 0 ); +arr.set( true, 1 ); +arr.set( true, 2 ); + +var bool = arr.every( predicate, context ); +// returns true + +var count = context.count; +// returns 3 +``` + #### BooleanArray.prototype.find( predicate\[, thisArg] ) @@ -715,6 +767,58 @@ A few notes: - If a target array cannot accommodate all values (i.e., the length of source array plus `i` exceeds the target array length), the method throws an error. - If provided a [typed array][@stdlib/array/typed] which shares an [`ArrayBuffer`][@stdlib/array/buffer] with the target array, the method will intelligently copy the source range to the destination range. + + +#### BooleanArray.prototype.some( predicate\[, thisArg] ) + +Returns a boolean indicating whether at least one element passes a test. + +```javascript +function predicate( v ) { + return v === true; +} + +var arr = new BooleanArray( 3 ); + +arr.set( false, 0 ); +arr.set( true, 1 ); +arr.set( false, 2 ); + +var bool = arr.some( predicate ); +// returns true +``` + +The `predicate` function is provided three arguments: + +- **value**: current array element. +- **index**: current array element index. +- **arr**: the array on which this method was called. + +To set the function execution context, provide a `thisArg`. + +```javascript +function predicate( v ) { + this.count += 1; + return v === true; +} + +var arr = new BooleanArray( 3 ); + +var context = { + 'count': 0 +}; + +arr.set( false, 0 ); +arr.set( true, 1 ); +arr.set( false, 2 ); + +var bool = arr.some( predicate, context ); +// returns true + +var count = context.count; +// returns 2 +``` + #### BooleanArray.prototype.sort( \[compareFcn] ) diff --git a/bool/benchmark/benchmark.every.js b/bool/benchmark/benchmark.every.js new file mode 100644 index 00000000..31de6173 --- /dev/null +++ b/bool/benchmark/benchmark.every.js @@ -0,0 +1,55 @@ +/** +* @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 isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var BooleanArray = require( './../lib' ); + + +// MAIN // + +bench( pkg+':every', function benchmark( b ) { + var bool; + var arr; + var i; + + arr = new BooleanArray( [ true, false, false, true ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = arr.every( predicate ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function predicate( v ) { + return v === true; + } +}); diff --git a/bool/benchmark/benchmark.every.length.js b/bool/benchmark/benchmark.every.length.js new file mode 100644 index 00000000..b42918f8 --- /dev/null +++ b/bool/benchmark/benchmark.every.length.js @@ -0,0 +1,116 @@ +/** +* @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 isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pow = require( '@stdlib/math/base/special/pow' ); +var Boolean = require( '@stdlib/boolean/ctor' ); +var pkg = require( './../package.json' ).name; +var BooleanArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Predicate function. +* +* @private +* @param {boolean} value - array element +* @param {NonNegativeInteger} idx - array element index +* @param {BooleanArray} arr - array instance +* @returns {boolean} boolean indicating whether a value passes a test +*/ +function predicate( value ) { + return value === true; +} + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr; + var i; + + arr = []; + for ( i = 0; i < len; i++ ) { + arr.push( Boolean( 1 ) ); + } + arr = new BooleanArray( arr ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = arr.every( predicate ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':every:len='+len, f ); + } +} + +main(); diff --git a/bool/benchmark/benchmark.some.js b/bool/benchmark/benchmark.some.js new file mode 100644 index 00000000..5024c006 --- /dev/null +++ b/bool/benchmark/benchmark.some.js @@ -0,0 +1,55 @@ +/** +* @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 isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var BooleanArray = require( './../lib' ); + + +// MAIN // + +bench( pkg+':every', function benchmark( b ) { + var bool; + var arr; + var i; + + arr = new BooleanArray( [ true, false, false, true ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = arr.some( predicate ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function predicate( v ) { + return v === true; + } +}); diff --git a/bool/benchmark/benchmark.some.length.js b/bool/benchmark/benchmark.some.length.js new file mode 100644 index 00000000..4cf28e18 --- /dev/null +++ b/bool/benchmark/benchmark.some.length.js @@ -0,0 +1,117 @@ +/** +* @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 isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pow = require( '@stdlib/math/base/special/pow' ); +var Boolean = require( '@stdlib/boolean/ctor' ); +var pkg = require( './../package.json' ).name; +var BooleanArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Predicate function. +* +* @private +* @param {boolean} value - array element +* @param {NonNegativeInteger} idx - array element index +* @param {BooleanArray} arr - array instance +* @returns {boolean} boolean indicating whether a value passes a test +*/ +function predicate( value ) { + return value === true; +} + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr; + var i; + + arr = []; + for ( i = 1; i < len; i++ ) { + arr.push( Boolean( 0 ) ); + } + arr.push( Boolean( 1 ) ); + arr = new BooleanArray( arr ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = arr.some( predicate ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':some:len='+len, f ); + } +} + +main(); diff --git a/bool/docs/repl.txt b/bool/docs/repl.txt index 6760579d..61c78393 100644 --- a/bool/docs/repl.txt +++ b/bool/docs/repl.txt @@ -286,6 +286,39 @@ 10 +{{alias}}.prototype.every( predicate[, thisArg] ) + Returns a boolean indicating whether all elements in the array pass a test. + + A predicate function is provided the following arguments: + + - value: current array element. + - index: current array element index. + - arr: the array on which the method was called. + + Parameters + ---------- + predicate: Function + Predicate function which tests array elements. If a predicate function + returns a truthy value, an array element passes; otherwise, an array + element fails. + + thisArg: Any (optional) + Execution context. + + Returns + ------- + bool: boolean + Boolean indicating whether all elements pass the test. + + Examples + -------- + > function predicate( v ) { return v === true; }; + > var arr = new {{alias}}( [ true, true, true ] ) + + > var bool = arr.every( predicate ) + true + + {{alias}}.prototype.find( predicate[, thisArg] ) Returns the first element in an array for which a predicate function returns a truthy value. @@ -527,7 +560,7 @@ Parameters ---------- v: bool|BooleanArray|ArrayLikeObject - Complex number or complex number array. + Boolean value or Boolean value array. i: integer (optional) Array index at which to start setting elements. Default: 0. @@ -544,6 +577,39 @@ true +{{alias}}.prototype.some( predicate[, thisArg] ) + Returns a boolean indicating whether at least one element passes a test. + + A predicate function is provided the following arguments: + + - value: current array element. + - index: current array element index. + - arr: the array on which the method was called. + + Parameters + ---------- + predicate: Function + Predicate function which tests array elements. If a predicate function + returns a truthy value, an array element passes; otherwise, an array + element fails. + + thisArg: Any (optional) + Execution context. + + Returns + ------- + bool: boolean + Boolean indicating whether at least one element passes the test. + + Examples + -------- + > function predicate( v ) { return v === true; }; + > var arr = new {{alias}}( [ false, true, false ] ) + + > var bool = arr.some( predicate ) + true + + {{alias}}.prototype.sort( [compareFunction] ) Sorts an array in-place. diff --git a/bool/docs/types/index.d.ts b/bool/docs/types/index.d.ts index 6dee9708..8ed7c9f2 100644 --- a/bool/docs/types/index.d.ts +++ b/bool/docs/types/index.d.ts @@ -271,6 +271,29 @@ declare class BooleanArray implements BooleanArrayInterface { */ readonly length: number; + /** + * Tests whether all elements in an array pass a test implemented by a predicate function. + * + * @param predicate - predicate function + * @param thisArg - predicate function execution context + * @returns boolean indicating whether all elements pass a test + * + * @example + * function predicate( v ) { + * return v === true; + * } + * + * var arr = new BooleanArray( 3 ); + * + * arr.set( true, 0 ); + * arr.set( true, 1 ); + * arr.set( true, 2 ); + * + * var bool = arr.every( predicate ); + * // returns true + */ + every( predicate: Predicate, thisArg?: ThisParameterType> ): boolean; + /** * Returns the first element in an array for which a predicate function returns a truthy value. * @@ -484,6 +507,29 @@ declare class BooleanArray implements BooleanArrayInterface { */ set( value: ArrayLike | any, i?: number ): void; + /** + * Tests whether at least one element in an array passes a test implemented by a predicate function. + * + * @param predicate - predicate function + * @param thisArg - predicate function execution context + * @returns boolean indicating whether at least one element passes a test + * + * @example + * function predicate( v ) { + * return v === true; + * } + * + * var arr = new BooleanArray( 3 ); + * + * arr.set( false, 0 ); + * arr.set( true, 1 ); + * arr.set( false, 2 ); + * + * var bool = arr.some( predicate ); + * // returns true + */ + some( predicate: Predicate, thisArg?: ThisParameterType> ): boolean; + /** * Sorts an array in-place. * diff --git a/bool/lib/main.js b/bool/lib/main.js index ad68b1f3..6a3e4ea2 100644 --- a/bool/lib/main.js +++ b/bool/lib/main.js @@ -449,6 +449,51 @@ setReadOnlyAccessor( BooleanArray.prototype, 'byteOffset', function get() { */ setReadOnly( BooleanArray.prototype, 'BYTES_PER_ELEMENT', BooleanArray.BYTES_PER_ELEMENT ); +/** +* Tests whether all elements in an array pass a test implemented by a predicate function. +* +* @name every +* @memberof BooleanArray.prototype +* @type {Function} +* @param {Function} predicate - predicate function +* @param {*} [thisArg] - predicate function execution context +* @throws {TypeError} `this` must be a boolean array +* @throws {TypeError} first argument must be a function +* @returns {boolean} boolean indicating whether all elements pass a test +* +* @example +* function predicate( v ) { +* return v === true; +* } +* +* var arr = new BooleanArray( 3 ); +* +* arr.set( true, 0 ); +* arr.set( true, 1 ); +* arr.set( true, 2 ); +* +* var bool = arr.every( predicate ); +* // returns true +*/ +setReadOnly( BooleanArray.prototype, 'every', function every( predicate, thisArg ) { + var buf; + var i; + + if ( !isBooleanArray( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a boolean array.' ); + } + if ( !isFunction( predicate ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) ); + } + buf = this._buffer; + for ( i = 0; i < this._length; i++ ) { + if ( !predicate.call( thisArg, Boolean( buf[ i ] ), i, this ) ) { + return false; + } + } + return true; +}); + /** * Returns the first element in an array for which a predicate function returns a truthy value. * @@ -899,6 +944,51 @@ setReadOnly( BooleanArray.prototype, 'set', function set( value ) { buf[ idx ] = ( value ) ? 1 : 0; }); +/** +* Tests whether at least one element in an array passes a test implemented by a predicate function. +* +* @name some +* @memberof BooleanArray.prototype +* @type {Function} +* @param {Function} predicate - predicate function +* @param {*} [thisArg] - predicate function execution context +* @throws {TypeError} `this` must be a boolean array +* @throws {TypeError} first argument must be a function +* @returns {boolean} boolean indicating whether at least one element passes a test +* +* @example +* function predicate( v ) { +* return v === true; +* } +* +* var arr = new BooleanArray( 3 ); +* +* arr.set( false, 0 ); +* arr.set( true, 1 ); +* arr.set( false, 2 ); +* +* var bool = arr.some( predicate ); +* // returns true +*/ +setReadOnly( BooleanArray.prototype, 'some', function some( predicate, thisArg ) { + var buf; + var i; + + if ( !isBooleanArray( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a boolean array.' ); + } + if ( !isFunction( predicate ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) ); + } + buf = this._buffer; + for ( i = 0; i < this._length; i++ ) { + if ( predicate.call( thisArg, Boolean( buf[ i ] ), i, this ) ) { + return true; + } + } + return false; +}); + /** * Sorts an array in-place. * diff --git a/bool/test/test.every.js b/bool/test/test.every.js new file mode 100644 index 00000000..e9162d94 --- /dev/null +++ b/bool/test/test.every.js @@ -0,0 +1,173 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var BooleanArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof BooleanArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is an `every` method for returning boolean indicating whether all elements pass a test', function test( t ) { + t.strictEqual( hasOwnProp( BooleanArray.prototype, 'every' ), true, 'has property' ); + t.strictEqual( isFunction( BooleanArray.prototype.every ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a boolean array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new BooleanArray( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.every.call( value, predicate ); + }; + } + + function predicate( v ) { + return v === true; + } +}); + +tape( 'the method throws an error if provided a first argument which is not a function', function test( t ) { + var values; + var arr; + var i; + + arr = new BooleanArray( 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.every( value ); + }; + } +}); + +tape( 'the method returns `true` if operating on an empty boolean array', function test( t ) { + var bool; + var arr; + + arr = new BooleanArray(); + bool = arr.every( predicate ); + + t.strictEqual( bool, true, 'returns expected value' ); + t.end(); + + function predicate() { + t.fail( 'should not be invoked' ); + } +}); + +tape( 'the method returns `true` if all elements pass a test', function test( t ) { + var bool; + var arr; + + arr = new BooleanArray( [ true, true, true, true ] ); + bool = arr.every( predicate ); + + t.strictEqual( bool, true, 'returns expected value' ); + t.end(); + + function predicate( v ) { + return v === true; + } +}); + +tape( 'the method returns `false` if one or more elements fail a test', function test( t ) { + var bool; + var arr; + + arr = new BooleanArray( [ true, false, true, false ] ); + bool = arr.every( predicate ); + + t.strictEqual( bool, false, 'returns expected value' ); + t.end(); + + function predicate( v ) { + return v === true; + } +}); + +tape( 'the method supports providing an execution context', function test( t ) { + var bool; + var ctx; + var arr; + + ctx = { + 'count': 0 + }; + arr = new BooleanArray( [ true, true, true, true ] ); + bool = arr.every( predicate, ctx ); + + t.strictEqual( bool, true, 'returns expected value' ); + t.strictEqual( ctx.count, 4, 'returns expected value'); + + t.end(); + + function predicate( v ) { + this.count += 1; // eslint-disable-line no-invalid-this + return v === true; + } +}); diff --git a/bool/test/test.some.js b/bool/test/test.some.js new file mode 100644 index 00000000..81b09922 --- /dev/null +++ b/bool/test/test.some.js @@ -0,0 +1,195 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var BooleanArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof BooleanArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `some` method for returning boolean indicating whether at least one element passes a test', function test( t ) { + t.strictEqual( hasOwnProp( BooleanArray.prototype, 'some' ), true, 'has property' ); + t.strictEqual( isFunction( BooleanArray.prototype.some ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a boolean array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new BooleanArray( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.some.call( value, predicate ); + }; + } + + function predicate( v ) { + return v === true; + } +}); + +tape( 'the method throws an error if provided a first argument which is not a function', function test( t ) { + var values; + var arr; + var i; + + arr = new BooleanArray( 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.some( value ); + }; + } +}); + +tape( 'the method returns `false` if operating on an empty boolean array', function test( t ) { + var bool; + var arr; + + arr = new BooleanArray(); + bool = arr.some( predicate ); + + t.strictEqual( bool, false, 'returns expected value' ); + t.end(); + + function predicate() { + t.fail( 'should not be invoked' ); + } +}); + +tape( 'the method returns `true` if at least one element passes a test', function test( t ) { + var bool; + var arr; + + arr = new BooleanArray( [ false, true, false, true ] ); + bool = arr.some( predicate ); + + t.strictEqual( bool, true, 'returns expected value' ); + t.end(); + + function predicate( v ) { + return v === true; + } +}); + +tape( 'the method returns `false` if all elements fail a test', function test( t ) { + var bool; + var arr; + + arr = new BooleanArray( [ false, false, false, false ] ); + bool = arr.some( predicate ); + + t.strictEqual( bool, false, 'returns expected value' ); + t.end(); + + function predicate( v ) { + return v === true; + } +}); + +tape( 'the method supports providing an execution context', function test( t ) { + var bool; + var ctx; + var arr; + + ctx = { + 'count': 0 + }; + arr = new BooleanArray( [ false, true, false, true ] ); + bool = arr.some( predicate, ctx ); + + t.strictEqual( bool, true, 'returns expected value' ); + t.strictEqual( ctx.count, 2, 'returns expected value'); + + t.end(); + + function predicate( v ) { + this.count += 1; // eslint-disable-line no-invalid-this + return v === true; + } +}); + +tape( 'the method stops executing upon encountering the first element which passes a test', function test( t ) { + var bool; + var ctx; + var arr; + + ctx = { + 'count': 0 + }; + arr = new BooleanArray( [ false, true, true, false ] ); + bool = arr.some( predicate, ctx ); + + t.strictEqual( bool, true, 'returns expected value' ); + t.strictEqual( ctx.count, 2, 'returns expected value'); + + t.end(); + + function predicate( v ) { + this.count += 1; // eslint-disable-line no-invalid-this + return v === true; + } +});