-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
63 lines (54 loc) · 1.69 KB
/
index.js
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
const assert = require('./error/assert');
const extend = require('./config/extend');
const tokenizer = require('./tokenizer');
const config = require('./config');
const replaceOperators = require('./operators/replace');
const clearSquare = require('./clearSquare');
const simplify = require('./simplify');
const beautify = require('./beautify');
/**
* @typedef {[
* string, object
* ]} Query
*/
module.exports = {
/**
* Current configuration of the parser.
*
* @type Config
*/
config: config(),
/**
* Change the configuration of the parser. Accepts objects with single attributes by
* extending the default values or the name of a .json config file located under
* /config.
*
* @param {Config|string} cfg
*/
setConfig: function (cfg) {
this.config = extend()(this.config, typeof cfg === 'string' ? config(cfg) : cfg);
},
/**
* Insert string to be parsed here. Returns a two-dimensional array containing the
* parsed statements. Throws ArgumentErrors if anything goes wrong.
*
* @param {string} query
* @returns {Array}
* @throws {ArgumentError}
*/
parse: function parse(query) {
assert(typeof query === 'string' && query !== '', 2000);
const _config = config({
and: '*',
or: '+',
lookDelimiter: '+'
});
query = [query, {}];
query = tokenizer(this.config)(query);
query = replaceOperators(this.config)(query, _config);
query = clearSquare(_config)(query, this.config);
query = simplify(_config)(query);
query = beautify(_config)(query, this.config);
return query;
}
};