dbgate-filterparser 5.0.5 → 5.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ import P from 'parsimmon';
2
+ export declare const datetimeParser: P.Language;
@@ -0,0 +1,205 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.datetimeParser = void 0;
7
+ const parsimmon_1 = __importDefault(require("parsimmon"));
8
+ const moment_1 = __importDefault(require("moment"));
9
+ const common_1 = require("./common");
10
+ const compoudCondition = conditionType => conditions => {
11
+ if (conditions.length == 1)
12
+ return conditions[0];
13
+ return {
14
+ [conditionType]: conditions,
15
+ };
16
+ };
17
+ function getTransformCondition(transform, value) {
18
+ return {
19
+ conditionType: 'binary',
20
+ operator: '=',
21
+ left: {
22
+ exprType: 'transform',
23
+ transform,
24
+ expr: {
25
+ exprType: 'placeholder',
26
+ },
27
+ },
28
+ right: {
29
+ exprType: 'value',
30
+ value,
31
+ },
32
+ };
33
+ }
34
+ const yearCondition = () => value => {
35
+ return getTransformCondition('YEAR', value);
36
+ };
37
+ const yearMonthCondition = () => value => {
38
+ const m = value.match(/(\d\d\d\d)-(\d\d?)/);
39
+ return {
40
+ conditionType: 'and',
41
+ conditions: [getTransformCondition('YEAR', m[1]), getTransformCondition('MONTH', m[2])],
42
+ };
43
+ };
44
+ const yearMonthDayCondition = () => value => {
45
+ const m = value.match(/(\d\d\d\d)-(\d\d?)-(\d\d?)/);
46
+ return {
47
+ conditionType: 'and',
48
+ conditions: [
49
+ getTransformCondition('YEAR', m[1]),
50
+ getTransformCondition('MONTH', m[2]),
51
+ getTransformCondition('DAY', m[3]),
52
+ ],
53
+ };
54
+ };
55
+ const yearEdge = edgeFunction => value => {
56
+ return (0, moment_1.default)(new Date(parseInt(value), 0, 1))[edgeFunction]('year')
57
+ .format('YYYY-MM-DDTHH:mm:ss.SSS');
58
+ };
59
+ const yearMonthEdge = edgeFunction => value => {
60
+ const m = value.match(/(\d\d\d\d)-(\d\d?)/);
61
+ return (0, moment_1.default)(new Date(parseInt(m[1]), parseInt(m[2]) - 1, 1))[edgeFunction]('month')
62
+ .format('YYYY-MM-DDTHH:mm:ss.SSS');
63
+ };
64
+ const yearMonthDayEdge = edgeFunction => value => {
65
+ const m = value.match(/(\d\d\d\d)-(\d\d?)-(\d\d?)/);
66
+ return (0, moment_1.default)(new Date(parseInt(m[1]), parseInt(m[2]) - 1, parseInt(m[3])))[edgeFunction]('day')
67
+ .format('YYYY-MM-DDTHH:mm:ss.SSS');
68
+ };
69
+ const yearMonthDayMinuteEdge = edgeFunction => value => {
70
+ const m = value.match(/(\d\d\d\d)-(\d\d?)-(\d\d?)\s+(\d\d?):(\d\d?)/);
71
+ const year = m[1];
72
+ const month = m[2];
73
+ const day = m[3];
74
+ const hour = m[4];
75
+ const minute = m[5];
76
+ const dateObject = new Date(year, month - 1, day, hour, minute);
77
+ return (0, moment_1.default)(dateObject)[edgeFunction]('minute').format('YYYY-MM-DDTHH:mm:ss.SSS');
78
+ };
79
+ const yearMonthDayMinuteSecondEdge = edgeFunction => value => {
80
+ const m = value.match(/(\d\d\d\d)-(\d\d?)-(\d\d?)(T|\s+)(\d\d?):(\d\d?):(\d\d?)/);
81
+ const year = m[1];
82
+ const month = m[2];
83
+ const day = m[3];
84
+ const hour = m[5];
85
+ const minute = m[6];
86
+ const second = m[7];
87
+ const dateObject = new Date(year, month - 1, day, hour, minute, second);
88
+ return (0, moment_1.default)(dateObject)[edgeFunction]('second').format('YYYY-MM-DDTHH:mm:ss.SSS');
89
+ };
90
+ const createIntervalCondition = (start, end) => {
91
+ return {
92
+ conditionType: 'and',
93
+ conditions: [
94
+ {
95
+ conditionType: 'binary',
96
+ operator: '>=',
97
+ left: {
98
+ exprType: 'placeholder',
99
+ },
100
+ right: {
101
+ exprType: 'value',
102
+ value: start,
103
+ },
104
+ },
105
+ {
106
+ conditionType: 'binary',
107
+ operator: '<=',
108
+ left: {
109
+ exprType: 'placeholder',
110
+ },
111
+ right: {
112
+ exprType: 'value',
113
+ value: end,
114
+ },
115
+ },
116
+ ],
117
+ };
118
+ };
119
+ const createDateIntervalCondition = (start, end) => {
120
+ return createIntervalCondition(start.format('YYYY-MM-DDTHH:mm:ss.SSS'), end.format('YYYY-MM-DDTHH:mm:ss.SSS'));
121
+ };
122
+ const fixedMomentIntervalCondition = (intervalType, diff) => () => {
123
+ return createDateIntervalCondition((0, moment_1.default)().add(intervalType, diff).startOf(intervalType), (0, moment_1.default)().add(intervalType, diff).endOf(intervalType));
124
+ };
125
+ const yearMonthDayMinuteCondition = () => value => {
126
+ const m = value.match(/(\d\d\d\d)-(\d\d?)-(\d\d?)\s+(\d\d?):(\d\d?)/);
127
+ const year = m[1];
128
+ const month = m[2];
129
+ const day = m[3];
130
+ const hour = m[4];
131
+ const minute = m[5];
132
+ const dateObject = new Date(year, month - 1, day, hour, minute);
133
+ return createDateIntervalCondition((0, moment_1.default)(dateObject).startOf('minute'), (0, moment_1.default)(dateObject).endOf('minute'));
134
+ };
135
+ const yearMonthDaySecondCondition = () => value => {
136
+ const m = value.match(/(\d\d\d\d)-(\d\d?)-(\d\d?)(T|\s+)(\d\d?):(\d\d?):(\d\d?)/);
137
+ const year = m[1];
138
+ const month = m[2];
139
+ const day = m[3];
140
+ const hour = m[5];
141
+ const minute = m[6];
142
+ const second = m[7];
143
+ const dateObject = new Date(year, month - 1, day, hour, minute, second);
144
+ return createDateIntervalCondition((0, moment_1.default)(dateObject).startOf('second'), (0, moment_1.default)(dateObject).endOf('second'));
145
+ };
146
+ const binaryCondition = operator => value => ({
147
+ conditionType: 'binary',
148
+ operator,
149
+ left: {
150
+ exprType: 'placeholder',
151
+ },
152
+ right: {
153
+ exprType: 'value',
154
+ value,
155
+ },
156
+ });
157
+ const createParser = () => {
158
+ const langDef = {
159
+ comma: () => (0, common_1.word)(','),
160
+ yearNum: () => parsimmon_1.default.regexp(/\d\d\d\d/).map(yearCondition()),
161
+ yearMonthNum: () => parsimmon_1.default.regexp(/\d\d\d\d-\d\d?/).map(yearMonthCondition()),
162
+ yearMonthDayNum: () => parsimmon_1.default.regexp(/\d\d\d\d-\d\d?-\d\d?/).map(yearMonthDayCondition()),
163
+ yearMonthDayMinute: () => parsimmon_1.default.regexp(/\d\d\d\d-\d\d?-\d\d?\s+\d\d?:\d\d?/).map(yearMonthDayMinuteCondition()),
164
+ yearMonthDaySecond: () => parsimmon_1.default.regexp(/\d\d\d\d-\d\d?-\d\d?(\s+|T)\d\d?:\d\d?:\d\d?/).map(yearMonthDaySecondCondition()),
165
+ yearNumStart: () => parsimmon_1.default.regexp(/\d\d\d\d/).map(yearEdge('startOf')),
166
+ yearNumEnd: () => parsimmon_1.default.regexp(/\d\d\d\d/).map(yearEdge('endOf')),
167
+ yearMonthStart: () => parsimmon_1.default.regexp(/\d\d\d\d-\d\d?/).map(yearMonthEdge('startOf')),
168
+ yearMonthEnd: () => parsimmon_1.default.regexp(/\d\d\d\d-\d\d?/).map(yearMonthEdge('endOf')),
169
+ yearMonthDayStart: () => parsimmon_1.default.regexp(/\d\d\d\d-\d\d?-\d\d?/).map(yearMonthDayEdge('startOf')),
170
+ yearMonthDayEnd: () => parsimmon_1.default.regexp(/\d\d\d\d-\d\d?-\d\d?/).map(yearMonthDayEdge('endOf')),
171
+ yearMonthDayMinuteStart: () => parsimmon_1.default.regexp(/\d\d\d\d-\d\d?-\d\d?\s+\d\d?:\d\d?/).map(yearMonthDayMinuteEdge('startOf')),
172
+ yearMonthDayMinuteEnd: () => parsimmon_1.default.regexp(/\d\d\d\d-\d\d?-\d\d?\s+\d\d?:\d\d?/).map(yearMonthDayMinuteEdge('endOf')),
173
+ yearMonthDayMinuteSecondStart: () => parsimmon_1.default.regexp(/\d\d\d\d-\d\d?-\d\d?(\s+|T)\d\d?:\d\d?:\d\d?/).map(yearMonthDayMinuteSecondEdge('startOf')),
174
+ yearMonthDayMinuteSecondEnd: () => parsimmon_1.default.regexp(/\d\d\d\d-\d\d?-\d\d?(\s+|T)\d\d?:\d\d?:\d\d?/).map(yearMonthDayMinuteSecondEdge('endOf')),
175
+ this: () => (0, common_1.word)('THIS'),
176
+ last: () => (0, common_1.word)('LAST'),
177
+ next: () => (0, common_1.word)('NEXT'),
178
+ week: () => (0, common_1.word)('WEEK'),
179
+ month: () => (0, common_1.word)('MONTH'),
180
+ year: () => (0, common_1.word)('YEAR'),
181
+ yesterday: () => (0, common_1.word)('YESTERDAY').map(fixedMomentIntervalCondition('day', -1)),
182
+ today: () => (0, common_1.word)('TODAY').map(fixedMomentIntervalCondition('day', 0)),
183
+ tomorrow: () => (0, common_1.word)('TOMORROW').map(fixedMomentIntervalCondition('day', 1)),
184
+ lastWeek: r => r.last.then(r.week).map(fixedMomentIntervalCondition('week', -1)),
185
+ thisWeek: r => r.this.then(r.week).map(fixedMomentIntervalCondition('week', 0)),
186
+ nextWeek: r => r.next.then(r.week).map(fixedMomentIntervalCondition('week', 1)),
187
+ lastMonth: r => r.last.then(r.month).map(fixedMomentIntervalCondition('month', -1)),
188
+ thisMonth: r => r.this.then(r.month).map(fixedMomentIntervalCondition('month', 0)),
189
+ nextMonth: r => r.next.then(r.month).map(fixedMomentIntervalCondition('month', 1)),
190
+ lastYear: r => r.last.then(r.year).map(fixedMomentIntervalCondition('year', -1)),
191
+ thisYear: r => r.this.then(r.year).map(fixedMomentIntervalCondition('year', 0)),
192
+ nextYear: r => r.next.then(r.year).map(fixedMomentIntervalCondition('year', 1)),
193
+ valueStart: r => parsimmon_1.default.alt(r.yearMonthDayMinuteSecondStart, r.yearMonthDayMinuteStart, r.yearMonthDayStart, r.yearMonthStart, r.yearNumStart),
194
+ valueEnd: r => parsimmon_1.default.alt(r.yearMonthDayMinuteSecondEnd, r.yearMonthDayMinuteEnd, r.yearMonthDayEnd, r.yearMonthEnd, r.yearNumEnd),
195
+ le: r => (0, common_1.word)('<=').then(r.valueEnd).map(binaryCondition('<=')),
196
+ ge: r => (0, common_1.word)('>=').then(r.valueStart).map(binaryCondition('>=')),
197
+ lt: r => (0, common_1.word)('<').then(r.valueStart).map(binaryCondition('<')),
198
+ gt: r => (0, common_1.word)('>').then(r.valueEnd).map(binaryCondition('>')),
199
+ element: r => parsimmon_1.default.alt(r.yearMonthDaySecond, r.yearMonthDayMinute, r.yearMonthDayNum, r.yearMonthNum, r.yearNum, r.yesterday, r.today, r.tomorrow, r.lastWeek, r.thisWeek, r.nextWeek, r.lastMonth, r.thisMonth, r.nextMonth, r.lastYear, r.thisYear, r.nextYear, r.le, r.lt, r.ge, r.gt).trim(common_1.whitespace),
200
+ factor: r => r.element.sepBy(common_1.whitespace).map(compoudCondition('$and')),
201
+ list: r => r.factor.sepBy(r.comma).map(compoudCondition('$or')),
202
+ };
203
+ return parsimmon_1.default.createLanguage(langDef);
204
+ };
205
+ exports.datetimeParser = createParser();
@@ -5,9 +5,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.parseFilter = void 0;
7
7
  const parsimmon_1 = __importDefault(require("parsimmon"));
8
- const moment_1 = __importDefault(require("moment"));
9
8
  const common_1 = require("./common");
10
9
  const mongoParser_1 = require("./mongoParser");
10
+ const datetimeParser_1 = require("./datetimeParser");
11
11
  const binaryCondition = operator => value => ({
12
12
  conditionType: 'binary',
13
13
  operator,
@@ -64,100 +64,6 @@ const negateCondition = condition => {
64
64
  condition,
65
65
  };
66
66
  };
67
- function getTransformCondition(transform, value) {
68
- return {
69
- conditionType: 'binary',
70
- operator: '=',
71
- left: {
72
- exprType: 'transform',
73
- transform,
74
- expr: {
75
- exprType: 'placeholder',
76
- },
77
- },
78
- right: {
79
- exprType: 'value',
80
- value,
81
- },
82
- };
83
- }
84
- const yearCondition = () => value => {
85
- return getTransformCondition('YEAR', value);
86
- };
87
- const yearMonthCondition = () => value => {
88
- const m = value.match(/(\d\d\d\d)-(\d\d?)/);
89
- return {
90
- conditionType: 'and',
91
- conditions: [getTransformCondition('YEAR', m[1]), getTransformCondition('MONTH', m[2])],
92
- };
93
- };
94
- const yearMonthDayCondition = () => value => {
95
- const m = value.match(/(\d\d\d\d)-(\d\d?)-(\d\d?)/);
96
- return {
97
- conditionType: 'and',
98
- conditions: [
99
- getTransformCondition('YEAR', m[1]),
100
- getTransformCondition('MONTH', m[2]),
101
- getTransformCondition('DAY', m[3]),
102
- ],
103
- };
104
- };
105
- const createIntervalCondition = (start, end) => {
106
- return {
107
- conditionType: 'and',
108
- conditions: [
109
- {
110
- conditionType: 'binary',
111
- operator: '>=',
112
- left: {
113
- exprType: 'placeholder',
114
- },
115
- right: {
116
- exprType: 'value',
117
- value: start,
118
- },
119
- },
120
- {
121
- conditionType: 'binary',
122
- operator: '<=',
123
- left: {
124
- exprType: 'placeholder',
125
- },
126
- right: {
127
- exprType: 'value',
128
- value: end,
129
- },
130
- },
131
- ],
132
- };
133
- };
134
- const createDateIntervalCondition = (start, end) => {
135
- return createIntervalCondition(start.format('YYYY-MM-DDTHH:mm:ss.SSS'), end.format('YYYY-MM-DDTHH:mm:ss.SSS'));
136
- };
137
- const fixedMomentIntervalCondition = (intervalType, diff) => () => {
138
- return createDateIntervalCondition((0, moment_1.default)().add(intervalType, diff).startOf(intervalType), (0, moment_1.default)().add(intervalType, diff).endOf(intervalType));
139
- };
140
- const yearMonthDayMinuteCondition = () => value => {
141
- const m = value.match(/(\d\d\d\d)-(\d\d?)-(\d\d?)\s+(\d\d?):(\d\d?)/);
142
- const year = m[1];
143
- const month = m[2];
144
- const day = m[3];
145
- const hour = m[4];
146
- const minute = m[5];
147
- const dateObject = new Date(year, month - 1, day, hour, minute);
148
- return createDateIntervalCondition((0, moment_1.default)(dateObject).startOf('minute'), (0, moment_1.default)(dateObject).endOf('minute'));
149
- };
150
- const yearMonthDaySecondCondition = () => value => {
151
- const m = value.match(/(\d\d\d\d)-(\d\d?)-(\d\d?)(T|\s+)(\d\d?):(\d\d?):(\d\d?)/);
152
- const year = m[1];
153
- const month = m[2];
154
- const day = m[3];
155
- const hour = m[5];
156
- const minute = m[6];
157
- const second = m[7];
158
- const dateObject = new Date(year, month - 1, day, hour, minute, second);
159
- return createDateIntervalCondition((0, moment_1.default)(dateObject).startOf('second'), (0, moment_1.default)(dateObject).endOf('second'));
160
- };
161
67
  const createParser = (filterType) => {
162
68
  const langDef = {
163
69
  string1: () => (0, common_1.token)(parsimmon_1.default.regexp(/"((?:\\.|.)*?)"/, 1))
@@ -176,11 +82,6 @@ const createParser = (filterType) => {
176
82
  .map(Number)
177
83
  .desc('number'),
178
84
  noQuotedString: () => parsimmon_1.default.regexp(/[^\s^,^'^"]+/).desc('string unquoted'),
179
- yearNum: () => parsimmon_1.default.regexp(/\d\d\d\d/).map(yearCondition()),
180
- yearMonthNum: () => parsimmon_1.default.regexp(/\d\d\d\d-\d\d?/).map(yearMonthCondition()),
181
- yearMonthDayNum: () => parsimmon_1.default.regexp(/\d\d\d\d-\d\d?-\d\d?/).map(yearMonthDayCondition()),
182
- yearMonthDayMinute: () => parsimmon_1.default.regexp(/\d\d\d\d-\d\d?-\d\d?\s+\d\d?:\d\d?/).map(yearMonthDayMinuteCondition()),
183
- yearMonthDaySecond: () => parsimmon_1.default.regexp(/\d\d\d\d-\d\d?-\d\d?(\s+|T)\d\d?:\d\d?:\d\d?/).map(yearMonthDaySecondCondition()),
184
85
  value: r => parsimmon_1.default.alt(...allowedValues.map(x => r[x])),
185
86
  valueTestEq: r => r.value.map(binaryCondition('=')),
186
87
  valueTestStr: r => r.value.map(likeCondition('like', '%#VALUE#%')),
@@ -194,24 +95,6 @@ const createParser = (filterType) => {
194
95
  false: () => parsimmon_1.default.regexp(/false/i).map(binaryFixedValueCondition('0')),
195
96
  trueNum: () => (0, common_1.word)('1').map(binaryFixedValueCondition('1')),
196
97
  falseNum: () => (0, common_1.word)('0').map(binaryFixedValueCondition('0')),
197
- this: () => (0, common_1.word)('THIS'),
198
- last: () => (0, common_1.word)('LAST'),
199
- next: () => (0, common_1.word)('NEXT'),
200
- week: () => (0, common_1.word)('WEEK'),
201
- month: () => (0, common_1.word)('MONTH'),
202
- year: () => (0, common_1.word)('YEAR'),
203
- yesterday: () => (0, common_1.word)('YESTERDAY').map(fixedMomentIntervalCondition('day', -1)),
204
- today: () => (0, common_1.word)('TODAY').map(fixedMomentIntervalCondition('day', 0)),
205
- tomorrow: () => (0, common_1.word)('TOMORROW').map(fixedMomentIntervalCondition('day', 1)),
206
- lastWeek: r => r.last.then(r.week).map(fixedMomentIntervalCondition('week', -1)),
207
- thisWeek: r => r.this.then(r.week).map(fixedMomentIntervalCondition('week', 0)),
208
- nextWeek: r => r.next.then(r.week).map(fixedMomentIntervalCondition('week', 1)),
209
- lastMonth: r => r.last.then(r.month).map(fixedMomentIntervalCondition('month', -1)),
210
- thisMonth: r => r.this.then(r.month).map(fixedMomentIntervalCondition('month', 0)),
211
- nextMonth: r => r.next.then(r.month).map(fixedMomentIntervalCondition('month', 1)),
212
- lastYear: r => r.last.then(r.year).map(fixedMomentIntervalCondition('year', -1)),
213
- thisYear: r => r.this.then(r.year).map(fixedMomentIntervalCondition('year', 0)),
214
- nextYear: r => r.next.then(r.year).map(fixedMomentIntervalCondition('year', 1)),
215
98
  eq: r => (0, common_1.word)('=').then(r.value).map(binaryCondition('=')),
216
99
  ne: r => (0, common_1.word)('!=').then(r.value).map(binaryCondition('<>')),
217
100
  ne2: r => (0, common_1.word)('<>').then(r.value).map(binaryCondition('<>')),
@@ -252,9 +135,6 @@ const createParser = (filterType) => {
252
135
  if (filterType == 'eval') {
253
136
  allowedElements.push('true', 'false');
254
137
  }
255
- if (filterType == 'datetime') {
256
- allowedElements.push('yearMonthDaySecond', 'yearMonthDayMinute', 'yearMonthDayNum', 'yearMonthNum', 'yearNum', 'yesterday', 'today', 'tomorrow', 'lastWeek', 'thisWeek', 'nextWeek', 'lastMonth', 'thisMonth', 'nextMonth', 'lastYear', 'thisYear', 'nextYear');
257
- }
258
138
  // must be last
259
139
  if (filterType == 'string' || filterType == 'eval') {
260
140
  allowedElements.push('valueTestStr');
@@ -267,10 +147,10 @@ const createParser = (filterType) => {
267
147
  const parsers = {
268
148
  number: createParser('number'),
269
149
  string: createParser('string'),
270
- datetime: createParser('datetime'),
271
150
  logical: createParser('logical'),
272
151
  eval: createParser('eval'),
273
152
  mongo: mongoParser_1.mongoParser,
153
+ datetime: datetimeParser_1.datetimeParser,
274
154
  };
275
155
  function parseFilter(value, filterType) {
276
156
  // console.log('PARSING', value, 'WITH', filterType);
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "5.0.5",
2
+ "version": "5.0.7",
3
3
  "name": "dbgate-filterparser",
4
4
  "main": "lib/index.js",
5
5
  "typings": "lib/index.d.ts",
@@ -13,7 +13,7 @@
13
13
  "lib"
14
14
  ],
15
15
  "devDependencies": {
16
- "dbgate-types": "^5.0.5",
16
+ "dbgate-types": "^5.0.7",
17
17
  "@types/jest": "^25.1.4",
18
18
  "@types/node": "^13.7.0",
19
19
  "jest": "^24.9.0",
@@ -22,7 +22,7 @@
22
22
  },
23
23
  "dependencies": {
24
24
  "@types/parsimmon": "^1.10.1",
25
- "dbgate-tools": "^5.0.5",
25
+ "dbgate-tools": "^5.0.7",
26
26
  "lodash": "^4.17.21",
27
27
  "moment": "^2.24.0",
28
28
  "parsimmon": "^1.13.0"