Skip to content

Commit 64c19d4

Browse files
committed
fixed issues & implementation of predicate functions at all places
1 parent e3f73ac commit 64c19d4

File tree

8 files changed

+98
-39
lines changed

8 files changed

+98
-39
lines changed

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

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,46 @@ limitations under the License.
4040
var countIf = require( '@stdlib/array/base/count-if' );
4141
```
4242

43-
#### countIf( x, predicate[, thisArg] )
43+
#### countIf( x, predicate\[, thisArg] )
4444

4545
Counts the number of elements in an array that satisfy the provided testing function.
4646

4747
```javascript
4848
var x = [ 0, 1, 0, 1, 2 ];
4949

50-
var predicate = function isEven( value ){ return ( value % 2 === 0 ) };
50+
function predicate( val ) {
51+
return ( val % 2 === 0; )
52+
};
5153

5254
var out = countIf( x, predicate );
5355
// returns 3
5456
```
5557

58+
If a `predicate` function returns a truthy value, the function counts that value.
59+
60+
The `predicate` function is provided three arguments:
61+
62+
- **value**: current array element.
63+
- **index**: current array element index.
64+
- **arr**: input array.
65+
66+
To set the `predicate` function execution context, provide a `thisArg`.
67+
68+
```javascript
69+
function predicate( value ) {
70+
return ( value > this.target );
71+
}
72+
73+
var x = [ 1, 2, 3, 4 ];
74+
75+
var context = {
76+
target: 3
77+
};
78+
79+
var out = countIf( x, predicate, context );
80+
// returns 1
81+
```
82+
5683
</section>
5784

5885
<!-- /.usage -->
@@ -82,8 +109,9 @@ var x = bernoulli( 100, 0.5, {
82109
});
83110
console.log( x );
84111

85-
var predicate = function ( val ) { return val === 1 }
86-
112+
function predicate( val ) {
113+
return val === 1;
114+
}
87115
var n = countIf( x, predicate );
88116
console.log( n );
89117
```

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
Testing function.
1212

1313
thisArg: any (optional)
14-
Input function's context.
14+
Execution context.
1515

1616
Returns
1717
-------

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ import { Collection } from '@stdlib/types/array';
3232
*
3333
* @example
3434
* var x = [ 0, 1, 0, 1 ];
35-
* var predicate = function( v, t ){ return v > t; }
36-
* var n = indexed( x, predicate, 0 );
35+
* var predicate = function( v ) {
36+
* return v > this;
37+
* }
38+
* var n = indexed( x, predicate );
3739
* // returns 2
3840
*/
3941
declare function countIf( x: Collection, predicate: ( ...args: any[] ) => boolean, thisArg?: any ): number;

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ var x = bernoulli( 100, 0.5, {
2626
});
2727
console.log( x );
2828

29-
var big = 10;
29+
var threshold = 0;
3030

31-
var predicate = function isBig( val, big ) { return (val > big) }
31+
function predicate( val ) {
32+
return (val > this)
33+
}
3234

33-
var n = countIf( x, predicate, big );
35+
var n = countIf( x, predicate, threshold );
3436
console.log( n );
37+

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

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

2121
/**
22-
* Counts the number of elements in an array that satisfy the provided testing function.
22+
* Count the number of elements in an array that satisfy the provided testing function.
2323
*
2424
* @module @stdlib/array/base/count-if
2525
*
@@ -28,7 +28,9 @@
2828
*
2929
* var x = [ 0, 1, 0, 1, 2 ];
3030
*
31-
* var predicate = function isEven( value ){ return ( value % 2 === 0 ) };
31+
* function predicate( value ) {
32+
* return ( value % 2 === 0 )
33+
* };
3234
*
3335
* var n = countIf( x, predicate );
3436
* // returns 3

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ var reinterpret = require( '@stdlib/strided/base/reinterpret-complex' );
3939
*
4040
* @example
4141
* var x = [ 0, 1, 0, 1 ];
42-
* var predicate = function( v, thisArg ){ return v > thisArg; }
43-
* var n = indexed( x, predicate, 0 );
42+
* function predicate( v ) {
43+
* return v > 0;
44+
* }
45+
* var n = indexed( x, predicate );
4446
* // returns 2
4547
*/
4648
function indexed( x, predicate, thisArg ) {
@@ -49,7 +51,7 @@ function indexed( x, predicate, thisArg ) {
4951

5052
n = 0;
5153
for ( i = 0; i < x.length; i++ ) {
52-
if ( predicate( x[ i ], thisArg ) ) {
54+
if ( predicate.call( thisArg, x[ i ], i, x ) ) {
5355
n += 1;
5456
}
5557
}
@@ -69,8 +71,10 @@ function indexed( x, predicate, thisArg ) {
6971
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
7072
*
7173
* var x = toAccessorArray( [ 0, 1, 0, 1 ] );
72-
* var predicate = function( v, thisArg ){ return v > thisArg; }
73-
* var n = accessors( x, predicate, 0 );
74+
* function predicate( v ) {
75+
* return v > 0;
76+
* }
77+
* var n = accessors( x, predicate );
7478
* // returns 2
7579
*/
7680
function accessors( x, predicate, thisArg ) {
@@ -82,7 +86,7 @@ function accessors( x, predicate, thisArg ) {
8286

8387
n = 0;
8488
for ( i = 0; i < x.length; i++ ) {
85-
if ( predicate( get( x, i ), thisArg ) ) {
89+
if ( predicate.call( thisArg, get( x, i ), i, x ) ) {
8690
n += 1;
8791
}
8892
}
@@ -96,14 +100,16 @@ function accessors( x, predicate, thisArg ) {
96100
* @param {Collection} x - input array
97101
* @param {(...args: any[]) => boolean} predicate - testing array
98102
* @param {*} [thisArg] - function context
99-
* @returns {NonNegativeInteger} number of values for which the provided function evalutes to true
103+
* @returns {NonNegativeInteger} number of values for which the provided function evaluates to true
100104
*
101105
* @example
102106
* var Complex128Array = require( '@stdlib/array/complex128' );
103107
*
104108
* var x = new Complex128Array( [ 1.0, 2.0, 0.0, 0.0, 3.0, 4.0, 0.0, 0.0 ] );
105-
* var predicate = function( v, thisArg ){ return v > thisArg; }
106-
* var n = complex( x );
109+
* function predicate( v ) {
110+
* return v > 0;
111+
* }
112+
* var n = complex( x, predicate );
107113
* // returns 2
108114
*/
109115
function complex( x, predicate, thisArg ) {
@@ -115,7 +121,7 @@ function complex( x, predicate, thisArg ) {
115121

116122
n = 0;
117123
for ( i = 0; i < view.length; i += 2 ) {
118-
if ( predicate( view[ i ], thisArg ) || predicate( view[ i+1 ], thisArg ) ) {
124+
if ( predicate.call( thisArg, view[ i ], i, view ) || predicate.call( thisArg, view[ i+1 ], i+1, view ) ) {
119125
n += 1;
120126
}
121127
}
@@ -135,8 +141,10 @@ function complex( x, predicate, thisArg ) {
135141
*
136142
* @example
137143
* var x = [ 0, 1, 0, 1, 1 ];
138-
* var predicate = function( v, thisArg ){ return v > thisArg; }
139-
* var n = countTruthy( x );
144+
* function predicate( v ) {
145+
* return v > 0;
146+
* }
147+
* var n = countIf( x, predicate );
140148
* // returns 3
141149
*/
142150
function countIf( x, predicate, thisArg ) {

lib/node_modules/@stdlib/array/base/count-if/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@stdlib/array/base/count-if",
33
"version": "0.0.0",
4-
"description": "Counts the number of elements in an array that satisfy the provided testing function.",
4+
"description": "Count the number of elements in an array that satisfy the provided testing function.",
55
"license": "Apache-2.0",
66
"author": {
77
"name": "The Stdlib Authors",

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

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ tape( 'the function counts the number of truthy values based on a testing functi
3939
var expected;
4040
var actual;
4141
var x;
42+
var thisArg = 2;
4243

4344
x = [ 0, 1, 0, 1, 2 ];
4445
expected = 3;
45-
actual = countIf( x, function( v, thisArg ) {
46-
return ( v % thisArg === 0 );
47-
}, 2 );
46+
actual = countIf( x, function( v ) {
47+
return ( v % this === 0 );
48+
}, thisArg );
4849

4950
t.strictEqual( actual, expected, 'returns expected value' );
5051
t.end();
@@ -69,12 +70,13 @@ tape( 'the function counts the number of truthy values based on a testing functi
6970
var expected;
7071
var actual;
7172
var x;
73+
var thisArg = 1;
7274

7375
x = new Int32Array([ 0, 1, 0, 1, 2 ]);
7476
expected = 1;
75-
actual = countIf( x, function( v, thisArg ) {
76-
return v > thisArg;
77-
}, 1 );
77+
actual = countIf( x, function( v ) {
78+
return v > this;
79+
}, thisArg );
7880

7981
t.strictEqual( actual, expected, 'returns expected value' );
8082
t.end();
@@ -165,13 +167,15 @@ tape( 'the function supports providing a custom execution context', function tes
165167
var expected;
166168
var actual;
167169
var x;
168-
var threshold = 2;
170+
var context = {
171+
threshold : 2
172+
};
169173

170174
x = [ 1, 2, 3, 4, 5 ];
171175
expected = 3; // Only values greater than 2
172-
actual = countIf( x, function( v, thisArg ) {
173-
return v > thisArg;
174-
}, threshold );
176+
actual = countIf( x, function( v ) {
177+
return v > this.threshold;
178+
}, context );
175179

176180
t.strictEqual( actual, expected, 'returns expected value' );
177181

@@ -183,7 +187,9 @@ tape( 'the function counts the number of objects with a custom predicate functio
183187
var expected;
184188
var actual;
185189
var x;
186-
var target = 20;
190+
var thisArg = {
191+
target : 20
192+
};
187193

188194
x = [
189195
{ name: 'John', value: 10 },
@@ -192,11 +198,21 @@ tape( 'the function counts the number of objects with a custom predicate functio
192198
];
193199
// Count the number of objects where the value property matches the target
194200
expected = 1; // One object have its value property equal to 20
195-
actual = countIf( x, function( v, thisArg ) {
196-
return v.value === thisArg;
197-
}, target );
201+
actual = countIf( x, function( v ) {
202+
return v.value === this.target;
203+
}, thisArg );
198204

199205
t.strictEqual( actual, expected, 'returns expected value' );
200206

201207
t.end();
202208
});
209+
210+
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 ) {
211+
var expected = 0;
212+
var x = [ false, 0, '', null, undefined ];
213+
var actual = countIf( x, function( v ) {
214+
return v; // Returns truthy values
215+
});
216+
t.strictEqual( actual, expected, 'returns expected value' );
217+
t.end();
218+
});

0 commit comments

Comments
 (0)