-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.ts
60 lines (51 loc) · 1.44 KB
/
parse.ts
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
import { expect } from 'chai'
import { parseQuery } from '../src/parse'
describe('parse.ts', () => {
it('should export a function parseQuery', () => {
expect(parseQuery).to.be.a('function')
})
it(`should return an an empty array
when an empty string is passed`, () => {
expect(parseQuery('')).to.be.an('array')
})
it(`should return an array of parsed optional term objects
when a string is passed with no op hints`, () => {
const parsedQuery = parseQuery('terms "a and b" c')
expect(parsedQuery).to.be.an('array').that.has.lengthOf(3)
expect(parsedQuery[0]).to.deep.equal({
op: '?',
type: 'tok',
val: 'terms',
})
expect(parsedQuery[1]).to.deep.equal({
op: '?',
type: 'phr',
val: '"a and b"',
})
})
it(`should return an array of parsed term objects
when a complex string is passed`, () => {
const parsedQuery = parseQuery('terms +"must have" -cannot -"have this"')
expect(parsedQuery).to.be.an('array').that.has.lengthOf(4)
expect(parsedQuery[0]).to.deep.equal({
op: '?',
type: 'tok',
val: 'terms',
})
expect(parsedQuery[1]).to.deep.equal({
op: '+',
type: 'phr',
val: '"must have"',
})
expect(parsedQuery[2]).to.deep.equal({
op: '-',
type: 'tok',
val: 'cannot',
})
expect(parsedQuery[3]).to.deep.equal({
op: '-',
type: 'phr',
val: '"have this"',
})
})
})