-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathcount_where.js
More file actions
70 lines (65 loc) · 2.14 KB
/
count_where.js
File metadata and controls
70 lines (65 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { PRINTABLE_ASCII } from '../const';
import v from '../voca';
describe('countWhere', function() {
it('should return the number of characters in a string for a predicate', function() {
expect(v.countWhere('', v.isAlpha)).toBe(0);
expect(v.countWhere('africa654', v.isAlpha)).toBe(6);
expect(v.countWhere('790', v.isAlpha)).toBe(0);
expect(v.countWhere(PRINTABLE_ASCII, v.isDigit)).toBe(10);
expect(
v.countWhere('****--**--**', function(character) {
return character === '*';
})
).toBe(8);
expect(
v.countWhere('****--**--**', function() {
return false;
})
).toBe(0);
});
it('should invoke the predicate with correct parameters and context', function() {
let verifyIndex = 0;
const context = {};
const verifyString = '0123456789';
expect(
v.countWhere(
verifyString,
function(character, index, string) {
expect(index).toBe(verifyIndex);
expect(this).toBe(context);
expect(string).toBe(verifyString);
expect(character).toBe(verifyString[verifyIndex]);
verifyIndex++;
return true;
},
context
)
).toBe(10);
});
it('should return the number of characters in a number for a predicate', function() {
expect(v.countWhere(123, v.isDigit)).toBe(3);
expect(v.countWhere(0, v.isDigit)).toBe(1);
expect(v.countWhere(-1.5, v.isDigit)).toBe(2);
});
it('should return the number of characters in a string representation of an object for a predicate', function() {
expect(v.countWhere(['droplet'], v.isDigit)).toBe(0);
expect(
v.countWhere(
{
toString: function() {
return 'homo sapiens';
},
},
v.isAlphaDigit
)
).toBe(11);
});
it('should return zero for a non function predicate', function() {
expect(v.countWhere('africa')).toBe(0);
expect(v.countWhere('africa', undefined)).toBe(0);
expect(v.countWhere('africa', null)).toBe(0);
expect(v.countWhere('africa', 'africa')).toBe(0);
expect(v.countWhere('africa', 0)).toBe(0);
expect(v.countWhere()).toBe(0);
});
});