Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7c17308

Browse files
Jaysukh-409kgryte
andauthored
feat: add at, fill, filter, and toLocalestring methods to array/bool
PR-URL: #2607 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Athan Reines <kgryte@gmail.com>
1 parent 523b380 commit 7c17308

16 files changed

+2023
-4
lines changed

‎lib/node_modules/@stdlib/array/bool/README.md‎

Lines changed: 176 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,38 @@ var len = arr.length;
334334
// returns 4
335335
```
336336

337+
<a name="method-at"></a>
338+
339+
#### BooleanArray.prototype.at( i )
340+
341+
Returns an array element located at integer position (index) `i`, with support for both nonnegative and negative integer positions.
342+
343+
```javascript
344+
var arr = new BooleanArray( 3 );
345+
346+
arr.set( true, 0 );
347+
arr.set( false, 1 );
348+
arr.set( true, 2 );
349+
350+
var v = arr.at( 0 );
351+
// returns true
352+
353+
v = arr.at( -1 );
354+
// returns true
355+
```
356+
357+
If provided an out-of-bounds index, the method returns `undefined`.
358+
359+
```javascript
360+
var arr = new BooleanArray( 10 );
361+
362+
var v = arr.at( 100 );
363+
// returns undefined
364+
365+
v = arr.at( -100 );
366+
// returns undefined
367+
```
368+
337369
<a name="method-every"></a>
338370

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

421+
<a name="method-fill"></a>
422+
423+
#### BooleanArray.prototype.fill( value\[, start\[, end]] )
424+
425+
Returns a modified typed array filled with a fill value.
426+
427+
```javascript
428+
var arr = new BooleanArray( 3 );
429+
430+
// Set all elements to the same value:
431+
arr.fill( true );
432+
433+
var v = arr.get( 0 );
434+
// returns true
435+
436+
v = arr.get( 1 );
437+
// returns true
438+
439+
v = arr.get( 2 );
440+
// returns true
441+
442+
// Fill all elements starting from the second element:
443+
arr.fill( false, 1 );
444+
445+
v = arr.get( 1 );
446+
// returns false
447+
448+
v = arr.get( 2 );
449+
// returns false
450+
451+
// Fill all elements from first element until the second-to-last element:
452+
arr.fill( false, 0, 2 );
453+
454+
v = arr.get( 0 );
455+
// returns false
456+
457+
v = arr.get( 1 );
458+
// returns false
459+
```
460+
461+
When a `start` and/or `end` index is negative, the respective index is determined relative to the last array element.
462+
463+
```javascript
464+
var arr = new BooleanArray( 3 );
465+
466+
// Set all array elements, except the last element, to the same value:
467+
arr.fill( true, 0, -1 );
468+
469+
var v = arr.get( 0 );
470+
// returns true
471+
472+
v = arr.get( 2 );
473+
// returns false
474+
```
475+
476+
<a name="method-filter"></a>
477+
478+
#### BooleanArray.prototype.filter( predicate\[, thisArg] )
479+
480+
Returns a new array containing the elements of an array which pass a test implemented by a predicate function.
481+
482+
```javascript
483+
function predicate( v ) {
484+
return ( v === true );
485+
}
486+
487+
var arr = new BooleanArray( 3 );
488+
489+
// Set the first three elements:
490+
arr.set( true, 0 );
491+
arr.set( false, 1 );
492+
arr.set( true, 2 );
493+
494+
var out = arr.filter( predicate );
495+
// returns <BooleanArray>
496+
497+
var len = out.length;
498+
// returns 2
499+
500+
var v = out.get( 0 );
501+
// returns true
502+
503+
v = out.get( 1 );
504+
// return true
505+
```
506+
507+
The `predicate` function is provided three arguments:
508+
509+
- **value**: current array element.
510+
- **index**: current array element index.
511+
- **arr**: the array on which this method was called.
512+
513+
To set the function execution context, provide a `thisArg`.
514+
515+
```javascript
516+
function predicate( v, i ) {
517+
this.count += 1;
518+
return ( v === true );
519+
}
520+
521+
var arr = new BooleanArray( 3 );
522+
523+
var context = {
524+
'count': 0
525+
};
526+
527+
arr.set( true, 0 );
528+
arr.set( false, 1 );
529+
arr.set( true, 2 );
530+
531+
var out = arr.filter( predicate, context );
532+
// returns <BooleanArray>
533+
534+
var len = out.length;
535+
// returns 2
536+
537+
var count = context.count;
538+
// returns 3
539+
```
540+
389541
<a name="method-find"></a>
390542

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

1200-
<a name="method-to-reversed"></a>
1201-
12021352
<a name="method-subarray"></a>
12031353

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

1428+
<a name="method-to-locale-string"></a>
1429+
1430+
#### BooleanArray.prototype.toLocaleString( \[locales\[, options]] )
1431+
1432+
Serializes an array as a locale-specific string.
1433+
1434+
```javascript
1435+
var arr = new BooleanArray( 3 );
1436+
1437+
arr.set( true, 0 );
1438+
arr.set( false, 1 );
1439+
arr.set( true, 2 );
1440+
1441+
var str = arr.toLocaleString();
1442+
// returns 'true,false,true'
1443+
```
1444+
1445+
The method supports the following arguments:
1446+
1447+
- **locales**: a string with a BCP 47 language tag or an array of such strings.
1448+
- **options**: configuration properties.
1449+
1450+
<a name="method-to-reversed"></a>
1451+
12781452
#### BooleanArray.prototype.toReversed()
12791453

12801454
Returns a new typed array containing the elements in reversed order.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var BooleanArray = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg+'::nonnegative_indices:at', function benchmark( b ) {
32+
var arr;
33+
var N;
34+
var v;
35+
var i;
36+
37+
arr = [];
38+
for ( i = 0; i < 10; i++ ) {
39+
arr.push( true );
40+
}
41+
arr = new BooleanArray( arr );
42+
N = arr.length;
43+
44+
b.tic();
45+
for ( i = 0; i < b.iterations; i++ ) {
46+
v = arr.at( i%N );
47+
if ( typeof v !== 'boolean' ) {
48+
b.fail( 'should return a boolean' );
49+
}
50+
}
51+
b.toc();
52+
if ( !isBoolean( v ) ) {
53+
b.fail( 'should return a boolean' );
54+
}
55+
b.pass( 'benchmark finished' );
56+
b.end();
57+
});
58+
59+
bench( pkg+'::negative_indices:at', function benchmark( b ) {
60+
var arr;
61+
var N;
62+
var v;
63+
var i;
64+
65+
arr = [];
66+
for ( i = 0; i < 10; i++ ) {
67+
arr.push( true );
68+
}
69+
arr = new BooleanArray( arr );
70+
N = arr.length;
71+
72+
b.tic();
73+
for ( i = 0; i < b.iterations; i++ ) {
74+
v = arr.at( -(i%N)-1 );
75+
if ( typeof v !== 'boolean' ) {
76+
b.fail( 'should return a boolean' );
77+
}
78+
}
79+
b.toc();
80+
if ( !isBoolean( v ) ) {
81+
b.fail( 'should return a boolean' );
82+
}
83+
b.pass( 'benchmark finished' );
84+
b.end();
85+
});

‎lib/node_modules/@stdlib/array/bool/benchmark/benchmark.every.length.js‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
var bench = require( '@stdlib/bench' );
2424
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
2525
var pow = require( '@stdlib/math/base/special/pow' );
26-
var Boolean = require( '@stdlib/boolean/ctor' );
2726
var pkg = require( './../package.json' ).name;
2827
var BooleanArray = require( './../lib' );
2928

@@ -56,7 +55,7 @@ function createBenchmark( len ) {
5655

5756
arr = [];
5857
for ( i = 0; i < len; i++ ) {
59-
arr.push( Boolean(1) );
58+
arr.push( true );
6059
}
6160
arr = new BooleanArray( arr );
6261

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isBooleanArray = require( '@stdlib/assert/is-booleanarray' );
25+
var pkg = require( './../package.json' ).name;
26+
var BooleanArray = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg+':fill', function benchmark( b ) {
32+
var values;
33+
var arr;
34+
var out;
35+
var i;
36+
37+
values = [
38+
true,
39+
false
40+
];
41+
arr = new BooleanArray( 5 );
42+
43+
b.tic();
44+
for ( i = 0; i < b.iterations; i++ ) {
45+
out = arr.fill( values[ i%values.length ] );
46+
if ( typeof out !== 'object' ) {
47+
b.fail( 'should return an object' );
48+
}
49+
}
50+
b.toc();
51+
if ( !isBooleanArray( out ) ) {
52+
b.fail( 'should return a BooleanArray' );
53+
}
54+
b.pass( 'benchmark finished' );
55+
b.end();
56+
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /