-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathsearch.js
More file actions
58 lines (52 loc) · 2.17 KB
/
search.js
File metadata and controls
58 lines (52 loc) · 2.17 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
import { PRINTABLE_ASCII } from '../const';
import v from '../voca';
describe('search', function() {
it('should return the index of a match', function() {
expect(v.search('we have a mission', /mission/)).toBe(10);
expect(v.search('we have a mission', 'a')).toBe(4);
expect(v.search('we have a mission', /we/)).toBe(0);
expect(v.search('we have a mission', /\s/)).toBe(2);
expect(v.search('we have a mission', '')).toBe(0);
expect(v.search('', '')).toBe(0);
expect(v.search(undefined, '')).toBe(0);
expect(v.search(null, '')).toBe(0);
expect(v.search(PRINTABLE_ASCII, '!')).toBe(1);
});
it('should return the index of a match and start index', function() {
expect(v.search('we have a mission', /a/, 6)).toBe(8);
expect(v.search('we have a mission', /we/, 0)).toBe(0);
expect(v.search('we have a mission', 'we', NaN)).toBe(0);
expect(v.search('we have a mission', '', 0)).toBe(0);
expect(v.search(PRINTABLE_ASCII, '#', 3)).toBe(3);
});
it('should return the index of a searched string in a string representation of an object', function() {
expect(v.search(['we have a mission'], /a/)).toBe(4);
expect(
v.search(
{
toString: function() {
return 'we have a mission';
},
},
/we/
)
).toBe(0);
});
it('should threat a null value as "null" match pattern', function() {
expect(v.search('we have a null mission', null)).toBe(10);
expect(v.search('we have a mission', null)).toBe(-1);
});
it('should return -1 for an invalid ending string and position', function() {
expect(v.search('we have a mission', /me/)).toBe(-1);
expect(v.search('we have a mission', /12/)).toBe(-1);
expect(v.search('we have a mission', /\s^/)).toBe(-1);
expect(v.search('we have a mission', 'we', 3)).toBe(-1);
expect(v.search('we have a mission', /mission/, 100)).toBe(-1);
expect(v.search('we have a mission', /mission/, Infinity)).toBe(-1);
expect(v.search('', /me/)).toBe(-1);
});
it('should return 0 for an undefined', function() {
expect(v.search('we have a mission')).toBe(0);
expect(v.search('we have a mission', undefined)).toBe(0);
});
});