Skip to content

Commit

Permalink
[#586] Added query language parser
Browse files Browse the repository at this point in the history
  • Loading branch information
vasek committed Feb 19, 2016
1 parent 7a69075 commit bfe5734
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions frontend/src/common/parseQuery.ts
@@ -0,0 +1,32 @@
/**
* @param query Search query to parse
*/
export default function parseQuery(query: string) {

// Regularize white spacing
// Make in-between white spaces a unique space
query = query.trim().replace(/s+/g, ' ');

// https://regex101.com/r/wT6zG3/2
const regex = /(-)?(?:(\S+):)?(?:'((?:[^'\\]|\\.)*)'|"((?:[^"\\]|\\.)*)"|(\S+))/g;
let term;
let terms = {};

while((term = regex.exec(query)) !== null) {
if (term !== regex.lastIndex) {
regex.lastIndex++;
}

const key = (term[2] || 'text').toLowerCase();

terms[key] = terms[key] || [];

terms[key].push({
n: term[1] === '-',
s: term[3] || term[4] || term[5]
});
}

return terms;

}

0 comments on commit bfe5734

Please sign in to comment.