-
Notifications
You must be signed in to change notification settings - Fork 1
/
request.js
54 lines (54 loc) · 1.54 KB
/
request.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
"use strict";
if (!String.prototype.encodeHTML) {
String.prototype.encodeHTML = function () {
return this.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
};
}
if (!String.prototype.decodeHTML) {
String.prototype.decodeHTML = function () {
return this.replace(/'/g, "'")
.replace(/"/g, '"')
.replace(/>/g, '>')
.replace(/</g, '<')
.replace(/&/g, '&');
};
}
class Request {
constructor(str, title) {
if (!str) {
this.all = true;
return;
}
this.all = false;
let lower = str.toLowerCase();
if (lower.indexOf("select ") == 0) {
// this is a sql.
this.sql = str.decodeHTML();
this.title = title || this.sql;
}
if (str.indexOf(":") > 0) {
let context = str.split(":")[0].trim();
if (context && context.toLowerCase().indexOf("queries")) {
throw "You're trying to start new conversation.";
}
}
if (str.match(/\w{8}-\w{4}-\w{4}-\w{4}-\w{12}/)) {
throw "You're trying to start new conversation.";
}
let fields = str.trim().split(/\s*,\s*/);
for (let i = 0; i < fields.length; i++) {
fields[i] = fields[i].replace(/\s(.)/g, (x) => x.toUpperCase().trim()).replace(/^(.)/, (x) => x.toLowerCase());
}
this.title = title || str;
if (fields.length && fields.length == 1 && fields[0].indexOf('detail') == 0) {
this.all = true;
return;
}
this.fields = fields;
}
}
module.exports = Request;