Skip to content

Commit d0e9106

Browse files
committed
edit: formatting with prettierrc
1 parent 3df1433 commit d0e9106

File tree

4 files changed

+56
-26
lines changed

4 files changed

+56
-26
lines changed

.prettierrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"trailingComma": "all",
3+
"tabWidth": 2,
4+
"semi": false,
5+
"singleQuote": true,
6+
}

src/index.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ import { buildSearch } from './search'
44
import { buildFilters } from './filter'
55

66
/** @returns an AQL query object. See @param query for details on required
7-
* values. @parm query .terms accepts either a string to be parsed or an array of @param term
7+
* values. @parm query .terms accepts either a string to be parsed or an array
8+
* of @param term
89
* */
9-
export function buildAQL(query: query, limit: any = { start: 0, end: 20 }): any {
10+
export function buildAQL(
11+
query: query,
12+
limit: any = { start: 0, end: 20 },
13+
): any {
1014
validateQuery(query)
1115

1216
const SEARCH = buildSearch(query)
@@ -22,6 +26,8 @@ export function buildAQL(query: query, limit: any = { start: 0, end: 20 }): any
2226
}
2327

2428
function validateQuery(query: query) {
25-
if (!query.view.length) throw Error('query.view must be a valid ArangoSearch View name')
26-
if (!query.collections.length) throw new Error('query.collections must have at least one name')
29+
if (!query.view.length)
30+
throw new Error('query.view must be a valid ArangoSearch View name')
31+
if (!query.collections.length)
32+
throw new Error('query.collections must have at least one name')
2733
}

src/parse.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {term} from './lib/structs'
1+
import { term } from './lib/structs'
22

33
export function parseQuery(queryString: string): term[] {
44
const queryRgx: RegExp = /[+?-]?(["'(]).+?(\1|\))|[^"'()\s]+/g
@@ -9,12 +9,12 @@ export function parseQuery(queryString: string): term[] {
99
return matches.map(match => {
1010
/* strip op */
1111
let op = '?'
12-
if (/[+?-]/.test(match[0])) {
13-
op = match[0]
12+
if (/[+?-]/.test(match[ 0 ])) {
13+
op = match[ 0 ]
1414
match = match.substring(1)
1515
}
1616

17-
if (match[0] == '"' || match[0] == "'") {
17+
if (match[ 0 ] == '"' || match[ 0 ] == "'") {
1818
return {
1919
type: 'phr',
2020
val: match,

src/search.ts

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ import { aql } from 'arangojs'
22
import { query, collection, term } from './lib/structs'
33
import { parseQuery } from './parse'
44

5-
65
export function buildSearch(query: query): any {
76
/* parse string query */
8-
query.terms = typeof query.terms == 'string'
9-
? parseQuery(query.terms)
10-
: query.terms
7+
query.terms =
8+
typeof query.terms == 'string' ? parseQuery(query.terms) : query.terms
119

1210
/* build boolean pieces */
1311
let ANDS = buildOPS(query.collections, query.terms, '+', query.key)
@@ -22,7 +20,9 @@ export function buildSearch(query: query): any {
2220
if (!!NOTS) {
2321
NOTS = aql`${ANDS || ORS ? aql.literal(' AND ') : undefined}
2422
${NOTS.phrases ? aql.literal(' NOT ') : undefined} ${NOTS.phrases}
25-
${NOTS.phrases && NOTS.tokens ? aql.literal(' AND ') : undefined} ${NOTS.tokens}`
23+
${NOTS.phrases && NOTS.tokens ? aql.literal(' AND ') : undefined} ${
24+
NOTS.tokens
25+
}`
2626
}
2727

2828
/* if an empty query.terms string or array is passed, SEARCH true, bringing
@@ -33,19 +33,24 @@ export function buildSearch(query: query): any {
3333
${ORS}
3434
${NOTS}
3535
${(!ANDS && !ORS && !NOTS) || undefined}
36-
OPTIONS ${{ collections: query.collections.map(c => c.name) }}
36+
OPTIONS ${{ collections: query.collections.map((c) => c.name) }}
3737
SORT TFIDF(doc) DESC`
3838
}
3939

40-
function buildOPS(collections: collection[], terms: term[], op: string, key:
41-
string = 'text'): any {
40+
function buildOPS(
41+
collections: collection[],
42+
terms: term[],
43+
op: string,
44+
key: string = 'text',
45+
): any {
4246
const opWord: string = op == '+' ? ' AND ' : ' OR '
4347

4448
let queryTerms: any = terms.filter((t: term) => t.op == op)
4549
if (!queryTerms.length) return
4650

4751
/* phrases */
48-
let phrases = queryTerms.filter((qT: term) => qT.type == 'phr')
52+
let phrases = queryTerms
53+
.filter((qT: term) => qT.type == 'phr')
4954
.map((phrase: any) => buildPhrase(phrase, collections, key))
5055
if (!phrases.length) {
5156
phrases = undefined
@@ -60,17 +65,25 @@ function buildOPS(collections: collection[], terms: term[], op: string, key:
6065
if (!phrases && !tokens) return
6166
if (op == '-') return { phrases, tokens }
6267
if (phrases && tokens) return aql.join([ phrases, tokens ], opWord)
63-
return (tokens || phrases)
68+
return tokens || phrases
6469
}
6570

66-
function buildPhrase(phrase: term, collections: collection[], key: string): any {
67-
const phrases = collections.map(coll => {
71+
function buildPhrase(
72+
phrase: term,
73+
collections: collection[],
74+
key: string,
75+
): any {
76+
const phrases = collections.map((coll) => {
6877
return aql`PHRASE(doc.${key}, ${phrase.val.slice(1, -1)}, ${coll.analyzer})`
6978
})
7079
return aql`(${aql.join(phrases, ' OR ')})`
7180
}
7281

73-
function buildTokens(tokens: term[], collections: collection[], key: string): any {
82+
function buildTokens(
83+
tokens: term[],
84+
collections: collection[],
85+
key: string,
86+
): any {
7487
if (!tokens.length) return
7588

7689
const opWordMap = {
@@ -85,19 +98,24 @@ function buildTokens(tokens: term[], collections: collection[], key: string): an
8598
return a
8699
}, {})
87100

88-
const makeTokenAnalyzers = (tokens: term[], op: string, analyzer: string,
89-
key: string) => {
101+
const makeTokenAnalyzers = (
102+
tokens: term[],
103+
op: string,
104+
analyzer: string,
105+
key: string,
106+
) => {
90107
return aql`
91108
ANALYZER(
92109
TOKENS(${tokens}, ${analyzer})
93110
${aql.literal(op)} IN doc.${key}, ${analyzer})`
94111
}
95112

96113
let remapped = []
97-
collections.forEach(coll => {
114+
collections.forEach((coll) => {
98115
remapped.push(
99-
...Object.keys(mapped).map(op => makeTokenAnalyzers(mapped[ op ], op,
100-
coll.analyzer, key))
116+
...Object.keys(mapped).map((op) =>
117+
makeTokenAnalyzers(mapped[ op ], op, coll.analyzer, key),
118+
),
101119
)
102120
})
103121

0 commit comments

Comments
 (0)