From 7e8ba39359834611e1bf3283a33925a381574427 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 23 Jun 2024 19:29:49 +0000 Subject: [PATCH] Auto-generated commit --- CHANGELOG.md | 4 +- bool/README.md | 22 ++ bool/benchmark/benchmark.includes.js | 56 +++++ bool/benchmark/benchmark.includes.length.js | 103 +++++++++ bool/docs/repl.txt | 30 ++- bool/docs/types/index.d.ts | 24 +++ bool/lib/main.js | 60 ++++++ bool/test/test.includes.js | 221 ++++++++++++++++++++ 8 files changed, 518 insertions(+), 2 deletions(-) create mode 100644 bool/benchmark/benchmark.includes.js create mode 100644 bool/benchmark/benchmark.includes.length.js create mode 100644 bool/test/test.includes.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 97e5d7d5..749eccaf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@
-## Unreleased (2024-06-22) +## Unreleased (2024-06-23)
@@ -1282,6 +1282,7 @@ This release closes the following issue: ##### Features +- [`fbc42b4`](https://github.com/stdlib-js/stdlib/commit/fbc42b4c66cf695c6c114f64bf3eff65186026f0) - add `includes` method to `array/bool` [(#2441)](https://github.com/stdlib-js/stdlib/pull/2441) - [`c58e9e4`](https://github.com/stdlib-js/stdlib/commit/c58e9e4dce4361b4ae7454eca926b0e00afb15aa) - add `indexOf` and `lastIndexOf` methods to `array/bool` [(#2432)](https://github.com/stdlib-js/stdlib/pull/2432) - [`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) @@ -2255,6 +2256,7 @@ A total of 13 people contributed to this release. Thank you to the following con
+- [`fbc42b4`](https://github.com/stdlib-js/stdlib/commit/fbc42b4c66cf695c6c114f64bf3eff65186026f0) - **feat:** add `includes` method to `array/bool` [(#2441)](https://github.com/stdlib-js/stdlib/pull/2441) _(by Jaysukh Makvana)_ - [`c58e9e4`](https://github.com/stdlib-js/stdlib/commit/c58e9e4dce4361b4ae7454eca926b0e00afb15aa) - **feat:** add `indexOf` and `lastIndexOf` methods to `array/bool` [(#2432)](https://github.com/stdlib-js/stdlib/pull/2432) _(by Jaysukh Makvana, Athan Reines)_ - [`61c5609`](https://github.com/stdlib-js/stdlib/commit/61c5609ba30f3b07cd97089746a5dca25a614d94) - **docs:** fix examples in REPL documentation for complex number arrays [(#2431)](https://github.com/stdlib-js/stdlib/pull/2431) _(by Jaysukh Makvana)_ - [`4d54abb`](https://github.com/stdlib-js/stdlib/commit/4d54abb0b5e5d2f146e85f2a65799907fbb5bd0c) - **feat:** add boolean dtype support to `array/base/none` [(#2424)](https://github.com/stdlib-js/stdlib/pull/2424) _(by Jaysukh Makvana, Athan Reines)_ diff --git a/bool/README.md b/bool/README.md index 1b6e645c..d51b6603 100644 --- a/bool/README.md +++ b/bool/README.md @@ -620,6 +620,28 @@ var v = arr.get( 100 ); // returns undefined ``` + + +#### BooleanArray.prototype.includes( searchElement\[, fromIndex] ) + +Returns a boolean indicating whether an array includes a provided value. + +```javascript +var arr = new BooleanArray( 5 ); + +arr.set( true, 0 ); +arr.set( false, 1 ); +arr.set( true, 2 ); +arr.set( true, 3 ); +arr.set( true, 4 ); + +var bool = arr.includes( true ); +// returns true + +bool = arr.includes( false, 2 ); +// returns false +``` + #### BooleanArray.prototype.indexOf( searchElement\[, fromIndex] ) diff --git a/bool/benchmark/benchmark.includes.js b/bool/benchmark/benchmark.includes.js new file mode 100644 index 00000000..42d55999 --- /dev/null +++ b/bool/benchmark/benchmark.includes.js @@ -0,0 +1,56 @@ +/** +* @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 Boolean = require( '@stdlib/boolean/ctor' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var BooleanArray = require( './../lib' ); + + +// MAIN // + +bench( pkg+':includes', function benchmark( b ) { + var bool; + var arr; + var i; + + arr = []; + for ( i = 0; i < 10; i++ ) { + arr.push( Boolean( i%2 ) ); + } + arr = new BooleanArray( arr ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = arr.includes( true, 0 ); + 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(); +}); diff --git a/bool/benchmark/benchmark.includes.length.js b/bool/benchmark/benchmark.includes.length.js new file mode 100644 index 00000000..d7b4b81b --- /dev/null +++ b/bool/benchmark/benchmark.includes.length.js @@ -0,0 +1,103 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var BooleanArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr; + var i; + + arr = []; + for ( i = 0; i < len-1; i++ ) { + arr.push( false ); + } + arr.push( true ); + 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.includes( true ); + 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+':includes:len='+len, f ); + } +} + +main(); diff --git a/bool/docs/repl.txt b/bool/docs/repl.txt index 7b75dedb..f676f1cd 100644 --- a/bool/docs/repl.txt +++ b/bool/docs/repl.txt @@ -257,7 +257,7 @@ > var offset = arr.byteOffset 0 - > var buf = new {{alias:@stdlib/array/buffer}}( 240 ) + > var buf = new {{alias:@stdlib/array/buffer}}( 240 ); > arr = new {{alias}}( buf, 64 ) > offset = arr.byteOffset @@ -484,6 +484,34 @@ true +{{alias}}.prototype.includes( searchElement[, fromIndex] ) + Returns a boolean indicating whether an array includes a provided value. + + Parameters + ---------- + searchElement: boolean + Search element. + + fromIndex: integer (optional) + Array index at which to start the search. If provided a negative value, + the method resolves the start index relative to the last array element. + Default: 0. + + Returns + ------- + bool: boolean + Boolean indicating whether an array includes a search element. + + Examples + -------- + > var arr = new {{alias}}( [ true, false, true, true, true ] ) + + > var bool = arr.includes( true ) + true + > bool = arr.includes( false, 3 ) + false + + {{alias}}.prototype.indexOf( searchElement[, fromIndex] ) Returns the first index at which a given element can be found. diff --git a/bool/docs/types/index.d.ts b/bool/docs/types/index.d.ts index b7329f52..1c3ee1a1 100644 --- a/bool/docs/types/index.d.ts +++ b/bool/docs/types/index.d.ts @@ -406,6 +406,30 @@ declare class BooleanArray implements BooleanArrayInterface { */ get( i: number ): boolean | void; + /** + * Returns a boolean indicating whether an array includes a provided value. + * + * @param searchElement - element to search for + * @param fromIndex - starting index (inclusive) + * @returns boolean indicating whether an array includes a value + * + * @example + * var arr = new BooleanArray( 5 ); + * + * arr.set( true, 0 ); + * arr.set( false, 1 ); + * arr.set( true, 2 ); + * arr.set( true, 3 ); + * arr.set( true, 4 ); + * + * var bool = arr.includes( true ); + * // returns true + * + * bool = arr.includes( false, 2 ); + * // returns false + */ + includes( searchElement: boolean, fromIndex?: number ): boolean; + /** * Returns the first index at which a given element can be found. * diff --git a/bool/lib/main.js b/bool/lib/main.js index cca3eb24..c8966933 100644 --- a/bool/lib/main.js +++ b/bool/lib/main.js @@ -720,6 +720,66 @@ setReadOnly( BooleanArray.prototype, 'get', function get( idx ) { return Boolean( this._buffer[ idx ] ); }); +/** +* Returns a boolean indicating whether an array includes a provided value. +* +* @name includes +* @memberof BooleanArray.prototype +* @type {Function} +* @param {boolean} searchElement - search element +* @param {integer} [fromIndex=0] - starting index (inclusive) +* @throws {TypeError} `this` must be a boolean array +* @throws {TypeError} first argument must be a boolean value +* @throws {TypeError} second argument must be an integer +* @returns {boolean} boolean indicating whether an array includes a value +* +* @example +* var arr = new BooleanArray( 5 ); +* +* arr.set( true, 0 ); +* arr.set( false, 1 ); +* arr.set( true, 2 ); +* arr.set( true, 3 ); +* arr.set( true, 4 ); +* +* var bool = arr.includes( true ); +* // returns true +* +* bool = arr.includes( false, 2 ); +* // returns false +*/ +setReadOnly( BooleanArray.prototype, 'includes', function includes( searchElement, fromIndex ) { + var buf; + var i; + + if ( !isBooleanArray( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a boolean array.' ); + } + if ( !isBoolean( searchElement ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a boolean. Value: `%s`.', searchElement ) ); + } + if ( arguments.length > 1 ) { + if ( !isInteger( fromIndex ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', fromIndex ) ); + } + if ( fromIndex < 0 ) { + fromIndex += this._length; + if ( fromIndex < 0 ) { + fromIndex = 0; + } + } + } else { + fromIndex = 0; + } + buf = this._buffer; + for ( i = fromIndex; i < this._length; i++ ) { + if ( searchElement === Boolean( buf[ i ] ) ) { + return true; + } + } + return false; +}); + /** * Returns the first index at which a given element can be found. * diff --git a/bool/test/test.includes.js b/bool/test/test.includes.js new file mode 100644 index 00000000..8cf5a5c5 --- /dev/null +++ b/bool/test/test.includes.js @@ -0,0 +1,221 @@ +/** +* @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 `includes` method', function test( t ) { + t.strictEqual( hasOwnProp( BooleanArray.prototype, 'includes' ), true, 'has property' ); + t.strictEqual( isFunction( BooleanArray.prototype.includes ), 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.includes.call( value, true ); + }; + } +}); + +tape( 'the method throws an error if provided a first argument which is not a boolean value', function test( t ) { + var values; + var arr; + var i; + + arr = new BooleanArray( 5 ); + + values = [ + '5', + 5, + NaN, + 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.includes( value ); + }; + } +}); + +tape( 'the method throws an error if provided a second argument which is not an integer', function test( t ) { + var values; + var arr; + var i; + + arr = new BooleanArray( 10 ); + + values = [ + '5', + 3.14, + 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.includes( true, 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.includes( true ); + + t.strictEqual( bool, false, 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns `false` if a boolean value is not found', function test( t ) { + var bool; + var arr; + + arr = new BooleanArray( 10 ); + bool = arr.includes( true ); + + t.strictEqual( bool, false, 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns `true` if an array contains a specified boolean value', function test( t ) { + var bool; + var arr; + + arr = new BooleanArray( [ true, false, false, true] ); + bool = arr.includes( false ); + + t.strictEqual( bool, true, 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns `false` if provided a second argument which exceeds the input array length', function test( t ) { + var bool; + var arr; + + arr = new BooleanArray( 10 ); + bool = arr.includes( true, 20 ); + + t.strictEqual( bool, false, 'returns expected value' ); + t.end(); +}); + +tape( 'the method supports specifying a starting search index', function test( t ) { + var bool; + var arr; + + arr = new BooleanArray( [ true, false, true, true, true ] ); + + bool = arr.includes( true, 0 ); + t.strictEqual( bool, true, 'returns expected value' ); + + bool = arr.includes( false, 1 ); + t.strictEqual( bool, true, 'returns expected value' ); + + bool = arr.includes( false, 3 ); + t.strictEqual( bool, false, 'returns expected value' ); + t.end(); +}); + +tape( 'the method supports specifying a starting search index (negative)', function test( t ) { + var bool; + var arr; + + arr = new BooleanArray( [ true, false, true, true, true ] ); + + bool = arr.includes( true, -5 ); + t.strictEqual( bool, true, 'returns expected value' ); + + bool = arr.includes( false, -2 ); + t.strictEqual( bool, false, 'returns expected value' ); + t.end(); +}); + +tape( 'when provided a starting index which resolves to an index which is less than zero, the method searches from the first array element', function test( t ) { + var bool; + var arr; + + arr = new BooleanArray( [ true, false, true, true, true ] ); + + bool = arr.includes( true, -10 ); + t.strictEqual( bool, true, 'returns expected value' ); + + bool = arr.includes( false, -10 ); + t.strictEqual( bool, true, 'returns expected value' ); + t.end(); +});