Skip to content

feat: add includes method to array/bool #2441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/node_modules/@stdlib/array/bool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,28 @@ var v = arr.get( 100 );
// returns undefined
```

<a name="includes"></a>

#### 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
```

<a name="method-index-of"></a>

#### BooleanArray.prototype.indexOf( searchElement\[, fromIndex] )
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
});
Original file line number Diff line number Diff line change
@@ -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();
30 changes: 29 additions & 1 deletion lib/node_modules/@stdlib/array/bool/docs/repl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@
<BooleanArray>
> 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 )
<BooleanArray>
> offset = arr.byteOffset
Expand Down Expand Up @@ -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 ] )
<BooleanArray>
> 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.

Expand Down
24 changes: 24 additions & 0 deletions lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
60 changes: 60 additions & 0 deletions lib/node_modules/@stdlib/array/bool/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Loading