Skip to content
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

feat: add at, fill, filter, toLocalestring to array/bool #2607

Merged
merged 8 commits into from
Jul 15, 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
178 changes: 176 additions & 2 deletions lib/node_modules/@stdlib/array/bool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,38 @@ var len = arr.length;
// returns 4
```

<a name="method-at"></a>

#### BooleanArray.prototype.at( i )

Returns an array element located at integer position (index) `i`, with support for both nonnegative and negative integer positions.

```javascript
var arr = new BooleanArray( 3 );

arr.set( true, 0 );
arr.set( false, 1 );
arr.set( true, 2 );

var v = arr.at( 0 );
// returns true

v = arr.at( -1 );
// returns true
```

If provided an out-of-bounds index, the method returns `undefined`.

```javascript
var arr = new BooleanArray( 10 );

var v = arr.at( 100 );
// returns undefined

v = arr.at( -100 );
// returns undefined
```

<a name="method-every"></a>

#### BooleanArray.prototype.every( predicate\[, thisArg] )
Expand Down Expand Up @@ -386,6 +418,126 @@ var count = context.count;
// returns 3
```

<a name="method-fill"></a>

#### BooleanArray.prototype.fill( value\[, start\[, end]] )

Returns a modified typed array filled with a fill value.

```javascript
var arr = new BooleanArray( 3 );

// Set all elements to the same value:
arr.fill( true );

var v = arr.get( 0 );
// returns true

v = arr.get( 1 );
// returns true

v = arr.get( 2 );
// returns true

// Fill all elements starting from the second element:
arr.fill( false, 1 );

v = arr.get( 1 );
// returns false

v = arr.get( 2 );
// returns false

// Fill all elements from first element until the second-to-last element:
arr.fill( false, 0, 2 );

v = arr.get( 0 );
// returns false

v = arr.get( 1 );
// returns false
```

When a `start` and/or `end` index is negative, the respective index is determined relative to the last array element.

```javascript
var arr = new BooleanArray( 3 );

// Set all array elements, except the last element, to the same value:
arr.fill( true, 0, -1 );

var v = arr.get( 0 );
// returns true

v = arr.get( 2 );
// returns false
```

<a name="method-filter"></a>

#### BooleanArray.prototype.filter( predicate\[, thisArg] )

Returns a new array containing the elements of an array which pass a test implemented by a predicate function.

```javascript
function predicate( v ) {
return ( v === true );
}

var arr = new BooleanArray( 3 );

// Set the first three elements:
arr.set( true, 0 );
arr.set( false, 1 );
arr.set( true, 2 );

var out = arr.filter( predicate );
// returns <BooleanArray>

var len = out.length;
// returns 2

var v = out.get( 0 );
// returns true

v = out.get( 1 );
// return 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, i ) {
this.count += 1;
return ( v === true );
}

var arr = new BooleanArray( 3 );

var context = {
'count': 0
};

arr.set( true, 0 );
arr.set( false, 1 );
arr.set( true, 2 );

var out = arr.filter( predicate, context );
// returns <BooleanArray>

var len = out.length;
// returns 2

var count = context.count;
// returns 3
```

<a name="method-find"></a>

#### BooleanArray.prototype.find( predicate\[, thisArg] )
Expand Down Expand Up @@ -1197,8 +1349,6 @@ The function should return a number where:
- a positive value indicates that `a` should come after `b`.
- zero or `NaN` indicates that `a` and `b` are considered equal.

<a name="method-to-reversed"></a>

<a name="method-subarray"></a>

#### BooleanArray.prototype.subarray( \[begin\[, end]] )
Expand Down Expand Up @@ -1275,6 +1425,30 @@ bool = subarr.get( len-1 );
// returns true
```

<a name="method-to-locale-string"></a>

#### BooleanArray.prototype.toLocaleString( \[locales\[, options]] )

Serializes an array as a locale-specific string.

```javascript
var arr = new BooleanArray( 3 );

arr.set( true, 0 );
arr.set( false, 1 );
arr.set( true, 2 );

var str = arr.toLocaleString();
// returns 'true,false,true'
```

The method supports the following arguments:

- **locales**: a string with a BCP 47 language tag or an array of such strings.
- **options**: configuration properties.

<a name="method-to-reversed"></a>

#### BooleanArray.prototype.toReversed()

Returns a new typed array containing the elements in reversed order.
Expand Down
85 changes: 85 additions & 0 deletions lib/node_modules/@stdlib/array/bool/benchmark/benchmark.at.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* @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+'::nonnegative_indices:at', function benchmark( b ) {
var arr;
var N;
var v;
var i;

arr = [];
for ( i = 0; i < 10; i++ ) {
arr.push( true );
}
arr = new BooleanArray( arr );
N = arr.length;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = arr.at( i%N );
if ( typeof v !== 'boolean' ) {
b.fail( 'should return a boolean' );
}
}
b.toc();
if ( !isBoolean( v ) ) {
b.fail( 'should return a boolean' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::negative_indices:at', function benchmark( b ) {
var arr;
var N;
var v;
var i;

arr = [];
for ( i = 0; i < 10; i++ ) {
arr.push( true );
}
arr = new BooleanArray( arr );
N = arr.length;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = arr.at( -(i%N)-1 );
if ( typeof v !== 'boolean' ) {
b.fail( 'should return a boolean' );
}
}
b.toc();
if ( !isBoolean( v ) ) {
b.fail( 'should return a boolean' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
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' );

Expand Down Expand Up @@ -56,7 +55,7 @@ function createBenchmark( len ) {

arr = [];
for ( i = 0; i < len; i++ ) {
arr.push( Boolean( 1 ) );
arr.push( true );
}
arr = new BooleanArray( arr );

Expand Down
56 changes: 56 additions & 0 deletions lib/node_modules/@stdlib/array/bool/benchmark/benchmark.fill.js
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 isBooleanArray = require( '@stdlib/assert/is-booleanarray' );
var pkg = require( './../package.json' ).name;
var BooleanArray = require( './../lib' );


// MAIN //

bench( pkg+':fill', function benchmark( b ) {
var values;
var arr;
var out;
var i;

values = [
true,
false
];
arr = new BooleanArray( 5 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = arr.fill( values[ i%values.length ] );
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !isBooleanArray( out ) ) {
b.fail( 'should return a BooleanArray' );
}
b.pass( 'benchmark finished' );
b.end();
});
Loading
Loading