From e3f73ac1694346f63838aa45bcc442354439557e Mon Sep 17 00:00:00 2001 From: Utkarsh Date: Sat, 24 Feb 2024 22:08:59 +0530 Subject: [PATCH 01/24] added array/base/count-if --- .../@stdlib/array/base/count-if/README.md | 117 ++++++++++ .../count-if/benchmark/benchmark.length.js | 100 +++++++++ .../@stdlib/array/base/count-if/docs/repl.txt | 28 +++ .../array/base/count-if/docs/types/index.d.ts | 44 ++++ .../array/base/count-if/docs/types/test.ts | 42 ++++ .../array/base/count-if/examples/index.js | 34 +++ .../@stdlib/array/base/count-if/lib/index.js | 44 ++++ .../@stdlib/array/base/count-if/lib/main.js | 155 ++++++++++++++ .../@stdlib/array/base/count-if/package.json | 65 ++++++ .../@stdlib/array/base/count-if/test/test.js | 202 ++++++++++++++++++ 10 files changed, 831 insertions(+) create mode 100644 lib/node_modules/@stdlib/array/base/count-if/README.md create mode 100644 lib/node_modules/@stdlib/array/base/count-if/benchmark/benchmark.length.js create mode 100644 lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/array/base/count-if/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/array/base/count-if/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/array/base/count-if/examples/index.js create mode 100644 lib/node_modules/@stdlib/array/base/count-if/lib/index.js create mode 100644 lib/node_modules/@stdlib/array/base/count-if/lib/main.js create mode 100644 lib/node_modules/@stdlib/array/base/count-if/package.json create mode 100644 lib/node_modules/@stdlib/array/base/count-if/test/test.js diff --git a/lib/node_modules/@stdlib/array/base/count-if/README.md b/lib/node_modules/@stdlib/array/base/count-if/README.md new file mode 100644 index 000000000000..e2bdeabf647e --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/count-if/README.md @@ -0,0 +1,117 @@ + + +# countIf + +> Counts the number of elements in an array that satisfy the provided testing function. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +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. + +```javascript +var x = [ 0, 1, 0, 1, 2 ]; + +var predicate = function isEven( value ){ return ( value % 2 === 0 ) }; + +var out = countIf( x, predicate ); +// returns 3 +``` + +
+ + + + + +
+ +
+ + + + + +
+ +## Examples + + + +```javascript +var bernoulli = require( '@stdlib/random/array/bernoulli' ); +var countIf = require( '@stdlib/array/base/count-if' ); + +var x = bernoulli( 100, 0.5, { + 'dtype': 'generic' +}); +console.log( x ); + +var predicate = function ( val ) { return val === 1 } + +var n = countIf( x, predicate ); +console.log( n ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/array/base/count-if/benchmark/benchmark.length.js b/lib/node_modules/@stdlib/array/base/count-if/benchmark/benchmark.length.js new file mode 100644 index 000000000000..b756749adb38 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/count-if/benchmark/benchmark.length.js @@ -0,0 +1,100 @@ +/** +* @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 isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; +var bernoulli = require( '@stdlib/random/array/bernoulli' ); +var pkg = require( './../package.json' ).name; +var countIf = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = bernoulli( len, 0.5, { + 'dtype': 'generic' + }); + 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 = countIf( x, function( v ) { + return v === 1; + } ); + if ( typeof out !== 'number' ) { + b.fail( 'should return a number' ); + } + } + b.toc(); + if ( !isNonNegativeInteger( out ) ) { + b.fail( 'should return a nonnegative integer' ); + } + 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+':dtype=generic,len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt b/lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt new file mode 100644 index 000000000000..1ee74114e94a --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt @@ -0,0 +1,28 @@ + +{{alias}}( x, predicate[, thisArg] ) + Counts the number of elements in an array that satisfy the provided testing function. + + Parameters + ---------- + x: ArrayLikeObject + Input array. + + predicate: Function + Testing function. + + thisArg: any (optional) + Input function's context. + + Returns + ------- + out: integer + Number of truthy values for which the testing function evaluates to true. + + Examples + -------- + > var out = {{alias}}( [ 0, 1, 1 ], function( v ){ return v === 1 } ) + 2 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/array/base/count-if/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/base/count-if/docs/types/index.d.ts new file mode 100644 index 000000000000..511140891a9f --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/count-if/docs/types/index.d.ts @@ -0,0 +1,44 @@ +/* +* @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. +*/ + +// TypeScript Version: 4.1 + +/// + +import { Collection } from '@stdlib/types/array'; + +/** +* Counts the number of truthy values in an array. +* +* @param { Collection } x - input array +* @param { ( ...args: any[] ) => boolean } predicate - testing function +* @param { * } [ thisArg ] - function context +* @returns number of values for which the provided function evaluates to true +* +* @example +* var x = [ 0, 1, 0, 1 ]; +* var predicate = function( v, t ){ return v > t; } +* var n = indexed( x, predicate, 0 ); +* // returns 2 +*/ +declare function countIf( x: Collection, predicate: ( ...args: any[] ) => boolean, thisArg?: any ): number; + + +// EXPORTS // + +export = countIf; diff --git a/lib/node_modules/@stdlib/array/base/count-if/docs/types/test.ts b/lib/node_modules/@stdlib/array/base/count-if/docs/types/test.ts new file mode 100644 index 000000000000..70d4ac100a2b --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/count-if/docs/types/test.ts @@ -0,0 +1,42 @@ +/* +* @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. +*/ + +import countIf from './index'; + + +// TESTS // + +// The function returns a number... +{ + countIf( [ 1, 2, 3 ], ()=>{ return true } ); // $ExpectType number +} + +// The compiler throws an error if the function is provided an argument which is not a collection or the function is not boolean returning... +{ + countIf( [ 5 ], function(){ return 1 } ); // $ExpectError + countIf( true ); // $ExpectError + countIf( false, function(){ return false} ); // $ExpectError + countIf( null ); // $ExpectError + countIf( [ {}, {} ], ()=>{} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + countIf(); // $ExpectError + countIf( [ 1, 2, 3 ], 2 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/array/base/count-if/examples/index.js b/lib/node_modules/@stdlib/array/base/count-if/examples/index.js new file mode 100644 index 000000000000..858c761f84bf --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/count-if/examples/index.js @@ -0,0 +1,34 @@ +/** +* @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'; + +var bernoulli = require( '@stdlib/random/array/bernoulli' ); +var countIf = require( './../lib' ); + +var x = bernoulli( 100, 0.5, { + 'dtype': 'generic' +}); +console.log( x ); + +var big = 10; + +var predicate = function isBig( val, big ) { return (val > big) } + +var n = countIf( x, predicate, big ); +console.log( n ); diff --git a/lib/node_modules/@stdlib/array/base/count-if/lib/index.js b/lib/node_modules/@stdlib/array/base/count-if/lib/index.js new file mode 100644 index 000000000000..989418d6195b --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/count-if/lib/index.js @@ -0,0 +1,44 @@ +/** +* @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'; + +/** +* Counts the number of elements in an array that satisfy the provided testing function. +* +* @module @stdlib/array/base/count-if +* +* @example +* var countIf = require( '@stdlib/array/base/count-if' ); +* +* var x = [ 0, 1, 0, 1, 2 ]; +* +* var predicate = function isEven( value ){ return ( value % 2 === 0 ) }; +* +* var n = countIf( x, predicate ); +* // returns 3 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/array/base/count-if/lib/main.js b/lib/node_modules/@stdlib/array/base/count-if/lib/main.js new file mode 100644 index 000000000000..a95692e8fcf4 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/count-if/lib/main.js @@ -0,0 +1,155 @@ +/** +* @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 isComplexTypedArray = require( '@stdlib/array/base/assert/is-complex-typed-array' ); +var isAccessorArray = require( '@stdlib/array/base/assert/is-accessor-array' ); +var resolveGetter = require( '@stdlib/array/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. +* +* @private +* @param {Collection} x - input array +* @param {(...args: any[]) => boolean} predicate - testing array +* @param {*} [thisArg] - function context +* @returns {NonNegativeInteger} number of values for which the provided function evaluates to true +* +* @example +* var x = [ 0, 1, 0, 1 ]; +* var predicate = function( v, thisArg ){ return v > thisArg; } +* var n = indexed( x, predicate, 0 ); +* // returns 2 +*/ +function indexed( x, predicate, thisArg ) { + var n; + var i; + + n = 0; + for ( i = 0; i < x.length; i++ ) { + if ( predicate( x[ i ], thisArg ) ) { + n += 1; + } + } + return n; +} + +/** +* Counts the number of elements in an accessor array that satisfy the provided testing function. +* +* @private +* @param {Collection} x - input array +* @param {(...args: any[]) => boolean} predicate - testing array +* @param {*} [thisArg] - function context +* @returns {NonNegativeInteger} number of values for which the provided function evaluates to true +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* +* var x = toAccessorArray( [ 0, 1, 0, 1 ] ); +* var predicate = function( v, thisArg ){ return v > thisArg; } +* var n = accessors( x, predicate, 0 ); +* // returns 2 +*/ +function accessors( x, predicate, thisArg ) { + var get; + var n; + var i; + + get = resolveGetter( x ); + + n = 0; + for ( i = 0; i < x.length; i++ ) { + if ( predicate( get( x, i ), thisArg ) ) { + n += 1; + } + } + return n; +} + +/** +* Counts the number of elements in a complex array that satisfy the provided testing function. +* +* @private +* @param {Collection} x - input array +* @param {(...args: any[]) => boolean} predicate - testing array +* @param {*} [thisArg] - function context +* @returns {NonNegativeInteger} number of values for which the provided function evalutes 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 ] ); +* var predicate = function( v, thisArg ){ return v > thisArg; } +* var n = complex( x ); +* // 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( view[ i ], thisArg ) || predicate( view[ i+1 ], thisArg ) ) { + n += 1; + } + } + return n; +} + + +// MAIN // + +/** +* Counts the number of elements in an array that satisfy the provided testing function. +* +* @param {Collection} x - input array +* @param {(...args: any[]) => boolean} predicate - testing array +* @param {*} [thisArg] - function context +* @returns {NonNegativeInteger} number of truthy values +* +* @example +* var x = [ 0, 1, 0, 1, 1 ]; +* var predicate = function( v, thisArg ){ return v > thisArg; } +* var n = countTruthy( x ); +* // 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 ); +} + + +// EXPORTS // + +module.exports = countIf; diff --git a/lib/node_modules/@stdlib/array/base/count-if/package.json b/lib/node_modules/@stdlib/array/base/count-if/package.json new file mode 100644 index 000000000000..3f78463d152a --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/count-if/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/array/base/count-if", + "version": "0.0.0", + "description": "Counts the number of elements in an array that satisfy the provided testing function.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdtypes", + "types", + "data", + "structure", + "array", + "count", + "sum", + "summation", + "countif", + "total", + "truthy" + ] +} diff --git a/lib/node_modules/@stdlib/array/base/count-if/test/test.js b/lib/node_modules/@stdlib/array/base/count-if/test/test.js new file mode 100644 index 000000000000..89342ae8b58e --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/count-if/test/test.js @@ -0,0 +1,202 @@ +/** +* @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 Complex128Array = require( '@stdlib/array/complex128' ); +var Int32Array = require( '@stdlib/array/int32' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var countIf = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof countIf, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function counts the number of truthy values based on a testing function (generic)', function test( t ) { + var expected; + var actual; + var x; + + x = [ 0, 1, 0, 1, 2 ]; + expected = 3; + actual = countIf( x, function( v, thisArg ) { + return ( v % thisArg === 0 ); + }, 2 ); + + t.strictEqual( actual, expected, '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; + var actual; + var x; + + x = toAccessorArray([ 0, 1, 0, 1, 2 ]); + expected = 2; + actual = countIf( x, function( v ) { + return v === 1; + }); + + t.strictEqual( actual, expected, '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; + var actual; + var x; + + x = new Int32Array([ 0, 1, 0, 1, 2 ]); + expected = 1; + actual = countIf( x, function( v, thisArg ) { + return v > thisArg; + }, 1 ); + + t.strictEqual( actual, expected, '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; + 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( v ) { + return v === 0; + }); + + t.strictEqual( actual, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns zero if provided an array of length `0`', function test( t ) { + var expected; + var actual; + var x; + + expected = 0; + + x = []; + actual = countIf( x, function( v ) { + return v === 1; + }); + t.strictEqual( actual, expected, 'returns expected value' ); + + x = toAccessorArray([]); + actual = countIf( x, function( v ) { + return v === 1; + }); + t.strictEqual( actual, expected, 'returns expected value' ); + + x = new Int32Array([]); + actual = countIf( x, function( v ) { + return v === 1; + }); + t.strictEqual( actual, expected, 'returns expected value' ); + + x = new Complex128Array([]); + actual = countIf( x, function( v ) { + return v === 1; + }); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns zero if no truthy values are found', function test( t ) { + var expected; + var actual; + var x; + + expected = 0; + + x = [ 0, 0, 0, 0 ]; + actual = countIf( x, function( v ) { + return v === 1; + }); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns the length of the array if all values are truthy', function test( t ) { + var expected; + var actual; + var x; + + x = [ 1, 1, 1, 1 ]; + expected = x.length; + + actual = countIf( x, function( v ) { + return v === 1; + }); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing a custom execution context', function test( t ) { + var expected; + var actual; + var x; + var threshold = 2; + + x = [ 1, 2, 3, 4, 5 ]; + expected = 3; // Only values greater than 2 + actual = countIf( x, function( v, thisArg ) { + return v > thisArg; + }, threshold ); + + t.strictEqual( actual, expected, '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 actual; + var x; + var target = 20; + + 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( v, thisArg ) { + return v.value === thisArg; + }, target ); + + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); From 64c19d465db2f7f64b71e973f036a1a01303ff19 Mon Sep 17 00:00:00 2001 From: Utkarsh Date: Sun, 25 Feb 2024 12:03:52 +0530 Subject: [PATCH 02/24] fixed issues & implementation of predicate functions at all places --- .../@stdlib/array/base/count-if/README.md | 36 +++++++++++++-- .../@stdlib/array/base/count-if/docs/repl.txt | 2 +- .../array/base/count-if/docs/types/index.d.ts | 6 ++- .../array/base/count-if/examples/index.js | 9 ++-- .../@stdlib/array/base/count-if/lib/index.js | 6 ++- .../@stdlib/array/base/count-if/lib/main.js | 32 +++++++++----- .../@stdlib/array/base/count-if/package.json | 2 +- .../@stdlib/array/base/count-if/test/test.js | 44 +++++++++++++------ 8 files changed, 98 insertions(+), 39 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/count-if/README.md b/lib/node_modules/@stdlib/array/base/count-if/README.md index e2bdeabf647e..b89b21bf6981 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/README.md +++ b/lib/node_modules/@stdlib/array/base/count-if/README.md @@ -40,19 +40,46 @@ limitations under the License. var countIf = require( '@stdlib/array/base/count-if' ); ``` -#### countIf( x, predicate[, thisArg] ) +#### countIf( x, predicate\[, thisArg] ) Counts the number of elements in an array that satisfy the provided testing function. ```javascript var x = [ 0, 1, 0, 1, 2 ]; -var predicate = function isEven( value ){ return ( value % 2 === 0 ) }; +function predicate( val ) { + return ( val % 2 === 0; ) +}; 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. +- **index**: current array element index. +- **arr**: input array. + +To set the `predicate` function execution context, provide a `thisArg`. + +```javascript +function predicate( value ) { + return ( value > this.target ); +} + +var x = [ 1, 2, 3, 4 ]; + +var context = { + target: 3 +}; + +var out = countIf( x, predicate, context ); +// returns 1 +``` + @@ -82,8 +109,9 @@ var x = bernoulli( 100, 0.5, { }); console.log( x ); -var predicate = function ( val ) { return val === 1 } - +function predicate( val ) { + return val === 1; +} var n = countIf( x, predicate ); console.log( n ); ``` diff --git a/lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt b/lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt index 1ee74114e94a..9aa0032b057a 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt +++ b/lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt @@ -11,7 +11,7 @@ Testing function. thisArg: any (optional) - Input function's context. + Execution context. Returns ------- diff --git a/lib/node_modules/@stdlib/array/base/count-if/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/base/count-if/docs/types/index.d.ts index 511140891a9f..f101af0a7096 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/array/base/count-if/docs/types/index.d.ts @@ -32,8 +32,10 @@ import { Collection } from '@stdlib/types/array'; * * @example * var x = [ 0, 1, 0, 1 ]; -* var predicate = function( v, t ){ return v > t; } -* var n = indexed( x, predicate, 0 ); +* var predicate = function( v ) { +* return v > this; +* } +* var n = indexed( x, predicate ); * // returns 2 */ declare function countIf( x: Collection, predicate: ( ...args: any[] ) => boolean, thisArg?: any ): number; diff --git a/lib/node_modules/@stdlib/array/base/count-if/examples/index.js b/lib/node_modules/@stdlib/array/base/count-if/examples/index.js index 858c761f84bf..f50d77c2342f 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/examples/index.js +++ b/lib/node_modules/@stdlib/array/base/count-if/examples/index.js @@ -26,9 +26,12 @@ var x = bernoulli( 100, 0.5, { }); console.log( x ); -var big = 10; +var threshold = 0; -var predicate = function isBig( val, big ) { return (val > big) } +function predicate( val ) { + return (val > this) +} -var n = countIf( x, predicate, big ); +var n = countIf( x, predicate, threshold ); console.log( n ); + diff --git a/lib/node_modules/@stdlib/array/base/count-if/lib/index.js b/lib/node_modules/@stdlib/array/base/count-if/lib/index.js index 989418d6195b..f67d16445ffd 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/lib/index.js +++ b/lib/node_modules/@stdlib/array/base/count-if/lib/index.js @@ -19,7 +19,7 @@ 'use strict'; /** -* Counts the number of elements in an array that satisfy the provided testing function. +* Count the number of elements in an array that satisfy the provided testing function. * * @module @stdlib/array/base/count-if * @@ -28,7 +28,9 @@ * * var x = [ 0, 1, 0, 1, 2 ]; * -* var predicate = function isEven( value ){ return ( value % 2 === 0 ) }; +* function predicate( value ) { +* return ( value % 2 === 0 ) +* }; * * var n = countIf( x, predicate ); * // returns 3 diff --git a/lib/node_modules/@stdlib/array/base/count-if/lib/main.js b/lib/node_modules/@stdlib/array/base/count-if/lib/main.js index a95692e8fcf4..4a472ccd1936 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/lib/main.js +++ b/lib/node_modules/@stdlib/array/base/count-if/lib/main.js @@ -39,8 +39,10 @@ var reinterpret = require( '@stdlib/strided/base/reinterpret-complex' ); * * @example * var x = [ 0, 1, 0, 1 ]; -* var predicate = function( v, thisArg ){ return v > thisArg; } -* var n = indexed( x, predicate, 0 ); +* function predicate( v ) { +* return v > 0; +* } +* var n = indexed( x, predicate ); * // returns 2 */ function indexed( x, predicate, thisArg ) { @@ -49,7 +51,7 @@ function indexed( x, predicate, thisArg ) { n = 0; for ( i = 0; i < x.length; i++ ) { - if ( predicate( x[ i ], thisArg ) ) { + if ( predicate.call( thisArg, x[ i ], i, x ) ) { n += 1; } } @@ -69,8 +71,10 @@ function indexed( x, predicate, thisArg ) { * var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); * * var x = toAccessorArray( [ 0, 1, 0, 1 ] ); -* var predicate = function( v, thisArg ){ return v > thisArg; } -* var n = accessors( x, predicate, 0 ); +* function predicate( v ) { +* return v > 0; +* } +* var n = accessors( x, predicate ); * // returns 2 */ function accessors( x, predicate, thisArg ) { @@ -82,7 +86,7 @@ function accessors( x, predicate, thisArg ) { n = 0; for ( i = 0; i < x.length; i++ ) { - if ( predicate( get( x, i ), thisArg ) ) { + if ( predicate.call( thisArg, get( x, i ), i, x ) ) { n += 1; } } @@ -96,14 +100,16 @@ function accessors( x, predicate, thisArg ) { * @param {Collection} x - input array * @param {(...args: any[]) => boolean} predicate - testing array * @param {*} [thisArg] - function context -* @returns {NonNegativeInteger} number of values for which the provided function evalutes to true +* @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 ] ); -* var predicate = function( v, thisArg ){ return v > thisArg; } -* var n = complex( x ); +* function predicate( v ) { +* return v > 0; +* } +* var n = complex( x, predicate ); * // returns 2 */ function complex( x, predicate, thisArg ) { @@ -115,7 +121,7 @@ function complex( x, predicate, thisArg ) { n = 0; for ( i = 0; i < view.length; i += 2 ) { - if ( predicate( view[ i ], thisArg ) || predicate( view[ i+1 ], thisArg ) ) { + if ( predicate.call( thisArg, view[ i ], i, view ) || predicate.call( thisArg, view[ i+1 ], i+1, view ) ) { n += 1; } } @@ -135,8 +141,10 @@ function complex( x, predicate, thisArg ) { * * @example * var x = [ 0, 1, 0, 1, 1 ]; -* var predicate = function( v, thisArg ){ return v > thisArg; } -* var n = countTruthy( x ); +* function predicate( v ) { +* return v > 0; +* } +* var n = countIf( x, predicate ); * // returns 3 */ function countIf( x, predicate, thisArg ) { diff --git a/lib/node_modules/@stdlib/array/base/count-if/package.json b/lib/node_modules/@stdlib/array/base/count-if/package.json index 3f78463d152a..34355f2ab3b7 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/package.json +++ b/lib/node_modules/@stdlib/array/base/count-if/package.json @@ -1,7 +1,7 @@ { "name": "@stdlib/array/base/count-if", "version": "0.0.0", - "description": "Counts the number of elements in an array that satisfy the provided testing function.", + "description": "Count the number of elements in an array that satisfy the provided testing function.", "license": "Apache-2.0", "author": { "name": "The Stdlib Authors", diff --git a/lib/node_modules/@stdlib/array/base/count-if/test/test.js b/lib/node_modules/@stdlib/array/base/count-if/test/test.js index 89342ae8b58e..f30e78b9d5c2 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/test/test.js +++ b/lib/node_modules/@stdlib/array/base/count-if/test/test.js @@ -39,12 +39,13 @@ tape( 'the function counts the number of truthy values based on a testing functi var expected; var actual; var x; + var thisArg = 2; x = [ 0, 1, 0, 1, 2 ]; expected = 3; - actual = countIf( x, function( v, thisArg ) { - return ( v % thisArg === 0 ); - }, 2 ); + actual = countIf( x, function( v ) { + return ( v % this === 0 ); + }, thisArg ); t.strictEqual( actual, expected, 'returns expected value' ); t.end(); @@ -69,12 +70,13 @@ tape( 'the function counts the number of truthy values based on a testing functi var expected; var actual; var x; + var thisArg = 1; x = new Int32Array([ 0, 1, 0, 1, 2 ]); expected = 1; - actual = countIf( x, function( v, thisArg ) { - return v > thisArg; - }, 1 ); + actual = countIf( x, function( v ) { + return v > this; + }, thisArg ); t.strictEqual( actual, expected, 'returns expected value' ); t.end(); @@ -165,13 +167,15 @@ tape( 'the function supports providing a custom execution context', function tes var expected; var actual; var x; - var threshold = 2; + var context = { + threshold : 2 + }; x = [ 1, 2, 3, 4, 5 ]; expected = 3; // Only values greater than 2 - actual = countIf( x, function( v, thisArg ) { - return v > thisArg; - }, threshold ); + actual = countIf( x, function( v ) { + return v > this.threshold; + }, context ); t.strictEqual( actual, expected, 'returns expected value' ); @@ -183,7 +187,9 @@ tape( 'the function counts the number of objects with a custom predicate functio var expected; var actual; var x; - var target = 20; + var thisArg = { + target : 20 + }; x = [ { name: 'John', value: 10 }, @@ -192,11 +198,21 @@ tape( 'the function counts the number of objects with a custom predicate functio ]; // 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( v, thisArg ) { - return v.value === thisArg; - }, target ); + actual = countIf( x, function( v ) { + return v.value === this.target; + }, thisArg ); t.strictEqual( actual, expected, 'returns expected value' ); t.end(); }); + +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 = 0; + var x = [ false, 0, '', null, undefined ]; + var actual = countIf( x, function( v ) { + return v; // Returns truthy values + }); + t.strictEqual( actual, expected, 'returns expected value' ); + t.end(); +}); From cc6be338c898ba306e77c5f22c47f2a7449ce8b4 Mon Sep 17 00:00:00 2001 From: Utkarsh Date: Tue, 27 Feb 2024 08:12:18 +0530 Subject: [PATCH 03/24] fixed linting issues --- .../@stdlib/array/base/count-if/README.md | 16 ++++++++-------- .../@stdlib/array/base/count-if/docs/repl.txt | 12 ++++++++---- .../array/base/count-if/docs/types/index.d.ts | 12 ++++++------ .../@stdlib/array/base/count-if/lib/index.js | 4 ++-- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/count-if/README.md b/lib/node_modules/@stdlib/array/base/count-if/README.md index b89b21bf6981..78877e4050f8 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/README.md +++ b/lib/node_modules/@stdlib/array/base/count-if/README.md @@ -48,7 +48,7 @@ Counts the number of elements in an array that satisfy the provided testing func var x = [ 0, 1, 0, 1, 2 ]; function predicate( val ) { - return ( val % 2 === 0; ) + return ( val % 2 === 0 ) }; var out = countIf( x, predicate ); @@ -66,16 +66,16 @@ The `predicate` function is provided three arguments: To set the `predicate` function execution context, provide a `thisArg`. ```javascript -function predicate( value ) { - return ( value > this.target ); -} - var x = [ 1, 2, 3, 4 ]; var context = { - target: 3 + `target`: 3 }; +function predicate( value ) { + return ( value > this.target ); +} + var out = countIf( x, predicate, context ); // returns 1 ``` @@ -109,8 +109,8 @@ var x = bernoulli( 100, 0.5, { }); console.log( x ); -function predicate( val ) { - return val === 1; +function predicate( val ) { + return val === 1; } var n = countIf( x, predicate ); console.log( n ); diff --git a/lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt b/lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt index 9aa0032b057a..8c1afebab056 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt +++ b/lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt @@ -1,12 +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 + that satisfy the provided testing function. Parameters ---------- x: ArrayLikeObject Input array. - + predicate: Function Testing function. @@ -16,11 +17,14 @@ Returns ------- out: integer - Number of truthy values for which the testing function evaluates to true. + Number of truthy values for which + the testing function evaluates to true. Examples -------- - > var out = {{alias}}( [ 0, 1, 1 ], function( v ){ return v === 1 } ) + > var out = {{alias}}( [ 0, 1, 1 ], function( v ){ + return v === 1 + } ) 2 See Also diff --git a/lib/node_modules/@stdlib/array/base/count-if/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/base/count-if/docs/types/index.d.ts index f101af0a7096..0aa52dd0f4a2 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/array/base/count-if/docs/types/index.d.ts @@ -25,20 +25,20 @@ import { Collection } from '@stdlib/types/array'; /** * Counts the number of truthy values in an array. * -* @param { Collection } x - input array -* @param { ( ...args: any[] ) => boolean } predicate - testing function -* @param { * } [ thisArg ] - function context +* @param x - input array +* @param predicate - testing function +* @param thisArg - function context * @returns number of values for which the provided function evaluates to true * * @example * var x = [ 0, 1, 0, 1 ]; -* var predicate = function( v ) { -* return v > this; +* var predicate = function( v ) { +* return v > this; * } * var n = indexed( x, predicate ); * // returns 2 */ -declare function countIf( x: Collection, predicate: ( ...args: any[] ) => boolean, thisArg?: any ): number; +declare function countIf( x: Collection, predicate: ( ...args: Array ) => boolean, thisArg?: any ): number; // EXPORTS // diff --git a/lib/node_modules/@stdlib/array/base/count-if/lib/index.js b/lib/node_modules/@stdlib/array/base/count-if/lib/index.js index f67d16445ffd..5d3daa78ab33 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/lib/index.js +++ b/lib/node_modules/@stdlib/array/base/count-if/lib/index.js @@ -28,8 +28,8 @@ * * var x = [ 0, 1, 0, 1, 2 ]; * -* function predicate( value ) { -* return ( value % 2 === 0 ) +* function predicate( value ) { +* return ( value % 2 === 0 ) * }; * * var n = countIf( x, predicate ); From 2bde74a455bee363fb603dc2cabb91aa4754c2fd Mon Sep 17 00:00:00 2001 From: Utkarsh <137638507+Ut-the-pro@users.noreply.github.com> Date: Sat, 2 Mar 2024 08:56:23 +0530 Subject: [PATCH 04/24] Update lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt Co-authored-by: Philipp Burckhardt Signed-off-by: Utkarsh <137638507+Ut-the-pro@users.noreply.github.com> --- lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt b/lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt index 8c1afebab056..092f6eee5998 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt +++ b/lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt @@ -22,9 +22,9 @@ Examples -------- - > var out = {{alias}}( [ 0, 1, 1 ], function( v ){ - return v === 1 - } ) + > var out = {{alias}}( [ 0, 1, 1 ], function predicate( v ) { + ... return v === 1; + ... } ) 2 See Also From 2e623ce131190e84b6daf1aef260da8aa117bfde Mon Sep 17 00:00:00 2001 From: Utkarsh <137638507+Ut-the-pro@users.noreply.github.com> Date: Sat, 2 Mar 2024 08:56:37 +0530 Subject: [PATCH 05/24] Update lib/node_modules/@stdlib/array/base/count-if/lib/index.js Co-authored-by: Philipp Burckhardt Signed-off-by: Utkarsh <137638507+Ut-the-pro@users.noreply.github.com> --- lib/node_modules/@stdlib/array/base/count-if/lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/base/count-if/lib/index.js b/lib/node_modules/@stdlib/array/base/count-if/lib/index.js index 5d3daa78ab33..af962706c435 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/lib/index.js +++ b/lib/node_modules/@stdlib/array/base/count-if/lib/index.js @@ -30,7 +30,7 @@ * * function predicate( value ) { * return ( value % 2 === 0 ) -* }; +* } * * var n = countIf( x, predicate ); * // returns 3 From 516f915f7ba80941c975cb27f8e943da869cca78 Mon Sep 17 00:00:00 2001 From: Utkarsh <137638507+Ut-the-pro@users.noreply.github.com> Date: Sat, 2 Mar 2024 08:56:54 +0530 Subject: [PATCH 06/24] Update lib/node_modules/@stdlib/array/base/count-if/lib/main.js Co-authored-by: Philipp Burckhardt Signed-off-by: Utkarsh <137638507+Ut-the-pro@users.noreply.github.com> --- lib/node_modules/@stdlib/array/base/count-if/lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/base/count-if/lib/main.js b/lib/node_modules/@stdlib/array/base/count-if/lib/main.js index 4a472ccd1936..cec81316803d 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/lib/main.js +++ b/lib/node_modules/@stdlib/array/base/count-if/lib/main.js @@ -33,7 +33,7 @@ var reinterpret = require( '@stdlib/strided/base/reinterpret-complex' ); * * @private * @param {Collection} x - input array -* @param {(...args: any[]) => boolean} predicate - testing array +* @param {Function} predicate - testing function * @param {*} [thisArg] - function context * @returns {NonNegativeInteger} number of values for which the provided function evaluates to true * From e8b86618cf603567f2b2378407ad714a3219d580 Mon Sep 17 00:00:00 2001 From: Utkarsh <137638507+Ut-the-pro@users.noreply.github.com> Date: Sat, 2 Mar 2024 08:57:13 +0530 Subject: [PATCH 07/24] Update lib/node_modules/@stdlib/array/base/count-if/lib/main.js Co-authored-by: Philipp Burckhardt Signed-off-by: Utkarsh <137638507+Ut-the-pro@users.noreply.github.com> --- lib/node_modules/@stdlib/array/base/count-if/lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/base/count-if/lib/main.js b/lib/node_modules/@stdlib/array/base/count-if/lib/main.js index cec81316803d..ef5f5690b119 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/lib/main.js +++ b/lib/node_modules/@stdlib/array/base/count-if/lib/main.js @@ -63,7 +63,7 @@ function indexed( x, predicate, thisArg ) { * * @private * @param {Collection} x - input array -* @param {(...args: any[]) => boolean} predicate - testing array +* @param {Function} predicate - testing function * @param {*} [thisArg] - function context * @returns {NonNegativeInteger} number of values for which the provided function evaluates to true * From a942040e1889d157c5ece3e2aa9448831e0bddfe Mon Sep 17 00:00:00 2001 From: Utkarsh <137638507+Ut-the-pro@users.noreply.github.com> Date: Sat, 2 Mar 2024 08:57:19 +0530 Subject: [PATCH 08/24] Update lib/node_modules/@stdlib/array/base/count-if/lib/main.js Co-authored-by: Philipp Burckhardt Signed-off-by: Utkarsh <137638507+Ut-the-pro@users.noreply.github.com> --- lib/node_modules/@stdlib/array/base/count-if/lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/base/count-if/lib/main.js b/lib/node_modules/@stdlib/array/base/count-if/lib/main.js index ef5f5690b119..052972896af1 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/lib/main.js +++ b/lib/node_modules/@stdlib/array/base/count-if/lib/main.js @@ -98,7 +98,7 @@ function accessors( x, predicate, thisArg ) { * * @private * @param {Collection} x - input array -* @param {(...args: any[]) => boolean} predicate - testing array +* @param {Function} predicate - testing function * @param {*} [thisArg] - function context * @returns {NonNegativeInteger} number of values for which the provided function evaluates to true * From 37ae175773088e0a964ff2fc98917058fdac9404 Mon Sep 17 00:00:00 2001 From: Utkarsh Gupta Date: Sat, 2 Mar 2024 09:16:43 +0530 Subject: [PATCH 09/24] improved type declaration for predicate function --- .../array/base/count-if/docs/types/index.d.ts | 52 +++++++++++++++++-- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/count-if/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/base/count-if/docs/types/index.d.ts index 0aa52dd0f4a2..719556a52a39 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/array/base/count-if/docs/types/index.d.ts @@ -20,7 +20,51 @@ /// -import { Collection } from '@stdlib/types/array'; +import { Collection, AccessorArrayLike } from '@stdlib/types/array'; + +/** +* Returns a boolean indicating whether an element passes a test. +* +* @returns boolean indicating whether an element passes a test +*/ +type Nullary = ( this: U ) => boolean; + +/** +* Returns a boolean indicating whether an element passes a test. +* +* @param value - current array element +* @returns boolean indicating whether an element passes a test +*/ +type Unary = ( this: U, value: T ) => boolean; + +/** +* Returns a boolean indicating whether an element passes a test. +* +* @param value - current array element +* @param index - current array element index +* @returns boolean indicating whether an element passes a test +*/ +type Binary = ( this: U, value: T, index: number ) => boolean; + +/** +* Returns a boolean indicating whether an element passes a test. +* +* @param value - current array element +* @param index - current array element index +* @param arr - input array +* @returns boolean indicating whether an element passes a test +*/ +type Ternary = ( this: U, value: T, index: number, arr: Collection | AccessorArrayLike ) => boolean; + +/** +* Returns a boolean indicating whether an element passes a test. +* +* @param value - current array element +* @param index - current array element index +* @param arr - input array +* @returns boolean indicating whether an element passes a test +*/ +type Predicate = Nullary | Unary | Binary | Ternary; /** * Counts the number of truthy values in an array. @@ -32,13 +76,13 @@ import { Collection } from '@stdlib/types/array'; * * @example * var x = [ 0, 1, 0, 1 ]; -* var predicate = function( v ) { +* function predicate( v ) { * return v > this; * } -* var n = indexed( x, predicate ); +* var n = countIf( x, predicate, 0 ); * // returns 2 */ -declare function countIf( x: Collection, predicate: ( ...args: Array ) => boolean, thisArg?: any ): number; +declare function countIf( x: Collection | AccessorArrayLike, predicate: Predicate, thisArg?: ThisParameterType> ): number; // EXPORTS // From c14e1222f68cdc5094d03e3dd85b6dc3b377aab8 Mon Sep 17 00:00:00 2001 From: Utkarsh <137638507+Ut-the-pro@users.noreply.github.com> Date: Sat, 2 Mar 2024 09:19:15 +0530 Subject: [PATCH 10/24] Update lib/node_modules/@stdlib/array/base/count-if/README.md Co-authored-by: Philipp Burckhardt Signed-off-by: Utkarsh <137638507+Ut-the-pro@users.noreply.github.com> --- lib/node_modules/@stdlib/array/base/count-if/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/base/count-if/README.md b/lib/node_modules/@stdlib/array/base/count-if/README.md index 78877e4050f8..6e25061a0210 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/README.md +++ b/lib/node_modules/@stdlib/array/base/count-if/README.md @@ -20,7 +20,7 @@ limitations under the License. # countIf -> Counts the number of elements in an array that satisfy the provided testing function. +> Count the number of elements in an array that satisfy the provided testing function. From 310ea3bfd1663de73ecffe14fed38f30cd5892f3 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Sat, 2 Mar 2024 12:42:11 -0500 Subject: [PATCH 11/24] Apply suggestions from code review Signed-off-by: Philipp Burckhardt --- .../@stdlib/array/base/count-if/docs/types/test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/count-if/docs/types/test.ts b/lib/node_modules/@stdlib/array/base/count-if/docs/types/test.ts index 70d4ac100a2b..44fb086bdf46 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/docs/types/test.ts +++ b/lib/node_modules/@stdlib/array/base/count-if/docs/types/test.ts @@ -23,14 +23,14 @@ import countIf from './index'; // The function returns a number... { - countIf( [ 1, 2, 3 ], ()=>{ return true } ); // $ExpectType number + countIf( [ 1, 2, 3 ], () => { return true } ); // $ExpectType number } // The compiler throws an error if the function is provided an argument which is not a collection or the function is not boolean returning... { - countIf( [ 5 ], function(){ return 1 } ); // $ExpectError + countIf( [ 5 ], function() { return 1 } ); // $ExpectError countIf( true ); // $ExpectError - countIf( false, function(){ return false} ); // $ExpectError + countIf( false, function() { return false} ); // $ExpectError countIf( null ); // $ExpectError countIf( [ {}, {} ], ()=>{} ); // $ExpectError } From a7ecf09d62e37eeba8d38ec9afea51ed4e21ad2b Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Sat, 2 Mar 2024 12:42:23 -0500 Subject: [PATCH 12/24] Update lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt Signed-off-by: Philipp Burckhardt --- lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt b/lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt index 092f6eee5998..893b744c3810 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt +++ b/lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt @@ -1,7 +1,7 @@ {{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 that satisfy the provided testing + function. Parameters ---------- From b88c937808c5c0f007cceb4545fc59212b312a6e Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Sat, 2 Mar 2024 12:42:29 -0500 Subject: [PATCH 13/24] Update lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt Signed-off-by: Philipp Burckhardt --- lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt b/lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt index 893b744c3810..8fa90eb66dab 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt +++ b/lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt @@ -17,8 +17,8 @@ Returns ------- out: integer - Number of truthy values for which - the testing function evaluates to true. + Number of truthy values for which the testing function evaluates to + true. Examples -------- From 3c1765ff4527b1bd33845081b5899d26ed6b9095 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Sat, 2 Mar 2024 15:40:53 -0500 Subject: [PATCH 14/24] Apply suggestions from code review Signed-off-by: Philipp Burckhardt --- .../@stdlib/array/base/count-if/docs/repl.txt | 4 ++-- .../@stdlib/array/base/count-if/lib/main.js | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt b/lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt index 8fa90eb66dab..457b1181c5f7 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt +++ b/lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt @@ -23,8 +23,8 @@ Examples -------- > var out = {{alias}}( [ 0, 1, 1 ], function predicate( v ) { - ... return v === 1; - ... } ) + ... return v === 1; + ... } ) 2 See Also diff --git a/lib/node_modules/@stdlib/array/base/count-if/lib/main.js b/lib/node_modules/@stdlib/array/base/count-if/lib/main.js index 052972896af1..898305980626 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/lib/main.js +++ b/lib/node_modules/@stdlib/array/base/count-if/lib/main.js @@ -39,7 +39,7 @@ var reinterpret = require( '@stdlib/strided/base/reinterpret-complex' ); * * @example * var x = [ 0, 1, 0, 1 ]; -* function predicate( v ) { +* function predicate( v ) { * return v > 0; * } * var n = indexed( x, predicate ); @@ -72,7 +72,7 @@ function indexed( x, predicate, thisArg ) { * * var x = toAccessorArray( [ 0, 1, 0, 1 ] ); * function predicate( v ) { -* return v > 0; +* return v > 0; * } * var n = accessors( x, predicate ); * // returns 2 @@ -107,7 +107,7 @@ function accessors( x, predicate, thisArg ) { * * 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; +* return v > 0; * } * var n = complex( x, predicate ); * // returns 2 @@ -135,14 +135,14 @@ function complex( x, predicate, thisArg ) { * Counts the number of elements in an array that satisfy the provided testing function. * * @param {Collection} x - input array -* @param {(...args: any[]) => boolean} predicate - testing array +* @param {Function} predicate - testing array * @param {*} [thisArg] - function context * @returns {NonNegativeInteger} number of truthy values * * @example * var x = [ 0, 1, 0, 1, 1 ]; * function predicate( v ) { -* return v > 0; +* return v > 0; * } * var n = countIf( x, predicate ); * // returns 3 From d8f6acb41d270fdb651ed24d9cdb679e7ceff505 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Sat, 2 Mar 2024 15:41:08 -0500 Subject: [PATCH 15/24] Update lib/node_modules/@stdlib/array/base/count-if/README.md Signed-off-by: Philipp Burckhardt --- lib/node_modules/@stdlib/array/base/count-if/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/base/count-if/README.md b/lib/node_modules/@stdlib/array/base/count-if/README.md index 6e25061a0210..e8134016fdc1 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/README.md +++ b/lib/node_modules/@stdlib/array/base/count-if/README.md @@ -49,7 +49,7 @@ var x = [ 0, 1, 0, 1, 2 ]; function predicate( val ) { return ( val % 2 === 0 ) -}; +} var out = countIf( x, predicate ); // returns 3 From 4abe551e3a563dba08a3212a0e37ed63fd2d7122 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Sat, 2 Mar 2024 15:41:12 -0500 Subject: [PATCH 16/24] Update lib/node_modules/@stdlib/array/base/count-if/README.md Signed-off-by: Philipp Burckhardt --- lib/node_modules/@stdlib/array/base/count-if/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/base/count-if/README.md b/lib/node_modules/@stdlib/array/base/count-if/README.md index e8134016fdc1..d26196d87d81 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/README.md +++ b/lib/node_modules/@stdlib/array/base/count-if/README.md @@ -48,7 +48,7 @@ Counts the number of elements in an array that satisfy the provided testing func var x = [ 0, 1, 0, 1, 2 ]; function predicate( val ) { - return ( val % 2 === 0 ) + return ( val % 2 === 0 ); } var out = countIf( x, predicate ); From 555404e5634fca02b983216756226c957c7fb23b Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Sat, 2 Mar 2024 15:48:43 -0500 Subject: [PATCH 17/24] Update lib/node_modules/@stdlib/array/base/count-if/README.md Signed-off-by: Philipp Burckhardt --- lib/node_modules/@stdlib/array/base/count-if/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/base/count-if/README.md b/lib/node_modules/@stdlib/array/base/count-if/README.md index d26196d87d81..74d789246843 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/README.md +++ b/lib/node_modules/@stdlib/array/base/count-if/README.md @@ -69,7 +69,7 @@ To set the `predicate` function execution context, provide a `thisArg`. var x = [ 1, 2, 3, 4 ]; var context = { - `target`: 3 + 'target': 3 }; function predicate( value ) { From 82cfd6fe0c00e1cdc44aaf101a920e97b98c1f2f Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Sat, 2 Mar 2024 15:48:48 -0500 Subject: [PATCH 18/24] Update lib/node_modules/@stdlib/array/base/count-if/examples/index.js Signed-off-by: Philipp Burckhardt --- lib/node_modules/@stdlib/array/base/count-if/examples/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/base/count-if/examples/index.js b/lib/node_modules/@stdlib/array/base/count-if/examples/index.js index f50d77c2342f..2b9ebd60d0b4 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/examples/index.js +++ b/lib/node_modules/@stdlib/array/base/count-if/examples/index.js @@ -29,7 +29,7 @@ console.log( x ); var threshold = 0; function predicate( val ) { - return (val > this) + return val === 1; } var n = countIf( x, predicate, threshold ); From 8b026b81a27679adb30e11733bfffeed0438e0f7 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Sat, 2 Mar 2024 15:48:55 -0500 Subject: [PATCH 19/24] Update lib/node_modules/@stdlib/array/base/count-if/examples/index.js Signed-off-by: Philipp Burckhardt --- lib/node_modules/@stdlib/array/base/count-if/examples/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/base/count-if/examples/index.js b/lib/node_modules/@stdlib/array/base/count-if/examples/index.js index 2b9ebd60d0b4..0b6d95dcbf13 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/examples/index.js +++ b/lib/node_modules/@stdlib/array/base/count-if/examples/index.js @@ -34,4 +34,3 @@ function predicate( val ) { var n = countIf( x, predicate, threshold ); console.log( n ); - From e591873449f4d5319f481f059da7c27fdec6e6ca Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Sat, 2 Mar 2024 15:49:00 -0500 Subject: [PATCH 20/24] Update lib/node_modules/@stdlib/array/base/count-if/examples/index.js Signed-off-by: Philipp Burckhardt --- lib/node_modules/@stdlib/array/base/count-if/examples/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/base/count-if/examples/index.js b/lib/node_modules/@stdlib/array/base/count-if/examples/index.js index 0b6d95dcbf13..c3866964b0bb 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/examples/index.js +++ b/lib/node_modules/@stdlib/array/base/count-if/examples/index.js @@ -28,7 +28,7 @@ console.log( x ); var threshold = 0; -function predicate( val ) { +function predicate( val ) { return val === 1; } From 66f8debe3b767f81fd42169b0402049e8eb01b14 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Sat, 2 Mar 2024 15:54:48 -0500 Subject: [PATCH 21/24] Update lib/node_modules/@stdlib/array/base/count-if/README.md Signed-off-by: Philipp Burckhardt --- lib/node_modules/@stdlib/array/base/count-if/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/base/count-if/README.md b/lib/node_modules/@stdlib/array/base/count-if/README.md index 74d789246843..368ab4a45690 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/README.md +++ b/lib/node_modules/@stdlib/array/base/count-if/README.md @@ -47,7 +47,7 @@ Counts the number of elements in an array that satisfy the provided testing func ```javascript var x = [ 0, 1, 0, 1, 2 ]; -function predicate( val ) { +function predicate( val ) { return ( val % 2 === 0 ); } From 9a400f6a5cb4d9f8ef39b2919abd99764007eea7 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Sat, 2 Mar 2024 15:58:32 -0500 Subject: [PATCH 22/24] Update lib/node_modules/@stdlib/array/base/count-if/examples/index.js Signed-off-by: Philipp Burckhardt --- lib/node_modules/@stdlib/array/base/count-if/examples/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/base/count-if/examples/index.js b/lib/node_modules/@stdlib/array/base/count-if/examples/index.js index c3866964b0bb..30d4a733c799 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/examples/index.js +++ b/lib/node_modules/@stdlib/array/base/count-if/examples/index.js @@ -29,7 +29,7 @@ console.log( x ); var threshold = 0; function predicate( val ) { - return val === 1; + return val === 1; } var n = countIf( x, predicate, threshold ); From 085e2659d93dfddca4b235b26507c05106c182c6 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Sat, 2 Mar 2024 16:06:38 -0500 Subject: [PATCH 23/24] Update test.js Signed-off-by: Philipp Burckhardt --- .../@stdlib/array/base/count-if/test/test.js | 303 +++++++++--------- 1 file changed, 158 insertions(+), 145 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/count-if/test/test.js b/lib/node_modules/@stdlib/array/base/count-if/test/test.js index f30e78b9d5c2..86fb05295f8d 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/test/test.js +++ b/lib/node_modules/@stdlib/array/base/count-if/test/test.js @@ -36,183 +36,196 @@ tape( 'main export is a function', function test( t ) { }); tape( 'the function counts the number of truthy values based on a testing function (generic)', function test( t ) { - var expected; - var actual; - var x; - var thisArg = 2; - - x = [ 0, 1, 0, 1, 2 ]; - expected = 3; - actual = countIf( x, function( v ) { - return ( v % this === 0 ); - }, thisArg ); - - t.strictEqual( actual, expected, 'returns expected value' ); - t.end(); + var expected; + var actual; + var x; + + x = [ 0, 1, 0, 1, 2 ]; + expected = 3; + actual = countIf( x, function predicate( v ) { + return ( v % 2 === 0 ); + }); + + t.strictEqual( actual, expected, '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; - var actual; - var x; - - x = toAccessorArray([ 0, 1, 0, 1, 2 ]); - expected = 2; - actual = countIf( x, function( v ) { - return v === 1; - }); - - t.strictEqual( actual, expected, 'returns expected value' ); - t.end(); + var expected; + var actual; + var x; + + x = toAccessorArray([ 0, 1, 0, 1, 2 ]); + expected = 2; + actual = countIf( x, function predicate( v ) { + return v === 1; + }); + + t.strictEqual( actual, expected, '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; - var actual; - var x; - var thisArg = 1; - - x = new Int32Array([ 0, 1, 0, 1, 2 ]); - expected = 1; - actual = countIf( x, function( v ) { - return v > this; - }, thisArg ); - - t.strictEqual( actual, expected, 'returns expected value' ); - t.end(); + var expected; + var actual; + var x; + + x = new Int32Array([ 0, 1, 0, 1, 2 ]); + expected = 1; + actual = countIf( x, function predicate( v ) { + return v > 1; + }); + + t.strictEqual( actual, expected, '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; - 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( v ) { - return v === 0; - }); - - t.strictEqual( actual, expected, 'returns expected value' ); - t.end(); + var expected; + 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; + }); + + t.strictEqual( actual, expected, 'returns expected value' ); + t.end(); }); tape( 'the function returns zero if provided an array of length `0`', function test( t ) { - var expected; - var actual; - var x; - - expected = 0; - - x = []; - actual = countIf( x, function( v ) { - return v === 1; - }); - t.strictEqual( actual, expected, 'returns expected value' ); - - x = toAccessorArray([]); - actual = countIf( x, function( v ) { - return v === 1; - }); - t.strictEqual( actual, expected, 'returns expected value' ); - - x = new Int32Array([]); - actual = countIf( x, function( v ) { - return v === 1; - }); - t.strictEqual( actual, expected, 'returns expected value' ); - - x = new Complex128Array([]); - actual = countIf( x, function( v ) { - return v === 1; - }); - t.strictEqual( actual, expected, 'returns expected value' ); - - t.end(); + var expected; + 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' ); + + t.end(); }); tape( 'the function returns zero if no truthy values are found', function test( t ) { - var expected; - var actual; - var x; + var expected; + var actual; + var x; - expected = 0; + expected = 0; - x = [ 0, 0, 0, 0 ]; - actual = countIf( x, function( v ) { - return v === 1; - }); - t.strictEqual( actual, expected, 'returns expected value' ); + x = [ 0, 0, 0, 0 ]; + actual = countIf( x, function predicate( v ) { + return v === 1; + }); + t.strictEqual( actual, expected, 'returns expected value' ); - t.end(); + t.end(); }); tape( 'the function returns the length of the array if all values are truthy', function test( t ) { - var expected; - var actual; - var x; + var expected; + var actual; + var x; - x = [ 1, 1, 1, 1 ]; - expected = x.length; + x = [ 1, 1, 1, 1 ]; + expected = x.length; - actual = countIf( x, function( v ) { - return v === 1; - }); - t.strictEqual( actual, expected, 'returns expected value' ); + actual = countIf( x, function predicate( v ) { + return v === 1; + }); + t.strictEqual( actual, expected, 'returns expected value' ); - t.end(); + t.end(); }); tape( 'the function supports providing a custom execution context', function test( t ) { - var expected; - var actual; - var x; - var context = { - threshold : 2 - }; - - x = [ 1, 2, 3, 4, 5 ]; - expected = 3; // Only values greater than 2 - actual = countIf( x, function( v ) { - return v > this.threshold; - }, context ); - - t.strictEqual( actual, expected, 'returns expected value' ); - - t.end(); + var expected; + var context; + 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 ); + + t.strictEqual( actual, expected, '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 actual; - var x; - var thisArg = { - target : 20 - }; - - 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( v ) { - return v.value === this.target; - }, thisArg ); - - t.strictEqual( actual, expected, 'returns expected value' ); - - t.end(); + var expected; + var thisArg; + var actual; + var x; + + thisArg = { + 'target': 20 + }; + 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' ); + + t.end(); }); 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 = 0; - var x = [ false, 0, '', null, undefined ]; - var actual = countIf( x, function( v ) { - return v; // Returns truthy values - }); - t.strictEqual( actual, expected, 'returns expected value' ); - t.end(); + var expected; + 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' ); + t.end(); }); From 297f300397eba4e0ce2060aadacff3339798817e Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Sat, 2 Mar 2024 16:09:40 -0500 Subject: [PATCH 24/24] Update benchmark.length.js Signed-off-by: Philipp Burckhardt --- .../array/base/count-if/benchmark/benchmark.length.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/count-if/benchmark/benchmark.length.js b/lib/node_modules/@stdlib/array/base/count-if/benchmark/benchmark.length.js index b756749adb38..13a9bc4c09c6 100644 --- a/lib/node_modules/@stdlib/array/base/count-if/benchmark/benchmark.length.js +++ b/lib/node_modules/@stdlib/array/base/count-if/benchmark/benchmark.length.js @@ -55,9 +55,9 @@ function createBenchmark( len ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - out = countIf( x, function( v ) { - return v === 1; - } ); + out = countIf( x, function predicate( v ) { + return v === 1; + } ); if ( typeof out !== 'number' ) { b.fail( 'should return a number' ); }