diff --git a/lib/node_modules/@stdlib/array/bool/README.md b/lib/node_modules/@stdlib/array/bool/README.md index 0708a55e5f0e..6fa437986951 100644 --- a/lib/node_modules/@stdlib/array/bool/README.md +++ b/lib/node_modules/@stdlib/array/bool/README.md @@ -334,6 +334,110 @@ var len = arr.length; // returns 4 ``` + + +#### BooleanArray.prototype.find( predicate\[, thisArg] ) + +Returns the first element in an array for which a predicate function returns a truthy value. + +```javascript +function predicate( v ) { + return v === true; +} + +var arr = new BooleanArray( 3 ); + +arr.set( true, 0 ); +arr.set( false, 1 ); +arr.set( true, 2 ); + +var v = arr.find( predicate ); +// returns 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( false, 0 ); +arr.set( false, 1 ); +arr.set( true, 2 ); + +var z = arr.find( predicate, context ); +// returns true + +var count = context.count; +// returns 3 +``` + + + +#### Complex64Array.prototype.findLast( predicate\[, thisArg] ) + +Returns the last element in an array for which a predicate function returns a truthy value. + +```javascript +function predicate( v ) { + return v === true; +} + +var arr = new BooleanArray( 3 ); + +arr.set( true, 0 ); +arr.set( false, 1 ); +arr.set( true, 2 ); + +var v = arr.findLast( predicate ); +// returns 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( false, 2 ); + +var z = arr.findLast( predicate, context ); +// returns true + +var count = context.count; +// returns 3 +``` + #### BooleanArray.prototype.get( i ) diff --git a/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.find.js b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.find.js new file mode 100644 index 000000000000..5401d45bd3b3 --- /dev/null +++ b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.find.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 isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var BooleanArray = require( './../lib' ); + + +// MAIN // + +bench( pkg+':find', function benchmark( b ) { + var arr; + var v; + var i; + + arr = new BooleanArray( [ true, false, false, true, true, false ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = arr.find( predicate ); + 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(); + + function predicate( v ) { + return ( v === false ); + } +}); diff --git a/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.find.length.js b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.find.length.js new file mode 100644 index 000000000000..7fd45a1f89d8 --- /dev/null +++ b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.find.length.js @@ -0,0 +1,117 @@ +/** +* @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 isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var BooleanArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Predicate function. +* +* @private +* @param {boolean} value - array element +* @param {NonNegativeInteger} idx - array element index +* @param {BooleanArray} arr - array instance +* @returns {boolean} boolean indicating whether a value passes a test +*/ +function predicate( value ) { + return ( value === false ); +} + +/** +* 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( Boolean( 1 ) ); + } + arr.push( Boolean( 0 ) ); + arr = new BooleanArray( arr ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = arr.find( predicate ); + 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(); + } +} + + +// 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+':find:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.find_last.js b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.find_last.js new file mode 100644 index 000000000000..f0878a99fc07 --- /dev/null +++ b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.find_last.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 isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var BooleanArray = require( './../lib' ); + + +// MAIN // + +bench( pkg+':findLast', function benchmark( b ) { + var arr; + var v; + var i; + + arr = new BooleanArray( [ true, false, false, true, true, false ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = arr.findLast( predicate ); + 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(); + + function predicate( v ) { + return ( v === false ); + } +}); diff --git a/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.find_last.length.js b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.find_last.length.js new file mode 100644 index 000000000000..1aef5ed9bc25 --- /dev/null +++ b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.find_last.length.js @@ -0,0 +1,117 @@ +/** +* @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 isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var BooleanArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Predicate function. +* +* @private +* @param {boolean} value - array element +* @param {NonNegativeInteger} idx - array element index +* @param {BooleanArray} arr - array instance +* @returns {boolean} boolean indicating whether a value passes a test +*/ +function predicate( value ) { + return ( value === false ); +} + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr; + var i; + + arr = []; + arr.push( Boolean( 0 ) ); + for ( i = 0; i < len-1; i++ ) { + arr.push( Boolean( 1 ) ); + } + arr = new BooleanArray( arr ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = arr.findLast( predicate ); + 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(); + } +} + + +// 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+':findLast:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts index 8219ea4c0fb3..2cff4fff06d8 100644 --- a/lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts @@ -62,6 +62,50 @@ type FromBinary = ( this: U, value: boolean, index: number ) => boolean; */ type FromCallback = FromNullary | FromUnary | FromBinary; +/** +* Checks whether an element in an array passes a test. +* +* @returns boolean indicating whether an element in an array passes a test +*/ +type NullaryPredicate = ( this: U ) => boolean; + +/** +* Checks whether an element in an array passes a test. +* +* @param value - current array element +* @returns boolean indicating whether an element in an array passes a test +*/ +type UnaryPredicate = ( this: U, value: boolean ) => boolean; + +/** +* Checks whether an element in an array passes a test. +* +* @param value - current array element +* @param index - current array element index +* @returns boolean indicating whether an element in an array passes a test +*/ +type BinaryPredicate = ( this: U, value: boolean, index: number ) => boolean; + +/** +* Checks whether an element in an array passes a test. +* +* @param value - current array element +* @param index - current array element index +* @param arr - array on which the method was called +* @returns boolean indicating whether an element in an array passes a test +*/ +type TernaryPredicate = ( this: U, value: boolean, index: number, arr: BooleanArray ) => boolean; + +/** +* Checks whether an element in an array passes a test. +* +* @param value - current array element +* @param index - current array element index +* @param arr - array on which the method was called +* @returns boolean indicating whether an element in an array passes a test +*/ +type Predicate = NullaryPredicate | UnaryPredicate | BinaryPredicate | TernaryPredicate; + /** * Callback invoked for each element in an array. * @@ -227,6 +271,52 @@ declare class BooleanArray implements BooleanArrayInterface { */ readonly length: number; + /** + * Returns the first element in an array for which a predicate function returns a truthy value. + * + * @param predicate - predicate function + * @param thisArg - predicate function execution context + * @returns array element or undefined + * + * @example + * function predicate( v ) { + * return v === true; + * } + * + * var arr = new BooleanArray( 3 ); + * + * arr.set( true, 0 ); + * arr.set( false, 1 ); + * arr.set( true, 2 ); + * + * var v = arr.find( predicate ); + * // returns true + */ + find( predicate: Predicate, thisArg?: ThisParameterType> ): boolean | void; + + /** + * Returns the last element in an array for which a predicate function returns a truthy value. + * + * @param predicate - predicate function + * @param thisArg - predicate function execution context + * @returns array element or undefined + * + * @example + * function predicate( v ) { + * return v === true; + * } + * + * var arr = new BooleanArray( 3 ); + * + * arr.set( true, 0 ); + * arr.set( false, 1 ); + * arr.set( true, 2 ); + * + * var v = arr.findLast( predicate ); + * // returns true + */ + findLast( predicate: Predicate, thisArg?: ThisParameterType> ): boolean | void; + /** * Returns an array element. * diff --git a/lib/node_modules/@stdlib/array/bool/lib/main.js b/lib/node_modules/@stdlib/array/bool/lib/main.js index 98d0b4173984..d7f0bb07c25b 100644 --- a/lib/node_modules/@stdlib/array/bool/lib/main.js +++ b/lib/node_modules/@stdlib/array/bool/lib/main.js @@ -448,6 +448,98 @@ setReadOnlyAccessor( BooleanArray.prototype, 'byteOffset', function get() { */ setReadOnly( BooleanArray.prototype, 'BYTES_PER_ELEMENT', BooleanArray.BYTES_PER_ELEMENT ); +/** +* Returns the first element in an array for which a predicate function returns a truthy value. +* +* @name find +* @memberof BooleanArray.prototype +* @type {Function} +* @param {Function} predicate - predicate function +* @param {*} [thisArg] - predicate function execution context +* @throws {TypeError} `this` must be a boolean array +* @throws {TypeError} first argument must be a function +* @returns {(boolean|void)} array element or undefined +* +* @example +* function predicate( v ) { +* return v === true; +* } +* +* var arr = new BooleanArray( 3 ); +* +* arr.set( true, 0 ); +* arr.set( false, 1 ); +* arr.set( true, 2 ); +* +* var v = arr.find( predicate ); +* // returns true +*/ +setReadOnly( BooleanArray.prototype, 'find', function find( predicate, thisArg ) { + var buf; + var v; + var i; + + if ( !isBooleanArray( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a boolean array.' ); + } + if ( !isFunction( predicate ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) ); + } + buf = this._buffer; + for ( i = 0; i < this._length; i++ ) { + v = Boolean( buf[ i ] ); + if ( predicate.call( thisArg, v, i, this ) ) { + return v; + } + } +}); + +/** +* Returns the last element in an array for which a predicate function returns a truthy value. +* +* @name findLast +* @memberof BooleanArray.prototype +* @type {Function} +* @param {Function} predicate - predicate function +* @param {*} [thisArg] - predicate function execution context +* @throws {TypeError} `this` must be a boolean array +* @throws {TypeError} first argument must be a function +* @returns {(boolean|void)} array element or undefined +* +* @example +* function predicate( v ) { +* return v === true; +* } +* +* var arr = new BooleanArray( 3 ); +* +* arr.set( true, 0 ); +* arr.set( false, 1 ); +* arr.set( true, 2 ); +* +* var v = arr.findLast( predicate ); +* // returns true +*/ +setReadOnly( BooleanArray.prototype, 'findLast', function findLast( predicate, thisArg ) { + var buf; + var v; + var i; + + if ( !isBooleanArray( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a boolean array.' ); + } + if ( !isFunction( predicate ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) ); + } + buf = this._buffer; + for ( i = this._length-1; i >= 0; i-- ) { + v = Boolean( buf[ i ] ); + if ( predicate.call( thisArg, v, i, this ) ) { + return v; + } + } +}); + /** * Returns an array element. * diff --git a/lib/node_modules/@stdlib/array/bool/test/test.find.js b/lib/node_modules/@stdlib/array/bool/test/test.find.js new file mode 100644 index 000000000000..2cd9df385843 --- /dev/null +++ b/lib/node_modules/@stdlib/array/bool/test/test.find.js @@ -0,0 +1,173 @@ +/** +* @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 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 `find` method', function test( t ) { + t.strictEqual( hasOwnProp( BooleanArray.prototype, 'find' ), true, 'has property' ); + t.strictEqual( isFunction( BooleanArray.prototype.find ), 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.find.call( value, predicate ); + }; + } + + function predicate( v ) { + return ( v === true ); + } +}); + +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.find( value ); + }; + } +}); + +tape( 'the method returns `undefined` if operating on an empty boolean array', function test( t ) { + var arr; + var v; + + arr = new BooleanArray(); + v = arr.find( predicate ); + + t.strictEqual( v, void 0, 'returns expected value' ); + t.end(); + + function predicate() { + t.fail( 'should not be invoked' ); + } +}); + +tape( 'the method returns the first element which passes a test', function test( t ) { + var arr; + var v; + + arr = new BooleanArray( [ true, false, true, false ] ); + v = arr.find( predicate ); + + t.strictEqual( v, false, 'returns expected value' ); + t.end(); + + function predicate( v ) { + return ( v === false ); + } +}); + +tape( 'the method returns `undefined` if all elements fail a test', function test( t ) { + var arr; + var v; + + arr = new BooleanArray( [ true, true, true, true, true ] ); + v = arr.find( predicate ); + + t.strictEqual( v, void 0, 'returns expected value' ); + t.end(); + + function predicate( v ) { + return ( v === false ); + } +}); + +tape( 'the method supports providing an execution context', function test( t ) { + var ctx; + var arr; + var v; + + ctx = { + 'count': 0 + }; + arr = new BooleanArray( [ true, true, false, true, false ] ); + v = arr.find( predicate, ctx ); + + t.strictEqual( v, false, 'returns expected value'); + t.strictEqual( ctx.count, 3, 'returns expected value'); + + t.end(); + + function predicate( v ) { + this.count += 1; // eslint-disable-line no-invalid-this + return ( v === false ); + } +}); diff --git a/lib/node_modules/@stdlib/array/bool/test/test.find_last.js b/lib/node_modules/@stdlib/array/bool/test/test.find_last.js new file mode 100644 index 000000000000..431bc3683d4a --- /dev/null +++ b/lib/node_modules/@stdlib/array/bool/test/test.find_last.js @@ -0,0 +1,173 @@ +/** +* @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 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 `findLast` method', function test( t ) { + t.strictEqual( hasOwnProp( BooleanArray.prototype, 'findLast' ), true, 'has property' ); + t.strictEqual( isFunction( BooleanArray.prototype.findLast ), 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.findLast.call( value, predicate ); + }; + } + + function predicate( v ) { + return ( v === false ); + } +}); + +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.findLast( value ); + }; + } +}); + +tape( 'the method returns `undefined` if operating on an empty boolean array', function test( t ) { + var arr; + var v; + + arr = new BooleanArray(); + v = arr.findLast( predicate ); + + t.strictEqual( v, void 0, 'returns expected value' ); + t.end(); + + function predicate() { + t.fail( 'should not be invoked' ); + } +}); + +tape( 'the method returns the last element which passes a test', function test( t ) { + var arr; + var v; + + arr = new BooleanArray( [ true, false, false, true, true, false ] ); + v = arr.findLast( predicate ); + + t.strictEqual( v, false, 'returns expected value' ); + t.end(); + + function predicate( v ) { + return ( v === false ); + } +}); + +tape( 'the method returns `undefined` if all elements fail a test', function test( t ) { + var arr; + var v; + + arr = new BooleanArray( [ true, true, true, true, true ] ); + v = arr.findLast( predicate ); + + t.strictEqual( v, void 0, 'returns expected value' ); + t.end(); + + function predicate( v ) { + return ( v === false ); + } +}); + +tape( 'the method supports providing an execution context', function test( t ) { + var ctx; + var arr; + var v; + + ctx = { + 'count': 0 + }; + arr = new BooleanArray( [ true, true, false, true, false ] ); + v = arr.findLast( predicate, ctx ); + + t.strictEqual( v, true, 'returns expected value'); + t.strictEqual( ctx.count, 2, 'returns expected value'); + + t.end(); + + function predicate( v ) { + this.count += 1; // eslint-disable-line no-invalid-this + return ( v === true ); + } +});