From a3e6a84daab28c11bb0e3e87e946b5f176f84f9d Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 5 Jun 2024 17:44:44 +0000 Subject: [PATCH] Auto-generated commit --- CHANGELOG.md | 2 + bool/README.md | 61 +++++++++ bool/benchmark/benchmark.map.js | 55 +++++++++ bool/benchmark/benchmark.map.length.js | 104 ++++++++++++++++ bool/docs/types/index.d.ts | 76 ++++++++++++ bool/lib/main.js | 55 +++++++++ bool/test/test.map.js | 164 +++++++++++++++++++++++++ 7 files changed, 517 insertions(+) create mode 100644 bool/benchmark/benchmark.map.js create mode 100644 bool/benchmark/benchmark.map.length.js create mode 100644 bool/test/test.map.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d9da053..e4fbbb0a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -919,6 +919,7 @@ This release closes the following issue: ##### Features +- [`40da309`](https://github.com/stdlib-js/stdlib/commit/40da3097c6ffaed4cd9284d6cdeff8bf11786553) - add `map` method to `array/bool` [(#2292)](https://github.com/stdlib-js/stdlib/pull/2292) - [`5d53c2d`](https://github.com/stdlib-js/stdlib/commit/5d53c2de22e6899dc3970b4714c0c4e228562f92) - add initial TypeScript declarations - [`e221398`](https://github.com/stdlib-js/stdlib/commit/e2213984af7a95af1b439aae8e2122968892c55d) - add `array/bool` @@ -1634,6 +1635,7 @@ A total of 13 people contributed to this release. Thank you to the following con
+- [`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)_ - [`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)_ - [`96e896a`](https://github.com/stdlib-js/stdlib/commit/96e896a39be08912b2e06dfb6b671ec13d042412) - **feat:** add support for boolean array indices _(by Athan Reines)_ - [`cc06b5c`](https://github.com/stdlib-js/stdlib/commit/cc06b5c8c67a1c8d1022920273535b29e5abf550) - **fix:** add missing generic parameter _(by Athan Reines)_ diff --git a/bool/README.md b/bool/README.md index 8b44a8ba..916915fc 100644 --- a/bool/README.md +++ b/bool/README.md @@ -360,6 +360,67 @@ var v = arr.get( 100 ); // returns undefined ``` + + +#### BooleanArray.prototype.map( callbackFn\[, thisArg] ) + +Returns a new array with each element being the result of a provided callback function. + +```javascript +function invert( v ) { + return !v; +} + +var arr = new BooleanArray( 3 ); + +arr.set( true, 0 ); +arr.set( false, 1 ); +arr.set( true, 2 ); + +var out = arr.map( invert ); +// returns + +var z = out.get( 0 ); +// returns false + +z = out.get( 1 ); +// returns true + +z = out.get( 2 ); +// returns false +``` + +The callback 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 invert( v, i ) { + this.count += i; + return !v; +} + +var arr = new BooleanArray( 3 ); + +var context = { + 'count': 0 +}; + +arr.set( true, 0 ); +arr.set( false, 1 ); +arr.set( true, 2 ); + +var out = arr.map( invert, context ); +// returns + +var count = context.count; +// returns 3; +``` + #### BooleanArray.prototype.set( v\[, i] ) diff --git a/bool/benchmark/benchmark.map.js b/bool/benchmark/benchmark.map.js new file mode 100644 index 00000000..7bade731 --- /dev/null +++ b/bool/benchmark/benchmark.map.js @@ -0,0 +1,55 @@ +/** +* @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+':map', function benchmark( b ) { + var out; + var arr; + var i; + + arr = new BooleanArray( [ true, false, false, true, true, false ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.map( invert ); + 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(); + + function invert( v ) { + return !v; + } +}); diff --git a/bool/benchmark/benchmark.map.length.js b/bool/benchmark/benchmark.map.length.js new file mode 100644 index 00000000..29b06c9a --- /dev/null +++ b/bool/benchmark/benchmark.map.length.js @@ -0,0 +1,104 @@ +/** +* @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 Boolean = require( '@stdlib/boolean/ctor' ); +var identity = require( '@stdlib/utils/identity-function' ); +var isBooleanArray = require( '@stdlib/assert/is-booleanarray' ); +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; i++ ) { + arr.push( Boolean( i%2 ) ); + } + arr = new BooleanArray( arr ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.map( identity ); + 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(); + } +} + + +// 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+':map:len='+len, f ); + } +} + +main(); diff --git a/bool/docs/types/index.d.ts b/bool/docs/types/index.d.ts index 902e892b..b05b4c79 100644 --- a/bool/docs/types/index.d.ts +++ b/bool/docs/types/index.d.ts @@ -62,6 +62,50 @@ type FromBinary = ( this: U, value: boolean, index: number ) => boolean; */ type FromCallback = FromNullary | FromUnary | FromBinary; +/** +* Callback invoked for each element in an array. +* +* @returns returned value +*/ +type NullaryMapFcn = ( this: U ) => any; + +/** +* Callback invoked for each element in an array. +* +* @param value - current array element +* @returns returned value +*/ +type UnaryMapFcn = ( this: U, value: boolean ) => any; + +/** +* Callback invoked for each element in an array. +* +* @param value - current array element +* @param index - current array element index +* @returns returned value +*/ +type BinaryMapFcn = ( this: U, value: boolean, index: number ) => any; + +/** +* Callback invoked for each element in an array. +* +* @param value - current array element +* @param index - current array element index +* @param arr - array on which the method was called +* @returns returned value +*/ +type TernaryMapFcn = ( this: U, value: boolean, index: number, arr: BooleanArray ) => any; + +/** +* Callback invoked for each element in an array. +* +* @param value - current array element +* @param index - current array element index +* @param arr - array on which the method was called +* @returns returned value +*/ +type MapFcn = NullaryMapFcn | UnaryMapFcn | BinaryMapFcn | TernaryMapFcn; + /** * Class for creating a Boolean array. */ @@ -194,6 +238,38 @@ declare class BooleanArray implements BooleanArrayInterface { */ get( i: number ): boolean | void; + /** + * Returns a new array with each element being the result of a provided callback function. + * + * @param fcn - callback function + * @param thisArg - callback function execution context + * @returns new boolean array + * + * @example + * function invert( v ) { + * return !v; + * } + * + * var arr = new BooleanArray( 3 ); + * + * arr.set( true, 0 ); + * arr.set( false, 1 ); + * arr.set( true, 2 ); + * + * var out = arr.map( invert ); + * // returns + * + * var v = out.get( 0 ); + * // returns false + * + * v = out.get( 1 ); + * // returns true + * + * v = out.get( 2 ); + * // returns false + */ + map( fcn: MapFcn, thisArg?: ThisParameterType> ): BooleanArray; + /** * Sets an array element. * diff --git a/bool/lib/main.js b/bool/lib/main.js index 10972642..bd636708 100644 --- a/bool/lib/main.js +++ b/bool/lib/main.js @@ -504,6 +504,61 @@ setReadOnlyAccessor( BooleanArray.prototype, 'length', function get() { return this._length; }); +/** +* Returns a new array with each element being the result of a provided callback function. +* +* @name map +* @memberof BooleanArray.prototype +* @type {Function} +* @param {Function} fcn - callback function +* @param {*} [thisArg] - callback function execution context +* @throws {TypeError} `this` must be a boolean array +* @throws {TypeError} first argument must be a function +* @returns {BooleanArray} new boolean array +* +* @example +* function invert( v ) { +* return !v; +* } +* +* var arr = new BooleanArray( 3 ); +* +* arr.set( true, 0 ); +* arr.set( false, 1 ); +* arr.set( true, 2 ); +* +* var out = arr.map( invert ); +* // returns +* +* var z = out.get( 0 ); +* // returns false +* +* z = out.get( 1 ); +* // returns true +* +* z = out.get( 2 ); +* // returns false +*/ +setReadOnly( BooleanArray.prototype, 'map', function map( fcn, thisArg ) { + var outbuf; + var out; + var buf; + var i; + if ( !isBooleanArray( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a boolean array.' ); + } + if ( !isFunction( fcn ) ) { + throw new TypeError( 'invalid argument. First argument must be a function. Value: `%s`.', fcn ); + } + buf = this._buffer; + out = new this.constructor( this._length ); + outbuf = out._buffer; // eslint-disable-line no-underscore-dangle + for ( i = 0; i < this._length; i++ ) { + outbuf[ i ] = Boolean( fcn.call( thisArg, Boolean( buf[ i ] ), i, this ) ); + } + return out; +}); + /** * Sets an array element. * diff --git a/bool/test/test.map.js b/bool/test/test.map.js new file mode 100644 index 00000000..c0c7c5b9 --- /dev/null +++ b/bool/test/test.map.js @@ -0,0 +1,164 @@ +/** +* @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 identity = require( '@stdlib/utils/identity-function' ); +var reinterpretBool = require( '@stdlib/strided/base/reinterpret-boolean' ); +var instanceOf = require( '@stdlib/assert/instance-of' ); +var Uint8Array = require( './../../uint8' ); +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 a `map` method', function test( t ) { + t.strictEqual( hasOwnProp( BooleanArray.prototype, 'map' ), true, 'has property' ); + t.strictEqual( isFunction( BooleanArray.prototype.map ), 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.map.call( value, identity ); + }; + } +}); + +tape( 'the method throws an error if provided a first argument which is not a function', function test( t ) { + var values; + var arr; + var i; + + arr = new BooleanArray( 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + 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.map( value ); + }; + } +}); + +tape( 'the method returns an empty array if operating on an empty boolean array', function test( t ) { + var arr; + var out; + + arr = new BooleanArray(); + out = arr.map( identity ); + + t.strictEqual( instanceOf( out, BooleanArray ), true, 'returns expected value' ); + t.strictEqual( out.length, 0, 'returns expected value' ); + t.notEqual( out, arr, 'returns a new instance' ); + t.end(); +}); + +tape( 'the method returns a new boolean array containing elements which are the result of a provided callback function', function test( t ) { + var expected; + var actual; + var arr; + + arr = new BooleanArray( [ true, false, false, true, true, false ] ); + expected = new Uint8Array( [ 0, 1, 1, 0, 0, 1 ] ); + actual = arr.map( invert ); + + t.strictEqual( instanceOf( actual, BooleanArray ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.notEqual( actual, arr, 'returns a new instance' ); + t.deepEqual( reinterpretBool( actual, 0 ), expected, 'returns expected value' ); + t.end(); + + function invert( v ) { + return !v; + } +}); + +tape( 'the method supports providing an execution context', function test( t ) { + var expected; + var actual; + var arr; + var ctx; + + arr = new BooleanArray( [ true, false, false, true, true, false ] ); + expected = new Uint8Array( [ 0, 1, 1, 0, 0, 1 ] ); + ctx = { + 'count': 0 + }; + actual = arr.map( invert, ctx ); + + t.strictEqual( instanceOf( actual, BooleanArray ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( reinterpretBool( actual, 0 ), expected, 'returns expected value' ); + t.strictEqual( ctx.count, 15, 'returns expected value' ); + t.end(); + + function invert( v, i ) { + this.count += i; // eslint-disable-line no-invalid-this + return !v; + } +});