From 76a8a6e136a030437ac6853bfa8fa5b262dce329 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 7 Apr 2024 21:05:06 +0000 Subject: [PATCH] Auto-generated commit --- base/README.md | 3 + base/count-if/README.md | 52 ++--- base/count-if/benchmark/benchmark.length.js | 15 +- base/count-if/docs/repl.txt | 20 +- base/count-if/docs/types/index.d.ts | 18 +- base/count-if/docs/types/test.ts | 77 ++++++- base/count-if/examples/index.js | 21 +- base/count-if/lib/index.js | 8 +- base/count-if/lib/main.js | 96 +++------ base/count-if/package.json | 2 +- base/count-if/test/test.js | 223 +++++++++----------- base/lib/index.js | 9 + 12 files changed, 290 insertions(+), 254 deletions(-) diff --git a/base/README.md b/base/README.md index 995910b2..da011a6e 100644 --- a/base/README.md +++ b/base/README.md @@ -202,6 +202,7 @@ The namespace exports the following: - [`unary5d( arrays, shape, fcn )`][@stdlib/array/base/unary5d]: apply a unary callback to elements in a five-dimensional nested input array and assign results to elements in a five-dimensional nested output array. - [`unarynd( arrays, shape, fcn )`][@stdlib/array/base/unarynd]: apply a unary callback to elements in an n-dimensional nested input array and assign results to elements in an n-dimensional nested output array. - [`unitspace( start, stop )`][@stdlib/array/base/unitspace]: generate a linearly spaced numeric array whose elements increment by 1. +- [`arrayWith( x, index, value )`][@stdlib/array/base/with]: return a new array with the element at the specified index replaced with a provided value. - [`zeroTo( n )`][@stdlib/array/base/zero-to]: generate a linearly spaced numeric array whose elements increment by 1 starting from zero. - [`zeros( len )`][@stdlib/array/base/zeros]: create a zero-filled "generic" array. - [`zeros2d( shape )`][@stdlib/array/base/zeros2d]: create a zero-filled two-dimensional nested array. @@ -565,6 +566,8 @@ console.log( objectKeys( ns ) ); [@stdlib/array/base/unitspace]: https://github.com/stdlib-js/array/tree/main/base/unitspace +[@stdlib/array/base/with]: https://github.com/stdlib-js/array/tree/main/base/with + [@stdlib/array/base/zero-to]: https://github.com/stdlib-js/array/tree/main/base/zero-to [@stdlib/array/base/zeros]: https://github.com/stdlib-js/array/tree/main/base/zeros diff --git a/base/count-if/README.md b/base/count-if/README.md index 368ab4a4..67ce5979 100644 --- a/base/count-if/README.md +++ b/base/count-if/README.md @@ -20,7 +20,7 @@ limitations under the License. # countIf -> Count the number of elements in an array that satisfy the provided testing function. +> Count the number of elements in an array which pass a test implemented by a predicate function. @@ -42,21 +42,19 @@ var countIf = require( '@stdlib/array/base/count-if' ); #### countIf( x, predicate\[, thisArg] ) -Counts the number of elements in an array that satisfy the provided testing function. +Counts the number of elements in an array which pass a test implemented by a predicate function. ```javascript -var x = [ 0, 1, 0, 1, 2 ]; - -function predicate( val ) { - return ( val % 2 === 0 ); +function predicate( value ) { + return ( value > 0 ); } +var x = [ 0, 1, 0, 1, 2 ]; + var out = countIf( x, predicate ); // returns 3 ``` -If a `predicate` function returns a truthy value, the function counts that value. - The `predicate` function is provided three arguments: - **value**: current array element. @@ -66,18 +64,22 @@ The `predicate` function is provided three arguments: To set the `predicate` function execution context, provide a `thisArg`. ```javascript -var x = [ 1, 2, 3, 4 ]; +function predicate( value ) { + this.count += 1; + return ( value > 0 ); +} + +var x = [ 0, 1, 0, 1, 2 ]; var context = { - 'target': 3 + 'count': 0 }; -function predicate( value ) { - return ( value > this.target ); -} - var out = countIf( x, predicate, context ); -// returns 1 +// returns 3 + +var cnt = context.count; +// returns 5 ``` @@ -101,19 +103,21 @@ var out = countIf( x, predicate, context ); ```javascript -var bernoulli = require( '@stdlib/random/array/bernoulli' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive; +var naryFunction = require( '@stdlib/utils/nary-function' ); var countIf = require( '@stdlib/array/base/count-if' ); -var x = bernoulli( 100, 0.5, { - 'dtype': 'generic' +var x = discreteUniform( 10, -5, 5, { + 'dtype': 'int32' }); -console.log( x ); +// returns -function predicate( val ) { - return val === 1; -} -var n = countIf( x, predicate ); -console.log( n ); +var out = countIf( x, naryFunction( isPositiveInteger, 1 ) ); +// returns + +console.log( x ); +console.log( out ); ``` diff --git a/base/count-if/benchmark/benchmark.length.js b/base/count-if/benchmark/benchmark.length.js index 13a9bc4c..aefe7cff 100644 --- a/base/count-if/benchmark/benchmark.length.js +++ b/base/count-if/benchmark/benchmark.length.js @@ -30,6 +30,17 @@ var countIf = require( './../lib' ); // FUNCTIONS // +/** +* Predicate function. +* +* @private +* @param {number} v - value +* @returns {boolean} result +*/ +function predicate( v ) { + return v > 0.0; +} + /** * Creates a benchmark function. * @@ -55,9 +66,7 @@ function createBenchmark( len ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - out = countIf( x, function predicate( v ) { - return v === 1; - } ); + out = countIf( x, predicate ); if ( typeof out !== 'number' ) { b.fail( 'should return a number' ); } diff --git a/base/count-if/docs/repl.txt b/base/count-if/docs/repl.txt index 457b1181..0767c780 100644 --- a/base/count-if/docs/repl.txt +++ b/base/count-if/docs/repl.txt @@ -1,7 +1,13 @@ {{alias}}( x, predicate[, thisArg] ) - Counts the number of elements in an array that satisfy the provided testing - function. + Counts the number of elements in an array which pass a test implemented by a + predicate function. + + The predicate function is provided three arguments: + + - value: current array element. + - index: current array element index. + - arr: the input array. Parameters ---------- @@ -9,7 +15,7 @@ Input array. predicate: Function - Testing function. + Predicate function. thisArg: any (optional) Execution context. @@ -17,14 +23,12 @@ Returns ------- out: integer - Number of truthy values for which the testing function evaluates to - true. + Result. Examples -------- - > var out = {{alias}}( [ 0, 1, 1 ], function predicate( v ) { - ... return v === 1; - ... } ) + > function f( v ) { return ( v > 0 ); }; + > var out = {{alias}}( [ 0, 1, 1 ], f ) 2 See Also diff --git a/base/count-if/docs/types/index.d.ts b/base/count-if/docs/types/index.d.ts index 719556a5..1310c1a0 100644 --- a/base/count-if/docs/types/index.d.ts +++ b/base/count-if/docs/types/index.d.ts @@ -67,20 +67,22 @@ type Ternary = ( this: U, value: T, index: number, arr: Collection | Ac type Predicate = Nullary | Unary | Binary | Ternary; /** -* Counts the number of truthy values in an array. +* Counts the number of elements in an array which pass a test implemented by a predicate function. * * @param x - input array -* @param predicate - testing function -* @param thisArg - function context -* @returns number of values for which the provided function evaluates to true +* @param predicate - predicate function +* @param thisArg - predicate function execution context +* @returns result * * @example -* var x = [ 0, 1, 0, 1 ]; * function predicate( v ) { -* return v > this; +* return v > 0; * } -* var n = countIf( x, predicate, 0 ); -* // returns 2 +* +* var x = [ 0, 1, 0, 1, 1 ]; +* +* var n = countIf( x, predicate ); +* // returns 3 */ declare function countIf( x: Collection | AccessorArrayLike, predicate: Predicate, thisArg?: ThisParameterType> ): number; diff --git a/base/count-if/docs/types/test.ts b/base/count-if/docs/types/test.ts index 44fb086b..c7b1efa7 100644 --- a/base/count-if/docs/types/test.ts +++ b/base/count-if/docs/types/test.ts @@ -16,27 +16,86 @@ * limitations under the License. */ -import countIf from './index'; +import toAccessorArray = require( './../../../../base/to-accessor-array' ); +import countIf = require( './index' ); + +/** +* Tests whether a value is positive. +* +* @param value - input value +* @returns boolean indicating whether an element is positive +*/ +function isPositive( value: number ): boolean { + return ( value > 0 ); +} // TESTS // // The function returns a number... { - countIf( [ 1, 2, 3 ], () => { return true } ); // $ExpectType number + countIf( [ 1, 2, 3 ], isPositive ); // $ExpectType number + countIf( new Float64Array( [ 1, 2, 3 ] ), isPositive ); // $ExpectType number + countIf( new Float32Array( [ 1, 2, 3 ] ), isPositive ); // $ExpectType number + countIf( new Int32Array( [ 1, 2, 3 ] ), isPositive ); // $ExpectType number + countIf( new Int16Array( [ 1, 2, 3 ] ), isPositive ); // $ExpectType number + countIf( new Int8Array( [ 1, 2, 3 ] ), isPositive ); // $ExpectType number + countIf( new Uint32Array( [ 1, 2, 3 ] ), isPositive ); // $ExpectType number + countIf( new Uint16Array( [ 1, 2, 3 ] ), isPositive ); // $ExpectType number + countIf( new Uint8Array( [ 1, 2, 3 ] ), isPositive ); // $ExpectType number + countIf( new Uint8ClampedArray( [ 1, 2, 3 ] ), isPositive ); // $ExpectType number + countIf( toAccessorArray( [ 1, 2, 3 ] ), isPositive ); // $ExpectType number + + countIf( [ 1, 2, 3 ], isPositive, {} ); // $ExpectType number + countIf( new Float64Array( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType number + countIf( new Float32Array( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType number + countIf( new Int32Array( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType number + countIf( new Int16Array( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType number + countIf( new Int8Array( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType number + countIf( new Uint32Array( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType number + countIf( new Uint16Array( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType number + countIf( new Uint8Array( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType number + countIf( new Uint8ClampedArray( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType number + countIf( toAccessorArray( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType number +} + +// The compiler throws an error if the function is provided a first argument which is not a collection... +{ + countIf( 2, isPositive ); // $ExpectError + countIf( false, isPositive ); // $ExpectError + countIf( true, isPositive ); // $ExpectError + countIf( {}, isPositive ); // $ExpectError + + countIf( 2, isPositive, {} ); // $ExpectError + countIf( false, isPositive, {} ); // $ExpectError + countIf( true, isPositive, {} ); // $ExpectError + countIf( {}, isPositive, {} ); // $ExpectError } -// The compiler throws an error if the function is provided an argument which is not a collection or the function is not boolean returning... +// The compiler throws an error if the function is provided a second argument which is not a function... { - countIf( [ 5 ], function() { return 1 } ); // $ExpectError - countIf( true ); // $ExpectError - countIf( false, function() { return false} ); // $ExpectError - countIf( null ); // $ExpectError - countIf( [ {}, {} ], ()=>{} ); // $ExpectError + countIf( [ 1, 2, 3 ], 'abc' ); // $ExpectError + countIf( [ 1, 2, 3 ], 2 ); // $ExpectError + countIf( [ 1, 2, 3 ], false ); // $ExpectError + countIf( [ 1, 2, 3 ], true ); // $ExpectError + countIf( [ 1, 2, 3 ], null ); // $ExpectError + countIf( [ 1, 2, 3 ], void 0 ); // $ExpectError + countIf( [ 1, 2, 3 ], {} ); // $ExpectError + countIf( [ 1, 2, 3 ], [] ); // $ExpectError + + countIf( [ 1, 2, 3 ], 'abc', {} ); // $ExpectError + countIf( [ 1, 2, 3 ], 2, {} ); // $ExpectError + countIf( [ 1, 2, 3 ], false, {} ); // $ExpectError + countIf( [ 1, 2, 3 ], true, {} ); // $ExpectError + countIf( [ 1, 2, 3 ], null, {} ); // $ExpectError + countIf( [ 1, 2, 3 ], void 0, {} ); // $ExpectError + countIf( [ 1, 2, 3 ], {}, {} ); // $ExpectError + countIf( [ 1, 2, 3 ], [], {} ); // $ExpectError } // The compiler throws an error if the function is provided an unsupported number of arguments... { countIf(); // $ExpectError - countIf( [ 1, 2, 3 ], 2 ); // $ExpectError + countIf( [ 1, 2, 3 ] ); // $ExpectError + countIf( [ 1, 2, 3 ], isPositive, {}, {} ); // $ExpectError } diff --git a/base/count-if/examples/index.js b/base/count-if/examples/index.js index 30d4a733..f51511b6 100644 --- a/base/count-if/examples/index.js +++ b/base/count-if/examples/index.js @@ -18,19 +18,18 @@ 'use strict'; -var bernoulli = require( '@stdlib/random/array/bernoulli' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive; +var naryFunction = require( '@stdlib/utils/nary-function' ); var countIf = require( './../lib' ); -var x = bernoulli( 100, 0.5, { - 'dtype': 'generic' +var x = discreteUniform( 10, -5, 5, { + 'dtype': 'int32' }); -console.log( x ); - -var threshold = 0; +// returns -function predicate( val ) { - return val === 1; -} +var out = countIf( x, naryFunction( isPositiveInteger, 1 ) ); +// returns -var n = countIf( x, predicate, threshold ); -console.log( n ); +console.log( x ); +console.log( out ); diff --git a/base/count-if/lib/index.js b/base/count-if/lib/index.js index af962706..876d9d45 100644 --- a/base/count-if/lib/index.js +++ b/base/count-if/lib/index.js @@ -19,19 +19,19 @@ 'use strict'; /** -* Count the number of elements in an array that satisfy the provided testing function. +* Count the number of elements in an array which pass a test implemented by a predicate function. * * @module @stdlib/array/base/count-if * * @example * var countIf = require( '@stdlib/array/base/count-if' ); * -* var x = [ 0, 1, 0, 1, 2 ]; -* * function predicate( value ) { -* return ( value % 2 === 0 ) +* return ( value > 0 ); * } * +* var x = [ 0, 1, 0, 1, 2 ]; +* * var n = countIf( x, predicate ); * // returns 3 */ diff --git a/base/count-if/lib/main.js b/base/count-if/lib/main.js index 4521abc9..41e1c01d 100644 --- a/base/count-if/lib/main.js +++ b/base/count-if/lib/main.js @@ -20,30 +20,30 @@ // MODULES // -var isComplexTypedArray = require( './../../../base/assert/is-complex-typed-array' ); var isAccessorArray = require( './../../../base/assert/is-accessor-array' ); var resolveGetter = require( './../../../base/resolve-getter' ); -var reinterpret = require( '@stdlib/strided/base/reinterpret-complex' ); // FUNCTIONS // /** -* Counts the number of elements in an indexed array that satisfy the provided testing function. +* Counts the number of elements in an indexed array which pass a test implemented by a predicate function. * * @private * @param {Collection} x - input array -* @param {Function} predicate - testing function -* @param {*} [thisArg] - function context -* @returns {NonNegativeInteger} number of values for which the provided function evaluates to true +* @param {Function} predicate - predicate function +* @param {*} thisArg - predicate function evaluation context +* @returns {NonNegativeInteger} result * * @example -* var x = [ 0, 1, 0, 1 ]; -* function predicate( v ) { -* return v > 0; +* function predicate( value ) { +* return ( value > 0 ); * } -* var n = indexed( x, predicate ); -* // returns 2 +* +* var x = [ 0, 1, 0, 1, 1 ]; +* +* var n = indexed( x, predicate, {} ); +* // returns 3 */ function indexed( x, predicate, thisArg ) { var n; @@ -59,23 +59,25 @@ function indexed( x, predicate, thisArg ) { } /** -* Counts the number of elements in an accessor array that satisfy the provided testing function. +* Counts the number of elements in an accessor array which pass a test implemented by a predicate function. * * @private * @param {Collection} x - input array -* @param {Function} predicate - testing function -* @param {*} [thisArg] - function context -* @returns {NonNegativeInteger} number of values for which the provided function evaluates to true +* @param {Function} predicate - predicate function +* @param {*} thisArg - predicate function evaluation context +* @returns {NonNegativeInteger} result * * @example * var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); * -* var x = toAccessorArray( [ 0, 1, 0, 1 ] ); -* function predicate( v ) { -* return v > 0; +* function predicate( value ) { +* return ( value > 0 ); * } -* var n = accessors( x, predicate ); -* // returns 2 +* +* var x = toAccessorArray( [ 0, 1, 0, 1, 1 ] ); +* +* var n = accessors( x, predicate, {} ); +* // returns 3 */ function accessors( x, predicate, thisArg ) { var get; @@ -93,65 +95,29 @@ function accessors( x, predicate, thisArg ) { return n; } -/** -* Counts the number of elements in a complex array that satisfy the provided testing function. -* -* @private -* @param {Collection} x - input array -* @param {Function} predicate - testing function -* @param {*} [thisArg] - function context -* @returns {NonNegativeInteger} number of values for which the provided function evaluates to true -* -* @example -* var Complex128Array = require( '@stdlib/array/complex128' ); -* -* var x = new Complex128Array( [ 1.0, 2.0, 0.0, 0.0, 3.0, 4.0, 0.0, 0.0 ] ); -* function predicate( v ) { -* return v > 0; -* } -* var n = complex( x, predicate ); -* // returns 2 -*/ -function complex( x, predicate, thisArg ) { - var view; - var n; - var i; - - view = reinterpret( x, 0 ); - - n = 0; - for ( i = 0; i < view.length; i += 2 ) { - if ( predicate.call( thisArg, view[ i ], i, view ) || predicate.call( thisArg, view[ i+1 ], i+1, view ) ) { - n += 1; - } - } - return n; -} - // MAIN // /** -* Counts the number of elements in an array that satisfy the provided testing function. +* Counts the number of elements in an array which pass a test implemented by a predicate function. * * @param {Collection} x - input array -* @param {Function} predicate - testing array -* @param {*} [thisArg] - function context -* @returns {NonNegativeInteger} number of truthy values +* @param {Function} predicate - predicate function +* @param {*} [thisArg] - predicate function evaluation context +* @returns {NonNegativeInteger} result * * @example -* var x = [ 0, 1, 0, 1, 1 ]; -* function predicate( v ) { -* return v > 0; +* function predicate( value ) { +* return ( value > 0 ); * } +* +* var x = [ 0, 1, 0, 1, 1 ]; +* * var n = countIf( x, predicate ); * // returns 3 */ function countIf( x, predicate, thisArg ) { if ( isAccessorArray( x ) ) { - if ( isComplexTypedArray( x ) ) { - return complex( x, predicate, thisArg ); - } return accessors( x, predicate, thisArg ); } return indexed( x, predicate, thisArg ); diff --git a/base/count-if/package.json b/base/count-if/package.json index 34355f2a..ec18b4b3 100644 --- a/base/count-if/package.json +++ b/base/count-if/package.json @@ -1,7 +1,7 @@ { "name": "@stdlib/array/base/count-if", "version": "0.0.0", - "description": "Count the number of elements in an array that satisfy the provided testing function.", + "description": "Count the number of elements in an array which pass a test implemented by a predicate function.", "license": "Apache-2.0", "author": { "name": "The Stdlib Authors", diff --git a/base/count-if/test/test.js b/base/count-if/test/test.js index 1c13f9f6..fe153e7e 100644 --- a/base/count-if/test/test.js +++ b/base/count-if/test/test.js @@ -24,9 +24,25 @@ var tape = require( 'tape' ); var Complex128Array = require( './../../../complex128' ); var Int32Array = require( './../../../int32' ); var toAccessorArray = require( './../../../base/to-accessor-array' ); +var real = require( '@stdlib/complex/real' ); +var imag = require( '@stdlib/complex/imag' ); var countIf = require( './../lib' ); +// FUNCTIONS // + +/** +* Predicate function. +* +* @private +* @param {number} value - value +* @returns {boolean} boolean indicating whether a value passes a test +*/ +function predicate( value ) { + return ( value > 0 ); +} + + // TESTS // tape( 'main export is a function', function test( t ) { @@ -35,197 +51,162 @@ tape( 'main export is a function', function test( t ) { t.end(); }); -tape( 'the function counts the number of truthy values based on a testing function (generic)', function test( t ) { - var expected; +tape( 'the function counts the number of elements which pass a test implemented by a predicate function (generic)', function test( t ) { var actual; var x; x = [ 0, 1, 0, 1, 2 ]; - expected = 3; - actual = countIf( x, function predicate( v ) { - return ( v % 2 === 0 ); - }); + actual = countIf( x, predicate ); - t.strictEqual( actual, expected, 'returns expected value' ); + t.strictEqual( actual, 3, 'returns expected value' ); t.end(); }); -tape( 'the function counts the number of truthy values based on a testing function (accessors)', function test( t ) { - var expected; +tape( 'the function counts the number of elements which pass a test implemented by a predicate function (accessors)', function test( t ) { var actual; var x; - x = toAccessorArray([ 0, 1, 0, 1, 2 ]); - expected = 2; - actual = countIf( x, function predicate( v ) { - return v === 1; - }); + x = toAccessorArray( [ 0, 1, 0, 1, 2 ] ); + actual = countIf( x, predicate ); - t.strictEqual( actual, expected, 'returns expected value' ); + t.strictEqual( actual, 3, 'returns expected value' ); t.end(); }); -tape( 'the function counts the number of truthy values based on a testing function (real typed array)', function test( t ) { - var expected; +tape( 'the function counts the number of elements which pass a test implemented by a predicate function (real typed array)', function test( t ) { var actual; var x; - x = new Int32Array([ 0, 1, 0, 1, 2 ]); - expected = 1; - actual = countIf( x, function predicate( v ) { - return v > 1; - }); + x = new Int32Array( [ 0, 1, 0, 1, 2 ] ); + actual = countIf( x, predicate ); - t.strictEqual( actual, expected, 'returns expected value' ); + t.strictEqual( actual, 3, 'returns expected value' ); t.end(); }); -tape( 'the function counts the number of truthy values based on a testing function (complex typed array)', function test( t ) { - var expected; +tape( 'the function counts the number of elements which pass a test implemented by a predicate function (complex typed array)', function test( t ) { var actual; var x; - x = new Complex128Array([ 0.0, 0.0, 1.0, 0.0, 3.0, 4.0, 0.0, 5.0 ]); - expected = 3; - actual = countIf( x, function predicate( v ) { - return v === 0; - }); + x = new Complex128Array( [ 0.0, 0.0, 1.0, 0.0, 3.0, 4.0, 0.0, 5.0 ] ); + actual = countIf( x, predicate ); - t.strictEqual( actual, expected, 'returns expected value' ); + t.strictEqual( actual, 1, 'returns expected value' ); t.end(); + + function predicate( value ) { + return ( + real( value ) > 0 && + imag( value ) > 0 + ); + } }); -tape( 'the function returns zero if provided an array of length `0`', function test( t ) { - var expected; +tape( 'the function returns `0` if provided an array having zero elements', function test( t ) { var actual; var x; - expected = 0; - x = []; - actual = countIf( x, function predicate( v ) { - return v === 1; - }); - t.strictEqual( actual, expected, 'returns expected value' ); - - x = toAccessorArray([]); - actual = countIf( x, function predicate( v ) { - return v === 1; - }); - t.strictEqual( actual, expected, 'returns expected value' ); - - x = new Int32Array([]); - actual = countIf( x, function predicate( v ) { - return v === 1; - }); - t.strictEqual( actual, expected, 'returns expected value' ); - - x = new Complex128Array([]); - actual = countIf( x, function predicate( v ) { - return v === 1; - }); - t.strictEqual( actual, expected, 'returns expected value' ); + actual = countIf( x, predicate ); + t.strictEqual( actual, 0, 'returns expected value' ); + + x = toAccessorArray( [] ); + actual = countIf( x, predicate ); + t.strictEqual( actual, 0, 'returns expected value' ); + + x = new Int32Array( [] ); + actual = countIf( x, predicate ); + t.strictEqual( actual, 0, 'returns expected value' ); + + x = new Complex128Array( [] ); + actual = countIf( x, predicate ); + t.strictEqual( actual, 0, 'returns expected value' ); t.end(); }); -tape( 'the function returns zero if no truthy values are found', function test( t ) { - var expected; +tape( 'the function returns `0` if no elements pass a test implemented by a predicate function (generic)', function test( t ) { var actual; var x; - expected = 0; - x = [ 0, 0, 0, 0 ]; - actual = countIf( x, function predicate( v ) { - return v === 1; - }); - t.strictEqual( actual, expected, 'returns expected value' ); + actual = countIf( x, predicate ); + t.strictEqual( actual, 0, 'returns expected value' ); t.end(); }); -tape( 'the function returns the length of the array if all values are truthy', function test( t ) { - var expected; +tape( 'the function returns `0` if no elements pass a test implemented by a predicate function (accessors)', function test( t ) { var actual; var x; - x = [ 1, 1, 1, 1 ]; - expected = x.length; - - actual = countIf( x, function predicate( v ) { - return v === 1; - }); - t.strictEqual( actual, expected, 'returns expected value' ); + x = toAccessorArray( [ 0, 0, 0, 0 ] ); + actual = countIf( x, predicate ); + t.strictEqual( actual, 0, 'returns expected value' ); t.end(); }); -tape( 'the function supports providing a custom execution context', function test( t ) { - var expected; - var context; +tape( 'the function returns the number of elements in an array if all elements pass a test implemented by a predicate function (generic)', function test( t ) { var actual; var x; - context = { - 'threshold': 2 - }; - x = [ 1, 2, 3, 4, 5 ]; - expected = 3; // Only values greater than 2 - actual = countIf( x, function predicate( v ) { - return v > this.threshold; // eslint-disable-line no-invalid-this - }, context ); + x = [ 1, 1, 1, 1 ]; + actual = countIf( x, predicate ); + + t.strictEqual( actual, x.length, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns the number of elements in an array if all elements pass a test implemented by a predicate function (accessors)', function test( t ) { + var actual; + var x; - t.strictEqual( actual, expected, 'returns expected value' ); + x = toAccessorArray( [ 1, 1, 1, 1 ] ); + actual = countIf( x, predicate ); + t.strictEqual( actual, x.length, 'returns expected value' ); t.end(); }); -tape( 'the function counts the number of objects with a custom predicate function using `thisArg`', function test( t ) { - var expected; - var thisArg; +tape( 'the function supports providing a custom execution context (generic)', function test( t ) { + var context; var actual; var x; - thisArg = { - 'target': 20 + x = [ 0, 1, 0, 1, 1 ]; + context = { + 'count': 0 }; - x = [ - { - 'name': 'John', - 'value': 10 - }, - { - 'name': 'Jane', - 'value': 20 - }, - { - 'name': 'Doe', - 'value': 30 - } - ]; - - // Count the number of objects where the value property matches the target - expected = 1; // One object have its value property equal to 20 - actual = countIf( x, function predicate( v ) { - return v.value === this.target; // eslint-disable-line no-invalid-this - }, thisArg ); - - t.strictEqual( actual, expected, 'returns expected value' ); + actual = countIf( x, predicate, context ); + t.strictEqual( actual, 3, 'returns expected value' ); + t.strictEqual( context.count, x.length, 'returns expected value' ); t.end(); + + function predicate( value ) { + this.count += 1; // eslint-disable-line no-invalid-this + return ( value > 0 ); + } }); -tape( 'the function returns zero if provided an array with no truthy values (false, null, undefined are treated as non truthy values)', function test( t ) { - var expected; +tape( 'the function supports providing a custom execution context (accessors)', function test( t ) { + var context; var actual; var x; - expected = 0; - x = [ false, 0, '', null, undefined ]; - actual = countIf( x, function predicate( v ) { - return v; // Returns truthy values - }); - t.strictEqual( actual, expected, 'returns expected value' ); + x = toAccessorArray( [ 0, 1, 0, 1, 1 ] ); + context = { + 'count': 0 + }; + actual = countIf( x, predicate, context ); + + t.strictEqual( actual, 3, 'returns expected value' ); + t.strictEqual( context.count, x.length, 'returns expected value' ); t.end(); + + function predicate( value ) { + this.count += 1; // eslint-disable-line no-invalid-this + return ( value > 0 ); + } }); diff --git a/base/lib/index.js b/base/lib/index.js index 547d41ee..c1327144 100644 --- a/base/lib/index.js +++ b/base/lib/index.js @@ -432,6 +432,15 @@ setReadOnly( ns, 'copyIndexed', require( './../../base/copy-indexed' ) ); */ setReadOnly( ns, 'countFalsy', require( './../../base/count-falsy' ) ); +/** +* @name countIf +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/array/base/count-if} +*/ +setReadOnly( ns, 'countIf', require( './../../base/count-if' ) ); + /** * @name countSameValue * @memberof ns