Skip to content

Commit a3e6a84

Browse files
committed
Auto-generated commit
1 parent 943e1fc commit a3e6a84

File tree

7 files changed

+517
-0
lines changed

7 files changed

+517
-0
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,7 @@ This release closes the following issue:
919919

920920
##### Features
921921

922+
- [`40da309`](https://github.com/stdlib-js/stdlib/commit/40da3097c6ffaed4cd9284d6cdeff8bf11786553) - add `map` method to `array/bool` [(#2292)](https://github.com/stdlib-js/stdlib/pull/2292)
922923
- [`5d53c2d`](https://github.com/stdlib-js/stdlib/commit/5d53c2de22e6899dc3970b4714c0c4e228562f92) - add initial TypeScript declarations
923924
- [`e221398`](https://github.com/stdlib-js/stdlib/commit/e2213984af7a95af1b439aae8e2122968892c55d) - add `array/bool`
924925

@@ -1634,6 +1635,7 @@ A total of 13 people contributed to this release. Thank you to the following con
16341635

16351636
<details>
16361637

1638+
- [`40da309`](https://github.com/stdlib-js/stdlib/commit/40da3097c6ffaed4cd9284d6cdeff8bf11786553) - **feat:** add `map` method to `array/bool` [(#2292)](https://github.com/stdlib-js/stdlib/pull/2292) _(by Jaysukh Makvana, Athan Reines)_
16371639
- [`3edcfe5`](https://github.com/stdlib-js/stdlib/commit/3edcfe5d814fd12a56dbe492ddc78663721f5acd) - **feat:** update namespace TypeScript declarations [(#2303)](https://github.com/stdlib-js/stdlib/pull/2303) _(by stdlib-bot, Athan Reines)_
16381640
- [`96e896a`](https://github.com/stdlib-js/stdlib/commit/96e896a39be08912b2e06dfb6b671ec13d042412) - **feat:** add support for boolean array indices _(by Athan Reines)_
16391641
- [`cc06b5c`](https://github.com/stdlib-js/stdlib/commit/cc06b5c8c67a1c8d1022920273535b29e5abf550) - **fix:** add missing generic parameter _(by Athan Reines)_

bool/README.md

+61
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,67 @@ var v = arr.get( 100 );
360360
// returns undefined
361361
```
362362

363+
<a name="method-map"></a>
364+
365+
#### BooleanArray.prototype.map( callbackFn\[, thisArg] )
366+
367+
Returns a new array with each element being the result of a provided callback function.
368+
369+
```javascript
370+
function invert( v ) {
371+
return !v;
372+
}
373+
374+
var arr = new BooleanArray( 3 );
375+
376+
arr.set( true, 0 );
377+
arr.set( false, 1 );
378+
arr.set( true, 2 );
379+
380+
var out = arr.map( invert );
381+
// returns <BooleanArray>
382+
383+
var z = out.get( 0 );
384+
// returns false
385+
386+
z = out.get( 1 );
387+
// returns true
388+
389+
z = out.get( 2 );
390+
// returns false
391+
```
392+
393+
The callback function is provided three arguments:
394+
395+
- **value**: current array element.
396+
- **index**: current array element index.
397+
- **arr**: the array on which this method was called.
398+
399+
To set the function execution context, provide a `thisArg`.
400+
401+
```javascript
402+
function invert( v, i ) {
403+
this.count += i;
404+
return !v;
405+
}
406+
407+
var arr = new BooleanArray( 3 );
408+
409+
var context = {
410+
'count': 0
411+
};
412+
413+
arr.set( true, 0 );
414+
arr.set( false, 1 );
415+
arr.set( true, 2 );
416+
417+
var out = arr.map( invert, context );
418+
// returns <BooleanArray>
419+
420+
var count = context.count;
421+
// returns 3;
422+
```
423+
363424
<a name="method-set"></a>
364425

365426
#### BooleanArray.prototype.set( v\[, i] )

bool/benchmark/benchmark.map.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+':map', function benchmark( b ) {
32+
var out;
33+
var arr;
34+
var i;
35+
36+
arr = new BooleanArray( [ true, false, false, true, true, false ] );
37+
38+
b.tic();
39+
for ( i = 0; i < b.iterations; i++ ) {
40+
out = arr.map( invert );
41+
if ( typeof out !== 'object' ) {
42+
b.fail( 'should return an object' );
43+
}
44+
}
45+
b.toc();
46+
if ( !isBooleanArray( out ) ) {
47+
b.fail( 'should return a BooleanArray' );
48+
}
49+
b.pass( 'benchmark finished' );
50+
b.end();
51+
52+
function invert( v ) {
53+
return !v;
54+
}
55+
});
+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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 pow = require( '@stdlib/math/base/special/pow' );
25+
var Boolean = require( '@stdlib/boolean/ctor' );
26+
var identity = require( '@stdlib/utils/identity-function' );
27+
var isBooleanArray = require( '@stdlib/assert/is-booleanarray' );
28+
var pkg = require( './../package.json' ).name;
29+
var BooleanArray = require( './../lib' );
30+
31+
32+
// FUNCTIONS //
33+
34+
/**
35+
* Creates a benchmark function.
36+
*
37+
* @private
38+
* @param {PositiveInteger} len - array length
39+
* @returns {Function} benchmark function
40+
*/
41+
function createBenchmark( len ) {
42+
var arr;
43+
var i;
44+
45+
arr = [];
46+
for ( i = 0; i < len; i++ ) {
47+
arr.push( Boolean( i%2 ) );
48+
}
49+
arr = new BooleanArray( arr );
50+
51+
return benchmark;
52+
53+
/**
54+
* Benchmark function.
55+
*
56+
* @private
57+
* @param {Benchmark} b - benchmark instance
58+
*/
59+
function benchmark( b ) {
60+
var out;
61+
var i;
62+
63+
b.tic();
64+
for ( i = 0; i < b.iterations; i++ ) {
65+
out = arr.map( identity );
66+
if ( typeof out !== 'object' ) {
67+
b.fail( 'should return an object' );
68+
}
69+
}
70+
b.toc();
71+
if ( !isBooleanArray( out ) ) {
72+
b.fail( 'should return a BooleanArray' );
73+
}
74+
b.pass( 'benchmark finished' );
75+
b.end();
76+
}
77+
}
78+
79+
80+
// MAIN //
81+
82+
/**
83+
* Main execution sequence.
84+
*
85+
* @private
86+
*/
87+
function main() {
88+
var len;
89+
var min;
90+
var max;
91+
var f;
92+
var i;
93+
94+
min = 1; // 10^min
95+
max = 6; // 10^max
96+
97+
for ( i = min; i <= max; i++ ) {
98+
len = pow( 10, i );
99+
f = createBenchmark( len );
100+
bench( pkg+':map:len='+len, f );
101+
}
102+
}
103+
104+
main();

bool/docs/types/index.d.ts

+76
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,50 @@ type FromBinary<U> = ( this: U, value: boolean, index: number ) => boolean;
6262
*/
6363
type FromCallback<U> = FromNullary<U> | FromUnary<U> | FromBinary<U>;
6464

65+
/**
66+
* Callback invoked for each element in an array.
67+
*
68+
* @returns returned value
69+
*/
70+
type NullaryMapFcn<U> = ( this: U ) => any;
71+
72+
/**
73+
* Callback invoked for each element in an array.
74+
*
75+
* @param value - current array element
76+
* @returns returned value
77+
*/
78+
type UnaryMapFcn<U> = ( this: U, value: boolean ) => any;
79+
80+
/**
81+
* Callback invoked for each element in an array.
82+
*
83+
* @param value - current array element
84+
* @param index - current array element index
85+
* @returns returned value
86+
*/
87+
type BinaryMapFcn<U> = ( this: U, value: boolean, index: number ) => any;
88+
89+
/**
90+
* Callback invoked for each element in an array.
91+
*
92+
* @param value - current array element
93+
* @param index - current array element index
94+
* @param arr - array on which the method was called
95+
* @returns returned value
96+
*/
97+
type TernaryMapFcn<U> = ( this: U, value: boolean, index: number, arr: BooleanArray ) => any;
98+
99+
/**
100+
* Callback invoked for each element in an array.
101+
*
102+
* @param value - current array element
103+
* @param index - current array element index
104+
* @param arr - array on which the method was called
105+
* @returns returned value
106+
*/
107+
type MapFcn<U> = NullaryMapFcn<U> | UnaryMapFcn<U> | BinaryMapFcn<U> | TernaryMapFcn<U>;
108+
65109
/**
66110
* Class for creating a Boolean array.
67111
*/
@@ -194,6 +238,38 @@ declare class BooleanArray implements BooleanArrayInterface {
194238
*/
195239
get( i: number ): boolean | void;
196240

241+
/**
242+
* Returns a new array with each element being the result of a provided callback function.
243+
*
244+
* @param fcn - callback function
245+
* @param thisArg - callback function execution context
246+
* @returns new boolean array
247+
*
248+
* @example
249+
* function invert( v ) {
250+
* return !v;
251+
* }
252+
*
253+
* var arr = new BooleanArray( 3 );
254+
*
255+
* arr.set( true, 0 );
256+
* arr.set( false, 1 );
257+
* arr.set( true, 2 );
258+
*
259+
* var out = arr.map( invert );
260+
* // returns <BooleanArray>
261+
*
262+
* var v = out.get( 0 );
263+
* // returns false
264+
*
265+
* v = out.get( 1 );
266+
* // returns true
267+
*
268+
* v = out.get( 2 );
269+
* // returns false
270+
*/
271+
map<U = unknown>( fcn: MapFcn<U>, thisArg?: ThisParameterType<MapFcn<U>> ): BooleanArray;
272+
197273
/**
198274
* Sets an array element.
199275
*

bool/lib/main.js

+55
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,61 @@ setReadOnlyAccessor( BooleanArray.prototype, 'length', function get() {
504504
return this._length;
505505
});
506506

507+
/**
508+
* Returns a new array with each element being the result of a provided callback function.
509+
*
510+
* @name map
511+
* @memberof BooleanArray.prototype
512+
* @type {Function}
513+
* @param {Function} fcn - callback function
514+
* @param {*} [thisArg] - callback function execution context
515+
* @throws {TypeError} `this` must be a boolean array
516+
* @throws {TypeError} first argument must be a function
517+
* @returns {BooleanArray} new boolean array
518+
*
519+
* @example
520+
* function invert( v ) {
521+
* return !v;
522+
* }
523+
*
524+
* var arr = new BooleanArray( 3 );
525+
*
526+
* arr.set( true, 0 );
527+
* arr.set( false, 1 );
528+
* arr.set( true, 2 );
529+
*
530+
* var out = arr.map( invert );
531+
* // returns <BooleanArray>
532+
*
533+
* var z = out.get( 0 );
534+
* // returns false
535+
*
536+
* z = out.get( 1 );
537+
* // returns true
538+
*
539+
* z = out.get( 2 );
540+
* // returns false
541+
*/
542+
setReadOnly( BooleanArray.prototype, 'map', function map( fcn, thisArg ) {
543+
var outbuf;
544+
var out;
545+
var buf;
546+
var i;
547+
if ( !isBooleanArray( this ) ) {
548+
throw new TypeError( 'invalid invocation. `this` is not a boolean array.' );
549+
}
550+
if ( !isFunction( fcn ) ) {
551+
throw new TypeError( 'invalid argument. First argument must be a function. Value: `%s`.', fcn );
552+
}
553+
buf = this._buffer;
554+
out = new this.constructor( this._length );
555+
outbuf = out._buffer; // eslint-disable-line no-underscore-dangle
556+
for ( i = 0; i < this._length; i++ ) {
557+
outbuf[ i ] = Boolean( fcn.call( thisArg, Boolean( buf[ i ] ), i, this ) );
558+
}
559+
return out;
560+
});
561+
507562
/**
508563
* Sets an array element.
509564
*

0 commit comments

Comments
 (0)