Skip to content

Commit 310c13c

Browse files
committed
fix: address handling of complex arrays, refactor tests, and clean-up
Ref: #1372 Ref: ecd3518
1 parent 64d9f19 commit 310c13c

File tree

10 files changed

+278
-254
lines changed

10 files changed

+278
-254
lines changed

lib/node_modules/@stdlib/array/base/count-if/README.md

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ limitations under the License.
2020

2121
# countIf
2222

23-
> Count the number of elements in an array that satisfy the provided testing function.
23+
> Count the number of elements in an array which pass a test implemented by a predicate function.
2424
2525
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
2626

@@ -42,21 +42,19 @@ var countIf = require( '@stdlib/array/base/count-if' );
4242

4343
#### countIf( x, predicate\[, thisArg] )
4444

45-
Counts the number of elements in an array that satisfy the provided testing function.
45+
Counts the number of elements in an array which pass a test implemented by a predicate function.
4646

4747
```javascript
48-
var x = [ 0, 1, 0, 1, 2 ];
49-
50-
function predicate( val ) {
51-
return ( val % 2 === 0 );
48+
function predicate( value ) {
49+
return ( value > 0 );
5250
}
5351

52+
var x = [ 0, 1, 0, 1, 2 ];
53+
5454
var out = countIf( x, predicate );
5555
// returns 3
5656
```
5757

58-
If a `predicate` function returns a truthy value, the function counts that value.
59-
6058
The `predicate` function is provided three arguments:
6159

6260
- **value**: current array element.
@@ -66,18 +64,22 @@ The `predicate` function is provided three arguments:
6664
To set the `predicate` function execution context, provide a `thisArg`.
6765

6866
```javascript
69-
var x = [ 1, 2, 3, 4 ];
67+
function predicate( value ) {
68+
this.count += 1;
69+
return ( value > 0 );
70+
}
71+
72+
var x = [ 0, 1, 0, 1, 2 ];
7073

7174
var context = {
72-
'target': 3
75+
'count': 0
7376
};
7477

75-
function predicate( value ) {
76-
return ( value > this.target );
77-
}
78-
7978
var out = countIf( x, predicate, context );
80-
// returns 1
79+
// returns 3
80+
81+
var cnt = context.count;
82+
// returns 5
8183
```
8284

8385
</section>
@@ -101,19 +103,21 @@ var out = countIf( x, predicate, context );
101103
<!-- eslint no-undef: "error" -->
102104

103105
```javascript
104-
var bernoulli = require( '@stdlib/random/array/bernoulli' );
106+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
107+
var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
108+
var naryFunction = require( '@stdlib/utils/nary-function' );
105109
var countIf = require( '@stdlib/array/base/count-if' );
106110

107-
var x = bernoulli( 100, 0.5, {
108-
'dtype': 'generic'
111+
var x = discreteUniform( 10, -5, 5, {
112+
'dtype': 'int32'
109113
});
110-
console.log( x );
114+
// returns <Int32Array>
111115

112-
function predicate( val ) {
113-
return val === 1;
114-
}
115-
var n = countIf( x, predicate );
116-
console.log( n );
116+
var out = countIf( x, naryFunction( isPositiveInteger, 1 ) );
117+
// returns <number>
118+
119+
console.log( x );
120+
console.log( out );
117121
```
118122

119123
</section>

lib/node_modules/@stdlib/array/base/count-if/benchmark/benchmark.length.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ var countIf = require( './../lib' );
3030

3131
// FUNCTIONS //
3232

33+
/**
34+
* Predicate function.
35+
*
36+
* @private
37+
* @param {number} v - value
38+
* @returns {boolean} result
39+
*/
40+
function predicate( v ) {
41+
return v > 0.0;
42+
}
43+
3344
/**
3445
* Creates a benchmark function.
3546
*
@@ -55,9 +66,7 @@ function createBenchmark( len ) {
5566

5667
b.tic();
5768
for ( i = 0; i < b.iterations; i++ ) {
58-
out = countIf( x, function predicate( v ) {
59-
return v === 1;
60-
} );
69+
out = countIf( x, predicate );
6170
if ( typeof out !== 'number' ) {
6271
b.fail( 'should return a number' );
6372
}

lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
11

22
{{alias}}( x, predicate[, thisArg] )
3-
Counts the number of elements in an array that satisfy the provided testing
4-
function.
3+
Counts the number of elements in an array which pass a test implemented by a
4+
predicate function.
5+
6+
The predicate function is provided three arguments:
7+
8+
- value: current array element.
9+
- index: current array element index.
10+
- arr: the input array.
511

612
Parameters
713
----------
814
x: ArrayLikeObject
915
Input array.
1016

1117
predicate: Function
12-
Testing function.
18+
Predicate function.
1319

1420
thisArg: any (optional)
1521
Execution context.
1622

1723
Returns
1824
-------
1925
out: integer
20-
Number of truthy values for which the testing function evaluates to
21-
true.
26+
Result.
2227

2328
Examples
2429
--------
25-
> var out = {{alias}}( [ 0, 1, 1 ], function predicate( v ) {
26-
... return v === 1;
27-
... } )
30+
> function f( v ) { return ( v > 0 ); };
31+
> var out = {{alias}}( [ 0, 1, 1 ], f )
2832
2
2933

3034
See Also

lib/node_modules/@stdlib/array/base/count-if/docs/types/index.d.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,22 @@ type Ternary<T, U> = ( this: U, value: T, index: number, arr: Collection<T> | Ac
6767
type Predicate<T, U> = Nullary<U> | Unary<T, U> | Binary<T, U> | Ternary<T, U>;
6868

6969
/**
70-
* Counts the number of truthy values in an array.
70+
* Counts the number of elements in an array which pass a test implemented by a predicate function.
7171
*
7272
* @param x - input array
73-
* @param predicate - testing function
74-
* @param thisArg - function context
75-
* @returns number of values for which the provided function evaluates to true
73+
* @param predicate - predicate function
74+
* @param thisArg - predicate function execution context
75+
* @returns result
7676
*
7777
* @example
78-
* var x = [ 0, 1, 0, 1 ];
7978
* function predicate( v ) {
80-
* return v > this;
79+
* return v > 0;
8180
* }
82-
* var n = countIf( x, predicate, 0 );
83-
* // returns 2
81+
*
82+
* var x = [ 0, 1, 0, 1, 1 ];
83+
*
84+
* var n = countIf( x, predicate );
85+
* // returns 3
8486
*/
8587
declare function countIf<T = unknown, U = unknown>( x: Collection<T> | AccessorArrayLike<T>, predicate: Predicate<T, U>, thisArg?: ThisParameterType<Predicate<T, U>> ): number;
8688

lib/node_modules/@stdlib/array/base/count-if/docs/types/test.ts

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,86 @@
1616
* limitations under the License.
1717
*/
1818

19-
import countIf from './index';
19+
import toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
20+
import countIf = require( './index' );
21+
22+
/**
23+
* Tests whether a value is positive.
24+
*
25+
* @param value - input value
26+
* @returns boolean indicating whether an element is positive
27+
*/
28+
function isPositive( value: number ): boolean {
29+
return ( value > 0 );
30+
}
2031

2132

2233
// TESTS //
2334

2435
// The function returns a number...
2536
{
26-
countIf( [ 1, 2, 3 ], () => { return true } ); // $ExpectType number
37+
countIf( [ 1, 2, 3 ], isPositive ); // $ExpectType number
38+
countIf( new Float64Array( [ 1, 2, 3 ] ), isPositive ); // $ExpectType number
39+
countIf( new Float32Array( [ 1, 2, 3 ] ), isPositive ); // $ExpectType number
40+
countIf( new Int32Array( [ 1, 2, 3 ] ), isPositive ); // $ExpectType number
41+
countIf( new Int16Array( [ 1, 2, 3 ] ), isPositive ); // $ExpectType number
42+
countIf( new Int8Array( [ 1, 2, 3 ] ), isPositive ); // $ExpectType number
43+
countIf( new Uint32Array( [ 1, 2, 3 ] ), isPositive ); // $ExpectType number
44+
countIf( new Uint16Array( [ 1, 2, 3 ] ), isPositive ); // $ExpectType number
45+
countIf( new Uint8Array( [ 1, 2, 3 ] ), isPositive ); // $ExpectType number
46+
countIf( new Uint8ClampedArray( [ 1, 2, 3 ] ), isPositive ); // $ExpectType number
47+
countIf( toAccessorArray( [ 1, 2, 3 ] ), isPositive ); // $ExpectType number
48+
49+
countIf( [ 1, 2, 3 ], isPositive, {} ); // $ExpectType number
50+
countIf( new Float64Array( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType number
51+
countIf( new Float32Array( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType number
52+
countIf( new Int32Array( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType number
53+
countIf( new Int16Array( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType number
54+
countIf( new Int8Array( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType number
55+
countIf( new Uint32Array( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType number
56+
countIf( new Uint16Array( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType number
57+
countIf( new Uint8Array( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType number
58+
countIf( new Uint8ClampedArray( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType number
59+
countIf( toAccessorArray( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType number
60+
}
61+
62+
// The compiler throws an error if the function is provided a first argument which is not a collection...
63+
{
64+
countIf( 2, isPositive ); // $ExpectError
65+
countIf( false, isPositive ); // $ExpectError
66+
countIf( true, isPositive ); // $ExpectError
67+
countIf( {}, isPositive ); // $ExpectError
68+
69+
countIf( 2, isPositive, {} ); // $ExpectError
70+
countIf( false, isPositive, {} ); // $ExpectError
71+
countIf( true, isPositive, {} ); // $ExpectError
72+
countIf( {}, isPositive, {} ); // $ExpectError
2773
}
2874

29-
// The compiler throws an error if the function is provided an argument which is not a collection or the function is not boolean returning...
75+
// The compiler throws an error if the function is provided a second argument which is not a function...
3076
{
31-
countIf( [ 5 ], function() { return 1 } ); // $ExpectError
32-
countIf( true ); // $ExpectError
33-
countIf( false, function() { return false} ); // $ExpectError
34-
countIf( null ); // $ExpectError
35-
countIf( [ {}, {} ], ()=>{} ); // $ExpectError
77+
countIf( [ 1, 2, 3 ], 'abc' ); // $ExpectError
78+
countIf( [ 1, 2, 3 ], 2 ); // $ExpectError
79+
countIf( [ 1, 2, 3 ], false ); // $ExpectError
80+
countIf( [ 1, 2, 3 ], true ); // $ExpectError
81+
countIf( [ 1, 2, 3 ], null ); // $ExpectError
82+
countIf( [ 1, 2, 3 ], void 0 ); // $ExpectError
83+
countIf( [ 1, 2, 3 ], {} ); // $ExpectError
84+
countIf( [ 1, 2, 3 ], [] ); // $ExpectError
85+
86+
countIf( [ 1, 2, 3 ], 'abc', {} ); // $ExpectError
87+
countIf( [ 1, 2, 3 ], 2, {} ); // $ExpectError
88+
countIf( [ 1, 2, 3 ], false, {} ); // $ExpectError
89+
countIf( [ 1, 2, 3 ], true, {} ); // $ExpectError
90+
countIf( [ 1, 2, 3 ], null, {} ); // $ExpectError
91+
countIf( [ 1, 2, 3 ], void 0, {} ); // $ExpectError
92+
countIf( [ 1, 2, 3 ], {}, {} ); // $ExpectError
93+
countIf( [ 1, 2, 3 ], [], {} ); // $ExpectError
3694
}
3795

3896
// The compiler throws an error if the function is provided an unsupported number of arguments...
3997
{
4098
countIf(); // $ExpectError
41-
countIf( [ 1, 2, 3 ], 2 ); // $ExpectError
99+
countIf( [ 1, 2, 3 ] ); // $ExpectError
100+
countIf( [ 1, 2, 3 ], isPositive, {}, {} ); // $ExpectError
42101
}

lib/node_modules/@stdlib/array/base/count-if/examples/index.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,18 @@
1818

1919
'use strict';
2020

21-
var bernoulli = require( '@stdlib/random/array/bernoulli' );
21+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
22+
var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
23+
var naryFunction = require( '@stdlib/utils/nary-function' );
2224
var countIf = require( './../lib' );
2325

24-
var x = bernoulli( 100, 0.5, {
25-
'dtype': 'generic'
26+
var x = discreteUniform( 10, -5, 5, {
27+
'dtype': 'int32'
2628
});
27-
console.log( x );
28-
29-
var threshold = 0;
29+
// returns <Int32Array>
3030

31-
function predicate( val ) {
32-
return val === 1;
33-
}
31+
var out = countIf( x, naryFunction( isPositiveInteger, 1 ) );
32+
// returns <number>
3433

35-
var n = countIf( x, predicate, threshold );
36-
console.log( n );
34+
console.log( x );
35+
console.log( out );

lib/node_modules/@stdlib/array/base/count-if/lib/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@
1919
'use strict';
2020

2121
/**
22-
* Count the number of elements in an array that satisfy the provided testing function.
22+
* Count the number of elements in an array which pass a test implemented by a predicate function.
2323
*
2424
* @module @stdlib/array/base/count-if
2525
*
2626
* @example
2727
* var countIf = require( '@stdlib/array/base/count-if' );
2828
*
29-
* var x = [ 0, 1, 0, 1, 2 ];
30-
*
3129
* function predicate( value ) {
32-
* return ( value % 2 === 0 )
30+
* return ( value > 0 );
3331
* }
3432
*
33+
* var x = [ 0, 1, 0, 1, 2 ];
34+
*
3535
* var n = countIf( x, predicate );
3636
* // returns 3
3737
*/

0 commit comments

Comments
 (0)